Skip to content

Instantly share code, notes, and snippets.

@dquintela
Last active January 12, 2023 00:38
Show Gist options
  • Save dquintela/09ee5fd14d2224bab09c9cc4b8b7d66e to your computer and use it in GitHub Desktop.
Save dquintela/09ee5fd14d2224bab09c9cc4b8b7d66e to your computer and use it in GitHub Desktop.
Restic Backups with user account on OMV
- Based on https://fedoramagazine.org/automate-backups-with-restic-and-systemd/
https://restic.readthedocs.io/
- Install restic
$ sudo apt-get install bzip2
$ mkdir restic && \
wget -q -O - https://github.com/restic/restic/releases/download/v0.9.5/restic_0.9.5_linux_amd64.bz2 | \
bunzip2 > restic/restic \
&& chmod +x restic/restic
$ export PATH=~/restic:$PATH
- Initialize repository
$ export B2_ACCOUNT_ID=<my_account_id>
$ export B2_ACCOUNT_KEY=<my_account_key>
[NOTE] Don't do this if there is already a repository there
$ restic -r b2:restic-bucket:/ init
- List current repository snapshots and stats
$ export RESTIC_PASSWORD=<repository_password_from_previous_init>
$ restic -vv -r b2:restic-bucket:/ snapshots
repository f4bce1b4 opened successfully, password is correct
ID Time Host Tags Paths
------------------------------------------------------------------------------------------------
f68ae4af 2019-05-06 01:17:51 openmediavault /sharedfolders/Sync/Files_First
86e7c2da 2019-05-06 13:59:38 openmediavault /sharedfolders/Sync/Files_First
28f16c7e 2019-05-07 23:00:27 openmediavault systemd.timer /sharedfolders/Sync/Files_First
d687b26f 2019-05-08 23:00:27 openmediavault systemd.timer /sharedfolders/Sync/Files_First
....
b56b1d3d 2019-09-11 16:00:49 openmediavault systemd.timer /sharedfolders/Sync/Files_First
/sharedfolders/Sync/Spring IO 2019
/sharedfolders/Sync/Work_Documents
------------------------------------------------------------------------------------------------
259 snapshots
$ restic -vv -r b2:restic-bucket:/ stats
repository f4bce1b4 opened successfully, password is correct
created new cache in /home/dquintela/.cache/restic
scanning...
Stats for all snapshots in restore-size mode:
Total File Count: 1247008
Total Size: 4.859 TiB
- Perform a backup
$ restic -vv -r b2:restic-bucket:/ backup "/sharedfolders/Sync/Files_First" "/sharedfolders/Sync/Work_Documents" "/sharedfolders/Sync/Spring IO 2019"
...
Files: 5439 new, 0 changed, 0 unmodified
Dirs: 2 new, 0 changed, 0 unmodified
Data Blobs: 1 new
Tree Blobs: 3 new
Added to the repo: 7.021 KiB
processed 5439 files, 24.096 GiB in 6:04
snapshot 3969c9d3 saved
- Automate: Create systemd config files
$ cat .config/restic-backup.conf
BACKUP_PATHS="/sharedfolders/Sync/Files_First /sharedfolders/Sync/Work_Documents /sharedfolders/Sync/Spring\\ IO\\ 2019"
BACKUP_EXCLUDES="--exclude-file /home/dquintela/.restic_excludes --exclude-if-present .exclude_from_backup"
RETENTION_HOURS=72
RETENTION_DAYS=15
RETENTION_WEEKS=8
RETENTION_MONTHS=6
RETENTION_YEARS=2
B2_ACCOUNT_ID=<my_account_id>
B2_ACCOUNT_KEY=<my_account_key>
RESTIC_REPOSITORY=b2:restic-bucket:/
RESTIC_PASSWORD=<repository_password_from_previous_init>
$ cat .config/systemd/user/restic-backup.service
[Unit]
Description=Restic backup service
[Service]
Type=oneshot
ExecStart=%h/restic/restic backup --verbose --one-file-system --tag systemd.timer $BACKUP_EXCLUDES $BACKUP_PATHS
ExecStartPost=%h/restic/restic forget --verbose --tag systemd.timer --group-by "paths,tags" --keep-hourly $RETENTION_HOURS --keep-daily $RETENTION_DAYS --keep-weekly $RETENTION_WEEKS --keep-monthly $RETENTION_MONTHS --keep-yearly $RETENTION_YEARS
EnvironmentFile=%h/.config/restic-backup.conf
$ cat .config/systemd/user/restic-backup.timer
[Unit]
Description=Backup with restic hourly
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.target
$ cat .config/systemd/user/restic-prune.service
[Unit]
Description=Restic backup service (data pruning)
[Service]
Type=oneshot
ExecStart=%h/restic/restic prune --verbose --verbose
EnvironmentFile=%h/.config/restic-backup.conf
$ cat .config/systemd/user/restic-prune.timer
[Unit]
Description=Prune data from the restic repository monthly
[Timer]
OnCalendar=monthly
RandomizedDelaySec=15m
Persistent=true
[Install]
WantedBy=timers.target
$ touch /home/dquintela/.restic_excludes
- List user units / installs user services
$ systemctl --user daemon-reload
Failed to connect to bus: No such file or directory
$ sudo apt-get install dbus-user-session
$ sudo reboot
$ systemctl --user daemon-reload
$ systemctl --user status restic-prune.timer
● restic-prune.timer - Prune data from the restic repository monthly
Loaded: loaded (/home/dquintela/.config/systemd/user/restic-prune.timer; disabled; vendor preset: enabled)
Active: inactive (dead)
$ systemctl --user status restic-backup.service
● restic-backup.service - Restic backup service
Loaded: loaded (/home/dquintela/.config/systemd/user/restic-backup.service; static; vendor preset: enabled)
Active: inactive (dead)
$ systemctl --user status restic-prune.service
● restic-prune.service - Restic backup service (data pruning)
Loaded: loaded (/home/dquintela/.config/systemd/user/restic-prune.service; static; vendor preset: enabled)
Active: inactive (dead)
# Test services
$ systemctl --user start restic-backup.service
$ systemctl --user start restic-prune.service
# Activate timers for services
$ systemctl --user list-timers
0 timers listed.
$ systemctl --user enable --now restic-backup.timer
Created symlink /home/dquintela/.config/systemd/user/timers.target.wants/restic-backup.timer → /home/dquintela/.config/systemd/user/restic-backup.timer.
$ systemctl --user enable --now restic-prune.timer
Created symlink /home/dquintela/.config/systemd/user/timers.target.wants/restic-prune.timer → /home/dquintela/.config/systemd/user/restic-prune.timer.
$ systemctl --user list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
Tue 2019-09-17 02:00:00 WEST 51min left n/a n/a restic-backup.timer restic-backup.service
Tue 2019-10-01 00:03:26 WEST 1 weeks 6 days left n/a n/a restic-prune.timer restic-prune.service
2 timers listed.
- Enable systemd user lingering (service keep running despite user logoff [start at boot]
# loginctl enable-linger dquintela
# ls -la /var/lib/systemd/linger/dquintela
-rw-r--r-- 1 root root 0 Sep 22 11:49 /var/lib/systemd/linger/dquintela
- Log service status and logs
$ journalctl --user-unit restic-backup.service
Hint: You are currently not seeing messages from other users and the system.
Users in the 'systemd-journal' group can see all messages. Pass -q to
turn off this notice.
No journal files were opened due to insufficient permissions.
Add "dquintela" user to group 'systemd-journal' though web-interface or command-line [logout and re-login]
$ sudo usermod -a -G systemd-journal dquintela
$ journalctl --user-unit restic-backup.service
$ journalctl -n 250 -f --user-unit restic-prune.service --user-unit restic-backup.service
@dquintela
Copy link
Author

Enable user lingering;

After session logout, service fails to keep running..

https://wiki.archlinux.org/index.php/Systemd/User#Automatic_start-up_of_systemd_user_instances

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