-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself_backup.sh
executable file
·81 lines (65 loc) · 1.94 KB
/
self_backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash
# A script to perform incremental backups using rsync
#
# Originally taken from https://linuxconfig.org/how-to-create-incremental-backups-using-rsync-on-linux
#
# This and the restore script don't really fit the aesthetic of
# LBCS, but I wanted to run these backups on basically all of my
# LBCS setups, so here we are.
#
# Example use (in crontab):
#
# # Daily backups
# 4 4 * * * <%= maindir %>/self_backup.sh account@account.rsync.net '+%j'
#
# # Daily restore test
# 5 5 * * * <%= maindir %>/self_restore_test.sh account@account.rsync.net webmaster@lojban.org
# Error trapping from https://gist.github.com/oldratlee/902ad9a398affca37bfcfab64612e7d1
__error_trapper() {
local parent_lineno="$1"
local code="$2"
local commands="$3"
echo "error exit status $code, at file $0 on or near line $parent_lineno: $commands"
}
trap '__error_trapper "${LINENO}/${BASH_LINENO}" "$?" "$BASH_COMMAND"' ERR
set -euE -o pipefail
shopt -s failglob
exec 2>&1
if [[ ! ${1-} ]]
then
echo "Need user@host for ssh as first argument."
exit 1
fi
dest="$1"
shift
if [[ ! ${1-} ]]
then
echo "Need date string (like '+%j') for backup schedule as second argument."
exit 1
fi
datestr="$1"
shift
BACKUP_DIR="backups/$(id -un)"
DATETIME="$(date "$datestr")"
BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
LATEST_LINK="${BACKUP_DIR}/latest"
# Set up the restore test file
rm -f "$HOME"/.rsync-restore-test-*
date "+%Y%m%d" >"$HOME/.rsync-restore-test-$(date +%Y%m%d)"
# Get the host key -_-;
ssh -o StrictHostKeyChecking=no "$dest" 'uname -a' || true
echo -e "\n\nRunning rsync.\n\n"
set -x
ssh "$dest" mkdir -p "${BACKUP_DIR}"
date
rsync -a -SHA --delete \
"$HOME/" \
--link-dest "../latest" \
--exclude=".cache" \
--exclude=".local" \
"$dest:${BACKUP_PATH}" || true
date
# shellcheck disable=SC2029
ssh "$dest" rm "${LATEST_LINK}" || true
ssh "$dest" ln -s "${DATETIME}" "${LATEST_LINK}"
echo "self_backup completed successfully"