commit 66cb8d4491ab07174ab336112622ee7a75301499
parent 2ec3b00777bc2307f1fd724877d0468958b82f7c
Author: Leah (ctucx) <git@ctu.cx>
Date: Mon, 17 Oct 2022 14:02:11 +0200
parent 2ec3b00777bc2307f1fd724877d0468958b82f7c
Author: Leah (ctucx) <git@ctu.cx>
Date: Mon, 17 Oct 2022 14:02:11 +0200
configurations/common/programs/scripts: add `backup-lollo` script
1 file changed, 122 insertions(+), 1 deletion(-)
M
|
123
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
diff --git a/configurations/common/programs/scripts.nix b/configurations/common/programs/scripts.nix @@ -16,8 +16,129 @@ let nix-store --optimise ''; + backup-lollo = pkgs.writeShellScriptBin "backup-lollo" '' + set -euo pipefail + IFS=$'\n\t' + + function repeatCharacter { + local count="$1"; + local character="$2"; + + for (( i = 0; i < "$count"; ++i )) + do + echo -n "$character"; + done + echo ""; + } + + function printEnd { + echo ""; + } + + function printStep { + repeatCharacter $(expr ''${#1} + 3) "#"; + echo -e "$1"; + repeatCharacter $(expr ''${#1} + 3) "#"; + } + + function checkRequirements { + # check if root + if [ "$USER" != "root" ]; then + echo "This script needs to be executed by root!"; + exit; + fi + + # check for disk + if [ ! -e /dev/disk/by-uuid/635a0d41-b702-45d6-8aff-605d11da343c ]; then + echo "Backup drive is not connected!"; + exit; + fi + } + + function mountDrive { + printStep "Mount backup-drive!" + + mkdir -p /mnt/backup; + ${pkgs.cryptsetup}/bin/cryptsetup luksOpen /dev/disk/by-uuid/635a0d41-b702-45d6-8aff-605d11da343c backup; + mount /dev/mapper/backup /mnt/backup; + + printEnd; + } + + function unmountDrive { + sync; + if grep -qs '/dev/mapper/backup ' /proc/mounts; then + printStep "Unmount backup-drive!"; + umount /dev/mapper/backup; + ${pkgs.cryptsetup}/bin/cryptsetup luksClose backup; + sync; + fi + echo "done!"; + } + + function copyResticRepos { + printStep "Copying /var/lib/restic to the backup-drive"; + + ${pkgs.rsync}/bin/rsync -ah --partial --delete --info=progress2 /var/lib/restic/ /mnt/backup/restic-servers/; + sync; + + printEnd; + } + + function resticBackupFolder { + printStep "Backing up '$2' to the backup-drive"; + + mkdir -p /mnt/backup/restic-$HOSTNAME; + + # check if password-file exists, if not create it + if [ ! -e /mnt/backup/restic-$HOSTNAME/$1.restic-passwd ]; then + echo -e "$(pwgen -N1 -B 32)" > /mnt/backup/restic-$HOSTNAME/$1.restic-passwd; + sync; + fi + + # check if restic repo is initialized, if not initialize it + if ! ${pkgs.restic}/bin/restic --cleanup-cache --password-file /mnt/backup/restic-$HOSTNAME/$1.restic-passwd --repo /mnt/backup/restic-$HOSTNAME/$1 snapshots &>/dev/null; then + ${pkgs.restic}/bin/restic --quiet --cleanup-cache --password-file /mnt/backup/restic-$HOSTNAME/$1.restic-passwd --repo /mnt/backup/restic-$HOSTNAME/$1 \ + init; + sync; + fi; + + #do the backup + ${pkgs.restic}/bin/restic --cleanup-cache --password-file /mnt/backup/restic-$HOSTNAME/$1.restic-passwd --repo /mnt/backup/restic-$HOSTNAME/$1 \ + backup $2; + sync; + + #cleanup + ${pkgs.restic}/bin/restic --cleanup-cache --password-file /mnt/backup/restic-$HOSTNAME/$1.restic-passwd --repo /mnt/backup/restic-$HOSTNAME/$1 \ + forget --keep-within-daily 14d --keep-within-weekly 2m --keep-within-monthly 2y --keep-within-yearly 99y; + ${pkgs.restic}/bin/restic --cleanup-cache --password-file /mnt/backup/restic-$HOSTNAME/$1.restic-passwd --repo /mnt/backup/restic-$HOSTNAME/$1 \ + prune; + sync; + + printEnd; + } + + trap unmountDrive EXIT; + checkRequirements; + mountDrive; + + copyResticRepos; + + #restic backups + resticBackupFolder audiobooks /home/leah/syncthing/Audiobooks; + resticBackupFolder bahn-richtlinien /home/leah/syncthing/Bahn-Richtlinien; + resticBackupFolder cutieshare /home/leah/syncthing/Cutieshare; + resticBackupFolder documents /home/leah/syncthing/Documents; + resticBackupFolder media-legacy /home/leah/syncthing/Media\ \(legacy\); + resticBackupFolder music /home/leah/syncthing/Music; + resticBackupFolder music-originals /home/leah/syncthing/Music\ \(Originals\); + resticBackupFolder pictures /home/leah/syncthing/Pictures; + resticBackupFolder videos /home/leah/syncthing/Videos; + resticBackupFolder wiki /home/leah/syncthing/Wiki; + ''; + in { - home-manager.users.leah.home.packages = [ nix-cleanup ]; + home-manager.users.leah.home.packages = [ nix-cleanup backup-lollo ]; }