Skip to content

Instantly share code, notes, and snippets.

@bigntallmike
Created October 5, 2026 17:42
Show Gist options
  • Select an option

  • Save bigntallmike/c46032e742c4a29ee9d78f64e1f5a8c5 to your computer and use it in GitHub Desktop.

Select an option

Save bigntallmike/c46032e742c4a29ee9d78f64e1f5a8c5 to your computer and use it in GitHub Desktop.
borg backup script
#!/bin/sh
# Quick and easy borg backup for personal use
# Consider it MIT or Apache licensed as necessary.
if [ "x$1" = "x" ]; then
echo "Specify a folder to back up on the command-line."
exit 1
fi
if [ -f $HOME/.config/netborgbackup ]; then
. $HOME/.config/netborgbackup
else
. /etc/sysconfig/netborgbackup
fi
export BORG_PASSPHRASE
OPTIONS="--stats"
if tty -s; then
OPTIONS="$OPTIONS --verbose --progress"
fi
EXCLUDES="/etc/sysconfig/backup_exclude.lst"
if [ -f $HOME/.config/backup_exclude.lst ]; then
EXCLUDES="$HOME/.config/backup_exclude.lst"
fi
REPO_URI="$BORG_USER@$BACKUP_SERVER:/backups/$BORG_REPO"
# Use current date timestamp as backup name
borg create $OPTIONS \
--one-file-system \
--exclude-from="$EXCLUDES" \
$REPO_URI::$(date +%Y%m%d%H%M%S) "$@"
borgret=$?
if [ $borgret -ne 0 ]; then
echo "Backup failed: $borgret"
exit $borgret
fi
KEEP_OPTIONS=""
if [ ! "X$KEEP_DAILY" = "x" ]; then
KEEP_OPTIONS="$KEEP_OPTIONS --keep-daily=$KEEP_DAILY"
fi
if [ ! "x$KEEP_WEEKLY" = "x" ]; then
KEEP_OPTIONS="$KEEP_OPTIONS --keep-weekly=$KEEP_WEEKLY"
fi
if [ ! "x$KEEP_MONTHLY" = "x" ]; then
KEEP_OPTIONS="$KEEP_OPTIONS --keep-monthly=$KEEP_MONTHLY"
fi
if [ ! "x$KEEP_YEARLY" = "x" ]; then
KEEP_OPTIONS="$KEEP_OPTIONS --keep-yearly=$KEEP_YEARLY"
fi
if [ "x$KEEP_OPTIONS" = "x" ]; then
# No keep options, no prune
exit 0
fi
borg prune $OPTIONS \
$KEEP_OPTIONS $REPO_URI
# To automate, create borgbackup.timer:
# [Unit]
# Description=Run borg backup every night
# [Timer]
# OnCalendar=*-*-* 02:30:00
# [Install]
# WantedBy=multi-user.target
#
# And borgbackup.service:
# [Unit]
# Description=borg backup service
# [Service]
# Type=oneshot
# ExecStart=/location/to/borgbackup.sh
# These can be in your user systemd folder or /etc/systemd/system/ depending.
# Test by `systemctl start borgbackup` ... then `systemctl enable borgbackup.timer`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment