Skip to content

Instantly share code, notes, and snippets.

@AfroThundr3007730
Last active May 15, 2023 23:33
Show Gist options
  • Save AfroThundr3007730/dd55ac1ac67b5a9f5dc9a427fb4415fc to your computer and use it in GitHub Desktop.
Save AfroThundr3007730/dd55ac1ac67b5a9f5dc9a427fb4415fc to your computer and use it in GitHub Desktop.
Borg automatic backup script for user directory.
# Goes in $HOME/.config/systemd/user/borg-auto.service
[Unit]
Description=Borg backup service
[Service]
Type=simple
Restart=no
ExecStart=%h/.local/bin/borg-auto
# Goes in $HOME/.config/systemd/user/borg-auto.timer
[Unit]
Description=Borg backup service
Wants=network.target
[Timer]
OnCalendar=daily
Persistent=1
[Install]
WantedBy=default.target
#!/bin/bash
# Borg automatic backup
# Set necessary variables
export BORG_REPO='ssh://[email protected]/path/to/repo'
export BORG_PASSCOMMAND='cat /home/USER/.config/borg/borg-pass'
BORG_LOG='/home/USER/.local/logs/borg.log'
# The readlink call is only needed because of the symlink mess in my user dir
mapfile -t BORG_DIRS <<< "$(
readlink -f ~ ~/{Desktop,Documents,Downloads,Pictures,Videos,Music,Games}
)"
# Do the daily backup
printf '%s: Starting the daily backup.\n' "$(date -u +%FT%TZ)" |
tee -a $BORG_LOG
borg create -vspC lzma -e '/home/USER/.cache' -e '/home/USER/.tmp' \
::'{hostname}_{now:%Y-%m-%d}' "${BORG_DIRS[@]}"
printf '%s: Pruning old backup archives.\n' "$(date -u +%FT%TZ)" |
tee -a $BORG_LOG
borg prune -v --list -d 7 -w 4 -m 12 -y 5 -a '{hostname}_*' ::
printf '%s: Daily backup complete.\n' "$(date -u +%FT%TZ)" |
tee -a $BORG_LOG
@AfroThundr3007730
Copy link
Author

To allow the service to run with no active user session, use loginctl enable-linger $USER

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment