Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- ## Configuration
- # Backup
- backup_host=___HOST___ # e.g. hostname.local, 192.168.123.45
- backup_host_user=___USERNAME__ # e.g. borg
- backup_path=___PATH___ # e.g. /mnt/backup/Borg
- backup_repo=___REPO___ # e.g. default
- backup_passcommand=___COMMAND___ # e.g. "pass show backup"
- # Notifications
- notify_user=___USERNAME___ # e.g. myname
- notify_uid=___USER_ID___ # e.g. 1000
- ## Backup
- print_info() { printf "\n%s %s\n\n" "$( date '+%a, %Y-%m-%d %H:%M:%S' )" "$*" >&2; }
- # Check if the backup host is up
- host_up=`ping -c 1 $backup_host &> /dev/null; echo $?`
- if [ $host_up = 0 ]; then
- # Setting this, so the repo does not need to be given on the commandline:
- export BORG_REPO=ssh://$backup_host_user@$backup_host$backup_path/$backup_repo
- # Setting this, so you won't be asked for your repository passphrase:
- #export BORG_PASSPHRASE='XYZl0ngandsecurepa_55_phrasea&&123'
- # or this to ask an external program to supply the passphrase:
- export BORG_PASSCOMMAND=$backup_passcommand
- # some helpers and error handling:
- trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
- print_info "Starting backup"
- # Backup the most important directories into an archive named after
- # the machine this script is currently running on:
- borg create \
- --verbose \
- --filter AME \
- --list \
- --stats \
- --show-rc \
- --compression lz4 \
- --exclude-caches \
- --exclude '/home/*/.cache/*' \
- --exclude '/var/cache/*' \
- --exclude '/var/tmp/*' \
- \
- ::'{hostname}-{now}' \
- /etc \
- /home \
- /root \
- /var \
- backup_exit=$?
- print_info "Pruning repository"
- # Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
- # archives of THIS machine. The '{hostname}-' prefix is very important to
- # limit prune's operation to this machine's archives and not apply to
- # other machines' archives also:
- borg prune \
- --list \
- --prefix '{hostname}-' \
- --show-rc \
- --keep-hourly 24 \
- --keep-daily 7 \
- --keep-weekly 4 \
- --keep-monthly 6 \
- prune_exit=$?
- # use highest exit code as global exit code
- global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
- if [ ${global_exit} -eq 0 ]; then
- notify_message="Backup and Prune finished successfully."
- else
- notify_icon=dialog-warning
- notify_level=critical
- if [ ${global_exit} -eq 1 ]; then
- notify_message="Backup and/or Prune finished with warnings."
- else
- notify_message="Backup and/or Prune finished with errors."
- fi
- sudo -u $notify_user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$notify_uid/bus notify-send 'BorgBackup' "$notify_message" --icon=$notify_icon --urgency=$notify_level
- fi
- else
- global_exit=$host_up
- notify_message="Host is down. Skipped backup."
- fi
- print_info "${notify_message}"
- exit ${global_exit}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement