Skip to content

Instantly share code, notes, and snippets.

@federicociro
Last active December 27, 2022 22:01
Show Gist options
  • Save federicociro/7e1bd52d19f8261491876a7f71ed565e to your computer and use it in GitHub Desktop.
Save federicociro/7e1bd52d19f8261491876a7f71ed565e to your computer and use it in GitHub Desktop.
Bash scripts to backup the system
[Unit]
Description=Create backup using Borg backup
[Service]
Type=oneshot
ExecStart=/bin/sh -c "/home/$USER/scripts/borg-backup.sh"
StandardOutput=file:/var/log/borg-backup.log
#!/bin/sh
# Docs: https://borgbackup.readthedocs.io/en/stable/index.html
# Source: https://ovk.github.io/silverbox/#_configuration_5
set -e
if pidof borg >/dev/null; then
echo "borg is already running"
exit 1
fi
borg_repo=/mnt/usb/backup_encrypt
borg_pass='passphrase'
# Setting this, so the repo does not need to be given on the commandline:
export BORG_REPO=$borg_repo
export BORG_PASSPHRASE=$borg_pass
# Prepare Nextcloud container to backup database
#OCC_OUTPUT=$(docker exec --user www-data nextcloud-fpm php occ maintenance:mode)
#OCC_OUTPUT=$(sudo -u www-data php /srv/http/nextcloud/occ maintenance:mode)
#if [ "$?" -ne "0" ]; then
# echo "failed to check if Nextcloud is already in maintenance mode"
# exit 1
#fi
#if ! printf "%s" "$OCC_OUTPUT" | grep -q "Maintenance mode is currently disabled"; then
# echo "unexpected occ output: $OCC_OUTPUT"
# exit 1
#fi
#if ! sudo -u www-data php /srv/http/nextcloud/occ maintenance:mode --on; then
# echo "failed to enable Nextcloud maintenance mode"
# exit 1
#fi
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
info "Starting backup"
date +"%Y-%m-%d %T"
#Before doing any other backup, dump into a .sql file all the mysql databases:
#mysqldump --all-databases --single-transaction --dump-date > /var/lib/mysql/backups/full-backup-$(date +\%F).sql
# Backup the most important directories into an archive named after
# the machine this script is currently running on:
# Create backup
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
--exclude-caches \
$borg_repo::'{hostname}-{now}' \
/boot \
/etc \
/home \
/opt \
/root \
/srv \
/usr/local \
/var/backups \
/var/lib/docker/volumes \
/var/local \
/var/log \
/var/mail \
/var/opt \
/var/spool \
/var/www \
--exclude '/home/*/.cache' \
--exclude '/root/.cache' \
--exclude '/var/*/cache' \
--exclude '/var/lib/ucf/cache' \
--exclude '/var/tmp' \
--exclude '/var/lock' \
--exclude '/var/run' \
backup_exit=$?
if [ "$?" -ne "0" ]; then
echo "borg create failed"
exit 2
fi
#if ! docker exec --user www-data nextcloud-fpm php occ maintenance:mode --off; then
#if ! sudo -u www-data php7.4 /srv/http/drive.federicociro.com/nextcloud/occ maintenance:mode --off; then
# echo "failed to disable Nextcloud maintenance mode"
# exit 1
#fi
backup_exit=$?
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-daily 7 \
--keep-weekly 4 \
--keep-monthly 6 \
$borg_repo
prune_exit=$?
echo "Backup sync completed."
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 0 ]; then
info "Backup and Prune finished successfully"
elif [ ${global_exit} -eq 1 ]; then
info "Backup and/or Prune finished with warnings"
else
info "Backup and/or Prune finished with errors"
fi
exit ${global_exit}
[Unit]
Description=Create backup using Borg backup
[Timer]
OnCalendar=*-*-* 00:00:00
AccuracySec=1h
Persistent=true
[Install]
WantedBy=timers.target
#!/bin/sh
# Setting this, so the repo does not need to be given on the commandline:
export BORG_REPO=/mnt/usb/backup_encrypt
export BORG_PASSPHRASE='passphrase'
if pidof -x rclone >/dev/null; then
echo "rclone is already running"
exit 1
fi
# Check backup for consistency before syncing to the cloud
borg check -v /mnt/usb/backup_encrypt
if [ "$?" -ne "0" ]; then
echo "borg check failed"
exit 2
fi
# Sync backup
rclone -v sync /mnt/usb/backup_encrypt backblaze:repo
if [ "$?" -ne "0" ]; then
echo "rclone sync failed"
exit 3
fi
echo "Backup sync completed."
echo ""
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 0 ]; then
info "Backup and Prune finished successfully"
elif [ ${global_exit} -eq 1 ]; then
info "Backup and/or Prune finished with warnings"
else
info "Backup and/or Prune finished with errors"
fi
exit ${global_exit}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment