Last active
May 15, 2023 23:33
-
-
Save AfroThundr3007730/dd55ac1ac67b5a9f5dc9a427fb4415fc to your computer and use it in GitHub Desktop.
Borg automatic backup script for user directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To allow the service to run with no active user session, use
loginctl enable-linger $USER