-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdo-backup.example
116 lines (96 loc) · 4.43 KB
/
do-backup.example
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
# https://borgbackup.readthedocs.io/en/stable/usage/create.html
# https://borgbackup.readthedocs.io/en/stable/usage/help.html#borg-help-patterns
# https://borgbackup.readthedocs.io/en/stable/usage/help.html#borg-help-placeholders
# https://refspecs.linuxfoundation.org/FHS_3.0/fhs-3.0.html
# http://www.pathname.com/fhs/pub/fhs-2.3.html
# https://manpages.debian.org/stretch/manpages-fr-extra/date.1.fr.html
# https://borgbackup.readthedocs.io/en/stable/faq.html#i-am-seeing-a-added-status-for-an-unchanged-file
# https://borgbackup.readthedocs.io/en/stable/faq.html#it-always-chunks-all-my-files-even-unchanged-ones
# Differentiated targets / dataset:
# sys = / /boot /etc /opt /var -/var/log
# data = /srv
# homes = /root /home
# logs = /var/log
bbbs_user_name=borg
COMMON_TARGET="/ /boot /etc /root /home /opt /srv /var /var/log"
COMMON_EXCLUDE="--exclude /sys/ --exclude /proc/ --exclude /dev/ --exclude /run/ --exclude /var/run/ --exclude /mnt/"
VOLATILE_EXCLUDE="--exclude /tmp/ --exclude /var/tmp/ --exclude /lost+found --exclude /var/cache/ --exclude /root/.cache --exclude /home/*/.cache"
# Especially when not using --one-file-system
# --exclude /var/run # -> /run
if [ "$(id --user --name)" != "$bbbs_user_name" ]; then
echo "$0 must be run as $bbbs_user_name"
exit
fi
bbbs_user_home=$(getent passwd "$bbbs_user_name" | cut -d: -f6)
bbbs_opt_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
log_prefix="${bbbs_user_home}/logs/"$(date --utc "+%Y-%m")
mkdir --parent "${log_prefix}"
pushd "$bbbs_user_home" > /dev/null
## Global
export BORGW_APPEND_ONLY=1
## Per client
hypervisor() {
# Hypervisor - https://pve.proxmox.com/wiki/Linux_Container
export BORGW_RESTRICT_REPOSITORY=/srv/borg/server-name/hypervisor
export SSH_ARGS=
host=server-name.fqdn
port=22
ssh_key="${bbbs_user_home}/ssh/key_server-name_hypervisor"
log_file="${log_prefix}/"$(date --utc "+%Y-%m-%d_%H.%M.%SZ")"_$host.log"
"${bbbs_opt_path}/ssh-wrapper-server" -t 12345 12345 "${bbbs_opt_path}/ssh-wrapper-client" "$host -p $port -oBatchMode=yes -i$ssh_key" \
sudo "${bbbs_opt_path}/bbbs-client" create \
--show-version --show-rc --verbose --list --stats --one-file-system --exclude-nodump --exclude-caches --keep-exclude-tags \
$COMMON_EXCLUDE $VOLATILE_EXCLUDE \
--exclude /var/lib/lxcfs/ \
--exclude /var/lib/vz/images/ \
ssh://backup-server/./::{utcnow:%Y-%m-%d} \
$COMMON_TARGET /etc/pve \
2>&1 | tee $log_file
}
vm_1() {
export BORGW_RESTRICT_REPOSITORY=/srv/borg/server-name/vm-1
gw_key=~/ssh/key_server-name_hypervisor
export SSH_ARGS="-o ProxyCommand=ssh -i$gw_key -W %h:%p server-name.fqdn -p 22"
host=172.16.0.1
port=22
ssh_key="${bbbs_user_home}/ssh/key_server-name_vm-1"
log_file="${log_prefix}/"$(date --utc "+%Y-%m-%d_%H.%M.%SZ")"_$host.log"
"${bbbs_opt_path}/ssh-wrapper-server" -t 12346 12346 "${bbbs_opt_path}/ssh-wrapper-client" "$host -p $port -oBatchMode=yes -i$ssh_key" \
sudo "${bbbs_opt_path}/bbbs-client" create \
--show-version --show-rc --verbose --list --stats --one-file-system --exclude-nodump --exclude-caches --keep-exclude-tags \
$COMMON_EXCLUDE $VOLATILE_EXCLUDE \
ssh://backup-server/./::{utcnow:%Y-%m-%d} \
$COMMON_TARGET \
2>&1 | tee $log_file
}
vm_1_stdin() {
export BORGW_RESTRICT_REPOSITORY=/srv/borg/server-name/vm-1
gw_key=~/ssh/key_server-name_hypervisor
export SSH_ARGS="-o ProxyCommand=ssh -i$gw_key -W %h:%p server-name.fqdn -p 22"
host=172.16.0.1
port=22
ssh_key="${bbbs_user_home}/ssh/key_server-name_vm-1"
log_file="${log_prefix}/"$(date --utc "+%Y-%m-%d_%H.%M.%SZ")"_$host_my-export.log"
"${bbbs_opt_path}/ssh-wrapper-server" -t 12347 12347 "${bbbs_opt_path}/ssh-wrapper-client" "$host -p $port -oBatchMode=yes -i$ssh_key" \
my_export_cmd cmd_arg1 cmd_arg2 "|" \
sudo "${bbbs_opt_path}/bbbs-client" create-stdin \
--show-version --show-rc --verbose --list --stats \
--stdin-name out-name.ext \
"ssh://backup-server/./::{utcnow:%Y-%m-%d}_my-export" - \
2>&1 | tee $log_file
}
#hypervisor
#vm_1
#vm_1_stdin
# Pass the host (=function) to backup as argument to the script
echo "------ Backups to process: $@"
for bak in "$@"; do
echo "---- Starting backup $bak"
"$bak"
echo "---- Backup $bak ended"
done
# Maybe the calls could be parallized with & (but you cannot use the same repo then)
# https://stackoverflow.com/questions/356100/how-to-wait-in-bash-for-several-subprocesses-to-finish-and-return-exit-code-0
# wait for remaining backup then
# killall -g -u "$bbbs_user_name"