2023-06-24 18:30:55 -05:00
|
|
|
#!/bin/sh
|
|
|
|
|
2023-06-29 19:05:25 -05:00
|
|
|
# Setting this, so it can be ported to multiple device easily
|
2023-06-24 18:30:55 -05:00
|
|
|
remoteUser=""
|
2023-06-29 19:05:25 -05:00
|
|
|
remoteHost="reposerver"
|
|
|
|
machine="$( hostnamectl --static )"
|
2023-06-24 18:30:55 -05:00
|
|
|
export BORG_REPO="$remoteUser@$remoteHost:/backup/$machine"
|
|
|
|
|
2023-06-29 19:05:25 -05:00
|
|
|
# See the section "Passphrase notes" for more infos
|
2023-06-24 18:30:55 -05:00
|
|
|
export BORG_PASSPHRASE=''
|
|
|
|
|
|
|
|
# Non-interactively accept relocation of a repository
|
|
|
|
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
|
|
|
|
|
2023-06-29 19:05:25 -05:00
|
|
|
# Some helpers and error handling
|
|
|
|
logFile="/home/ez/.cache/logs/borg/$machine-$( date +%Y-%m-%d-%H:%M:%S.%N )"
|
2023-06-24 18:30:55 -05:00
|
|
|
infoLine() { printf "%s\t%s\n" "$( date +%H:%M:%S )" "$*" | tee -a "$logFile"; }
|
|
|
|
seeLog() { printf "\n%s %s" "$*" "$logFile"; }
|
|
|
|
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
|
|
|
|
|
|
|
|
# Notify user of running backup
|
|
|
|
notify() {
|
|
|
|
if [ "$(command -v sudo)" ] && [ "$(command -v notify-send)" ]; then
|
|
|
|
dialog_kind=$(echo "$1"|tr '[:upper:]' '[:lower:]')
|
|
|
|
if [ "$dialog_kind" = 'info' ]; then
|
|
|
|
dialog_kind='information'
|
|
|
|
fi
|
|
|
|
for u in $(users); do
|
2023-06-29 19:05:25 -05:00
|
|
|
sudo -u "$u" DISPLAY=:0
|
2023-06-24 18:30:55 -05:00
|
|
|
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/"$(sudo -u "$u" id -u)/bus" \
|
2023-06-29 19:05:25 -05:00
|
|
|
notify-send --app-name="Borg Backup" "$1" "$2" --icon="dialog-$dialog_kind"
|
2023-06-24 18:30:55 -05:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Remove logs of pruned backups
|
|
|
|
tidyLogs() {
|
|
|
|
mapfile -t removeList < <(grep -w "^Pruning archive" "$logFile" | awk '{ print $4 }' )
|
|
|
|
for ((i=0; i<${#removeList[@]}; i++)); do
|
2023-06-29 19:05:25 -05:00
|
|
|
# Expand path of logFile
|
|
|
|
# Slice off the current backup name
|
|
|
|
# Slice off seconds from timestamp
|
|
|
|
# Use wildcard to match the rest for matching filenames easily
|
|
|
|
set -- "${logFile%/*}"/"${removeList[i]%:*}"*
|
|
|
|
rm "$@"
|
2023-06-24 18:30:55 -05:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Backup the most important directories into an archive named after
|
2023-06-29 19:05:25 -05:00
|
|
|
# the machine this script is currently running on
|
2023-06-24 18:30:55 -05:00
|
|
|
notify info "Starting backup to $BORG_REPO"
|
|
|
|
infoLine "Starting backup to $BORG_REPO"
|
|
|
|
|
|
|
|
borg create \
|
|
|
|
--verbose \
|
|
|
|
--stats \
|
|
|
|
--show-rc \
|
|
|
|
--compression lz4 \
|
|
|
|
--progress \
|
|
|
|
--exclude-caches \
|
|
|
|
--exclude 'home/*/.cache/*' \
|
|
|
|
--exclude 'var/tmp/*' \
|
|
|
|
--exclude '/etc/mtab' \
|
|
|
|
--exclude '/backup/' \
|
|
|
|
--exclude '/dev/' \
|
|
|
|
--exclude '/lost+found/' \
|
|
|
|
--exclude '/mnt/' \
|
|
|
|
--exclude '/proc/' \
|
|
|
|
--exclude '/run/' \
|
|
|
|
--exclude '/sys/' \
|
|
|
|
--exclude '/tmp/' \
|
|
|
|
--exclude '/home/*/.cache/mozilla/firefox/' \
|
|
|
|
--exclude '/home/*/.local/share/Trash/' \
|
|
|
|
--exclude '/home/*/.thumbnails/' \
|
|
|
|
\
|
|
|
|
::'{hostname}-{now:%Y-%m-%d-%H:%M:%S.%f}' \
|
|
|
|
/boot \
|
|
|
|
/bin \
|
|
|
|
/efi \
|
|
|
|
/etc \
|
|
|
|
/home \
|
|
|
|
/lib \
|
|
|
|
/lib64 \
|
|
|
|
/opt \
|
|
|
|
/root \
|
|
|
|
/sbin \
|
|
|
|
/srv \
|
|
|
|
/usr \
|
|
|
|
/var \
|
|
|
|
2>> "$logFile"
|
|
|
|
|
|
|
|
backup_exit=$?
|
|
|
|
|
|
|
|
# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
|
|
|
|
# archives of THIS machine. The '{hostname}-*' matching is very important to
|
|
|
|
# limit prune's operation to this machine's archives and not apply to
|
2023-06-29 19:05:25 -05:00
|
|
|
# other machines' archives also
|
2023-06-24 18:52:07 -05:00
|
|
|
notify info "Pruning repository"
|
2023-06-24 18:30:55 -05:00
|
|
|
infoLine "Pruning repository"
|
|
|
|
|
|
|
|
borg prune \
|
|
|
|
--list \
|
|
|
|
--glob-archives '{hostname}-*' \
|
|
|
|
--show-rc \
|
|
|
|
--keep-daily 3 \
|
|
|
|
--keep-weekly 4 \
|
|
|
|
--keep-monthly 6 \
|
|
|
|
--keep-yearly 1 \
|
|
|
|
2>> "$logFile"
|
|
|
|
|
|
|
|
prune_exit=$?
|
|
|
|
|
|
|
|
# actually free repo disk space by compacting segments
|
2023-06-24 18:52:07 -05:00
|
|
|
notify info "Compacting repository"
|
2023-06-24 18:30:55 -05:00
|
|
|
infoLine "Compacting repository"
|
|
|
|
|
|
|
|
borg compact 2>> "$logFile"
|
|
|
|
|
|
|
|
compact_exit=$?
|
|
|
|
|
|
|
|
# Don't forget to tidy up the log files!
|
|
|
|
tidyLogs
|
|
|
|
|
|
|
|
# use highest exit code as global exit code
|
|
|
|
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
|
|
|
|
global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))
|
|
|
|
|
|
|
|
if [ ${global_exit} -eq 0 ]; then
|
|
|
|
notify info "Backup, Prune, and Compact finished successfully to $BORG_REPO"
|
|
|
|
infoLine "Backup, Prune, and Compact finished successfully to $BORG_REPO"
|
|
|
|
seeLog "Log available:"
|
|
|
|
elif [ ${global_exit} -eq 1 ]; then
|
|
|
|
notify info "Backup, Prune, and/or Compact finished with warnings"
|
|
|
|
infoLine "Backup, Prune, and/or Compact finished with warnings"
|
|
|
|
seeLog "Check log for warnings:"
|
|
|
|
else
|
|
|
|
notify info "Backup, Prune, and/or Compact finished with errors"
|
|
|
|
infoLine "Backup, Prune, and/or Compact finished with errors"
|
|
|
|
seeLog "Check log for errors:"
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit ${global_exit}
|