From 3a50a4a276e54505f715755ce27329782b92f5bd Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 17 Sep 2022 16:01:15 +0200 Subject: [PATCH] Add backup script and systemd service + timer to run it periodically --- dot_local/share/scripts/executable_backup.nu | 70 +++++++++++++++++++ .../systemd/user/backup.service | 6 ++ private_dot_config/systemd/user/backup.timer | 9 +++ 3 files changed, 85 insertions(+) create mode 100644 dot_local/share/scripts/executable_backup.nu create mode 100644 private_dot_config/systemd/user/backup.service create mode 100644 private_dot_config/systemd/user/backup.timer diff --git a/dot_local/share/scripts/executable_backup.nu b/dot_local/share/scripts/executable_backup.nu new file mode 100644 index 0000000..e1e58c9 --- /dev/null +++ b/dot_local/share/scripts/executable_backup.nu @@ -0,0 +1,70 @@ +#!/bin/nu +let-env BORG_REPO = '/run/media/trivernis/Backup' +let BACKUP_PATHS = [ + ~/Documents + ~/Videos + ~/Pictures/ + ~/Music + ~/.config + ~/.zshrc + ~/.local + /mnt/Data/Cloud/Nextcloud/ + /mnt/Data/Bilder/ + /mnt/Data/Dokumente/ + /mnt/Data/Filme/ + /mnt/Data/Audio/ + /mnt/Massdata/ + /etc +] + +if (pgrep borg | length) > 0 { + echo "Borg is running" + exit +} + +borg break-lock +echo "Creating archive" +(borg create + --progress + --warning + --filter AME + --compression zstd + --exclude-caches + --exclude '*/.cache' + --exclude '*/Rust/*/target' + --exclude '*/target/debug' + --exclude '*/target/release' + --exclude '*/node_modules' + --exclude '*/massdata.tar.gz' + --exclude '*/dynmap/web/tiles' + --exclude '*/work/*decompile' + --exclude '*/PortableGit*' + --exclude '*.pyc' + --exclude '*/Java/*/build' + --exclude '*/Java/*/out/artifacts' + --exclude '*/Java/*/target' + --exclude '*/Kotlin/*/target' + --exclude '*/Kotlin/*/out' + --exclude '*/Kotlin/*/build' + --exclude '*/.local/share/Steam' + --list + --stats + '::{hostname}-{now}' + $BACKUP_PATHS +) +echo "Deleting old files" +(borg prune + --progress + --list + --glob-archives '{hostname}-*' + --show-rc + --keep-daily 2 + --keep-weekly 2 + --keep-monthly 1 +) +echo "Running check for five minutes" +(borg check + --progress + --repository-only + --max-duration 300 +) \ No newline at end of file diff --git a/private_dot_config/systemd/user/backup.service b/private_dot_config/systemd/user/backup.service new file mode 100644 index 0000000..48f4f34 --- /dev/null +++ b/private_dot_config/systemd/user/backup.service @@ -0,0 +1,6 @@ +[Unit] +Description=Does a backup + +[Service] +ExecStart=nu %h/.local/share/scripts/backup.nu + diff --git a/private_dot_config/systemd/user/backup.timer b/private_dot_config/systemd/user/backup.timer new file mode 100644 index 0000000..c01ed9d --- /dev/null +++ b/private_dot_config/systemd/user/backup.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run daily backups + +[Timer] +OnCalendar=daily +Persistent=true + +[Install] +WantedBy=timers.target