bundlewrap/bundles/backup-client/files/generate-backup

72 lines
2 KiB
Text
Raw Normal View History

#!/bin/bash
statusfile=/var/tmp/backup.monitoring
ssh_login="${username}@${server}"
2021-08-04 13:55:14 +00:00
NL=$'\n'
if ! [[ -f /etc/backup.priv ]]
then
echo "/etc/backup.priv does not exist" | logger -t backup-client -p user.error
echo "abort_no_key" > "$statusfile"
exit 1
fi
run-parts --exit-on-error -- /etc/backup-pre-hooks.d
exitcode=$?
if [[ $exitcode != 0 ]]
then
echo "run-parts /etc/backup-pre-hooks.d exited $exitcode" | logger -t backup-client -p user.error
echo "hook $exitcode" > "$statusfile"
exit 1
fi
do_backup() {
rsync_errorcodes_for_this_path=""
backup_has_successfully_run="no"
for try in {1..5}
do
echo "Backup for '$1', try $try ..." | logger -t backup-client -p user.info
# 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=$?
echo "Backup for '$1' try $try exited $exitcode" | logger -t backup-client -p user.info
if [[ $exitcode != 0 ]] && [[ $exitcode != 24 ]]
then
rsync_errorcodes_for_this_path+=" $exitcode"
sleep 30
else
backup_has_successfully_run="yes"
break
fi
done
if [[ "$backup_has_successfully_run" != "yes" ]]
then
echo "Backup for '$1' did not succeed!" | logger -t backup-client -p user.error
2021-08-04 13:55:14 +00:00
<%text>
rsync_errors+="${NL}${1}${rsync_errorcodes_for_this_path}"
2021-08-04 13:55:14 +00:00
</%text>
fi
}
rsync_errors=""
% for path in sorted(paths):
do_backup "${path}"
% endfor
if [[ -n "$rsync_errors" ]]
then
echo "rsync_error$rsync_errors" > "$statusfile"
else
echo "ok" > "$statusfile"
fi