2020-11-13 11:36:52 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
statusfile=/var/tmp/backup.monitoring
|
|
|
|
ssh_login="${username}@${server}"
|
|
|
|
|
2021-08-04 13:55:14 +00:00
|
|
|
NL=$'\n'
|
|
|
|
|
2020-11-13 11:36:52 +00:00
|
|
|
if ! [[ -f /etc/backup.priv ]]
|
|
|
|
then
|
2021-08-08 05:47:07 +00:00
|
|
|
echo "/etc/backup.priv does not exist" | logger -t backup-client -p user.error
|
2020-11-13 11:36:52 +00:00
|
|
|
echo "abort_no_key" > "$statusfile"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-02-07 19:47:22 +00:00
|
|
|
run-parts --exit-on-error -- /etc/backup-pre-hooks.d
|
|
|
|
exitcode=$?
|
2021-02-09 06:17:25 +00:00
|
|
|
if [[ $exitcode != 0 ]]
|
2021-02-07 19:47:22 +00:00
|
|
|
then
|
2021-08-08 05:47:07 +00:00
|
|
|
echo "run-parts /etc/backup-pre-hooks.d exited $exitcode" | logger -t backup-client -p user.error
|
2021-02-07 19:47:22 +00:00
|
|
|
echo "hook $exitcode" > "$statusfile"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-02-13 08:18:00 +00:00
|
|
|
do_backup() {
|
2021-08-07 04:59:33 +00:00
|
|
|
rsync_errorcodes_for_this_path=""
|
|
|
|
backup_has_successfully_run="no"
|
|
|
|
|
|
|
|
for try in {1..5}
|
|
|
|
do
|
2021-08-08 05:47:07 +00:00
|
|
|
echo "Backup for '$1', try $try ..." | logger -t backup-client -p user.info
|
2021-08-07 04:59:33 +00:00
|
|
|
|
|
|
|
# Compress level 1 is a good compromise between speed and cpu usage.
|
|
|
|
rsync --compress-level=1 -aAP --numeric-ids --delete --relative \
|
|
|
|
--rsync-path="/usr/bin/rsync --fake-super" \
|
|
|
|
-e "ssh -o IdentityFile=/etc/backup.priv -o StrictHostKeyChecking=accept-new -p ${port}" \
|
|
|
|
"$1" "$ssh_login":backups/
|
|
|
|
|
|
|
|
# Exit code 24 means some files have vanished during rsync.
|
|
|
|
# I don't know why, but this is very common, apparently?
|
|
|
|
exitcode=$?
|
2021-08-08 05:47:07 +00:00
|
|
|
echo "Backup for '$1' try $try exited $exitcode" | logger -t backup-client -p user.info
|
2021-08-07 04:59:33 +00:00
|
|
|
if [[ $exitcode != 0 ]] && [[ $exitcode != 24 ]]
|
|
|
|
then
|
|
|
|
rsync_errorcodes_for_this_path+=" $exitcode"
|
2021-08-07 08:30:19 +00:00
|
|
|
sleep 30
|
2021-08-07 04:59:33 +00:00
|
|
|
else
|
|
|
|
backup_has_successfully_run="yes"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ "$backup_has_successfully_run" != "yes" ]]
|
2021-02-13 08:18:00 +00:00
|
|
|
then
|
2021-08-08 05:47:07 +00:00
|
|
|
echo "Backup for '$1' did not succeed!" | logger -t backup-client -p user.error
|
2021-08-04 13:55:14 +00:00
|
|
|
<%text>
|
2021-08-08 05:47:07 +00:00
|
|
|
rsync_errors+="${NL}${1}${rsync_errorcodes_for_this_path}"
|
2021-08-04 13:55:14 +00:00
|
|
|
</%text>
|
2021-02-13 08:18:00 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:36:52 +00:00
|
|
|
rsync_errors=""
|
|
|
|
% for path in sorted(paths):
|
2021-02-13 08:18:00 +00:00
|
|
|
do_backup "${path}"
|
2020-11-13 11:36:52 +00:00
|
|
|
% endfor
|
|
|
|
|
|
|
|
if [[ -n "$rsync_errors" ]]
|
|
|
|
then
|
|
|
|
echo "rsync_error$rsync_errors" > "$statusfile"
|
2020-11-13 12:23:57 +00:00
|
|
|
else
|
|
|
|
echo "ok" > "$statusfile"
|
2020-11-13 11:36:52 +00:00
|
|
|
fi
|