2023-12-13 14:58:10 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
ifExists = groups: builtins.filter (group: builtins.hasAttr group config.users.groups) groups;
|
2023-12-15 13:27:38 +01:00
|
|
|
rsync-backups = pkgs.writeScriptBin "rsync-backups" ''
|
2023-12-13 14:58:10 +01:00
|
|
|
#!${pkgs.stdenv.shell}
|
|
|
|
# Send the gotify notification:
|
2023-12-14 03:50:23 +01:00
|
|
|
|
2023-12-15 13:17:46 +01:00
|
|
|
GOTIFY_TOKEN=`cat /var/run/secrets/gotify_token | head -n 1`
|
2023-12-14 03:50:23 +01:00
|
|
|
payload="{\"message\": \"Backup has started\nDate: `date`\", \"priority\": 2,\"title\": \"backups-rpi4: Backup Started\"}"
|
2023-12-15 13:38:01 +01:00
|
|
|
curl -X "POST" "https://gotify.sysctl.io/message" -H "accept: application/json" -H "Content-Type: application/json" -H "X-Gotify-Key: $GOTIFY_TOKEN" -d "$payload"
|
2023-12-13 14:58:10 +01:00
|
|
|
|
2023-12-16 09:33:07 +01:00
|
|
|
drive1=`lsblk -o NAME,SERIAL | grep -v luks | grep 9RGHYP8C | awk {'print $1'}`
|
|
|
|
drive2=`lsblk -o NAME,SERIAL | grep -v luks | grep 9RGXW93C | awk {'print $1'}`
|
|
|
|
drive3=`lsblk -o NAME,SERIAL | grep -v luks | grep 5QH05G3F | awk {'print $1'}`
|
|
|
|
|
|
|
|
mount1=`mount -l | grep $drive1`
|
|
|
|
mounted1=`echo $?`
|
|
|
|
mount2=`mount -l | grep $drive2`
|
|
|
|
mounted2=`echo $?`
|
|
|
|
mount3=`mount -l | grep $drive3`
|
|
|
|
mounted3=`echo $?`
|
|
|
|
|
|
|
|
echo "status"
|
|
|
|
echo "$drive1 / $mount1 / $mounted1"
|
|
|
|
echo "$drive2 / $mount2 / $mounted2"
|
|
|
|
echo "$drive3 / $mount3 / $mounted3"
|
|
|
|
|
|
|
|
# If any drive isn't found, exit:
|
|
|
|
if [[ $mounted1 != 0 ]] && [[ munted2 != 0 ]] && [[ $mounted3 != 0 ]] ; then
|
|
|
|
payload="{\"message\": \"Backup stopped. One or more local drives are not mounted.\nDate: `date`\", \"priority\": 2,\"title\": \"backups-rpi4: Backup Stopped\"}"
|
|
|
|
curl -X "POST" "https://gotify.sysctl.io/message" -H "accept: application/json" -H "Content-Type: application/json" -H "X-Gotify-Key: $GOTIFY_TOKEN" -d "$payload"
|
|
|
|
|
|
|
|
fi
|
2023-12-13 14:58:10 +01:00
|
|
|
|
|
|
|
echo "Drive 1: $drive1"
|
|
|
|
echo "Drive 2: $drive2"
|
|
|
|
echo "Drive 3: $drive3"
|
|
|
|
|
|
|
|
echo "Checking if the NAS is mounted on nuc-docker01:"
|
|
|
|
check=$(ssh root@nuc-docker01 df -h | grep Storage | wc -l)
|
|
|
|
if [[ $check != 3 ]] ; then
|
2023-12-14 03:50:23 +01:00
|
|
|
payload="{\"message\": \"Backup stopped. nuc-docker01 does not have 3 mounts in Storage\nDate: `date`\", \"priority\": 2,\"title\": \"backups-rpi4: Backup Stopped\"}"
|
2023-12-15 13:38:01 +01:00
|
|
|
curl -X "POST" "https://gotify.sysctl.io/message" -H "accept: application/json" -H "Content-Type: application/json" -H "X-Gotify-Key: $GOTIFY_TOKEN" -d "$payload"
|
2023-12-13 14:58:10 +01:00
|
|
|
exit
|
|
|
|
fi
|
2023-12-14 04:02:00 +01:00
|
|
|
|
2023-12-14 05:13:19 +01:00
|
|
|
echo -e "======= Time started : `date`"
|
2023-12-13 14:58:10 +01:00
|
|
|
|
2023-12-15 13:27:38 +01:00
|
|
|
if [[ `ps aux | grep rsync | grep -v grep | grep -v rsync-backups` ]] ; then
|
2023-12-14 05:13:19 +01:00
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "rsync still running. Exiting...."
|
|
|
|
echo -e "======= Time complete: `date`"
|
2023-12-13 14:58:10 +01:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2023-12-14 05:13:19 +01:00
|
|
|
echo -e "rsync -avr --delete --exclude=luks.img /Storage/Backups/Manual/Computers/ /mnt/$drive1/Backups/Manual/Computers/"
|
|
|
|
rsync -avr --delete --exclude=luks.img root@nuc-docker01:/Storage/Backups/Manual/Computers/ /mnt/$drive1/Backups/Manual/Computers/
|
|
|
|
|
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete /Storage/Backups/Backblaze/ /mnt/$drive1/Backups/Backblaze/"
|
|
|
|
rsync -avr --delete root@nuc-docker01:/Storage/Backups/Backblaze/ /mnt/$drive1/Backups/Backblaze/
|
|
|
|
|
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete /Storage/Backups/Logs/ /mnt/$drive1/Backups/Backblaze/"
|
|
|
|
rsync -avr --delete root@nuc-docker01:/Storage/Backups/Logs/ /mnt/$drive1/Backups/Logs/
|
|
|
|
|
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete /Storage/Backups/Docker/ /mnt/$drive1/Backups/Docker/"
|
|
|
|
rsync -avr --delete root@nuc-docker01:/Storage/Backups/Docker/ /mnt/$drive1/Backups/Docker/
|
|
|
|
|
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete /Storage/Video/Pictures/ /mnt/$drive1/Pictures/"
|
|
|
|
rsync -avr --delete root@nuc-docker01:/Storage/Video/Pictures/ /mnt/$drive1/Pictures/
|
|
|
|
|
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete /Storage/Video/Books/ /mnt/$drive1/Books/"
|
|
|
|
rsync -avr --delete root@nuc-docker01:/Storage/Video/Books/ /mnt/$drive1/Books/
|
|
|
|
|
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete /Storage/Video/Music/ /mnt/$drive1/Music/"
|
|
|
|
rsync -avr --delete root@nuc-docker01:/Storage/Video/Music/ /mnt/$drive1/Music/
|
|
|
|
|
2023-12-25 12:14:00 +01:00
|
|
|
# echo -e "================================================================================"
|
|
|
|
# echo -e "======= Time: `date`"
|
|
|
|
# echo -e "rsync -avr --delete --exclude=Downloads/ /Storage/Data/ /mnt/$drive1/Data/"
|
|
|
|
# rsync -avr --delete --exclude=Downloads/ root@nuc-docker01:/Storage/Data/ /mnt/$drive1/Data/
|
2023-12-14 05:13:19 +01:00
|
|
|
|
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete /Storage/Video/Movies/ /mnt/$drive2/Movies/"
|
|
|
|
rsync -avr --delete root@nuc-docker01:/Storage/Video/Movies/ /mnt/$drive2/Movies/
|
|
|
|
|
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete /Storage/Video/TV_Shows/[0-9]* /mnt/$drive1/TV_Shows/"
|
|
|
|
rsync -avr --delete root@nuc-docker01:/Storage/Video/TV_Shows/[0-9]* /mnt/$drive1/TV_Shows/
|
2023-12-13 14:58:10 +01:00
|
|
|
|
2023-12-14 05:13:19 +01:00
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete /Storage/Video/TV_Shows/[A-K]* /mnt/$drive1/TV_Shows/"
|
|
|
|
rsync -avr --delete root@nuc-docker01:/Storage/Video/TV_Shows/[A-E]* /mnt/$drive1/TV_Shows/
|
2023-12-13 14:58:10 +01:00
|
|
|
|
2023-12-14 05:13:19 +01:00
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete /Storage/Video/TV_Shows/[L-M]* /mnt/$drive2/TV_Shows/"
|
|
|
|
rsync -avr --delete root@nuc-docker01:/Storage/Video/TV_Shows/[F-M]* /mnt/$drive2/TV_Shows/
|
2023-12-13 14:58:10 +01:00
|
|
|
|
2023-12-14 05:13:19 +01:00
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete /Storage/Video/TV_Shows/[N-Z]* /mnt/$drive3/TV_Shows/"
|
|
|
|
rsync -avr --delete root@nuc-docker01:/Storage/Video/TV_Shows/[N-Z]* /mnt/$drive3/TV_Shows/
|
2023-12-13 14:58:10 +01:00
|
|
|
|
2023-12-14 05:13:19 +01:00
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete /Storage/Video/YouTube /mnt/$drive3/YouTube/"
|
|
|
|
rsync -avr --delete root@nuc-docker01:/Storage/Video/YouTube/ /mnt/$drive3/YouTube/
|
2023-12-13 14:58:10 +01:00
|
|
|
|
2023-12-14 05:13:19 +01:00
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "======= Time complete: `date`"
|
2023-12-13 14:58:10 +01:00
|
|
|
|
2023-12-14 05:13:19 +01:00
|
|
|
echo "Sending completion message"
|
|
|
|
payload="{\"message\": \"Backup completed\nDate: `date`\n`df -h | grep sda`\n`df -h | grep sdb`\n`df -h | grep sdc`\", \"priority\": 2,\"title\": \"backups-rpi4: Backup Complete\"}"
|
2023-12-15 13:38:01 +01:00
|
|
|
curl -X "POST" "https://gotify.sysctl.io/message" -H "accept: application/json" -H "Content-Type: application/json" -H "X-Gotify-Key: $GOTIFY_TOKEN" -d "$payload"
|
2023-12-13 14:58:10 +01:00
|
|
|
|
|
|
|
'';
|
2023-12-25 12:14:00 +01:00
|
|
|
rsync-data-vol = pkgs.writeScriptBin "rsync-data-vol" ''
|
|
|
|
#!${pkgs.stdenv.shell}
|
|
|
|
# Send the gotify notification:
|
|
|
|
|
|
|
|
GOTIFY_TOKEN=`cat /var/run/secrets/gotify_token | head -n 1`
|
|
|
|
payload="{\"message\": \"Data Vol Backup has started\nDate: `date`\", \"priority\": 2,\"title\": \"backups-rpi4: Data Vol Backup Started\"}"
|
|
|
|
curl -X "POST" "https://gotify.sysctl.io/message" -H "accept: application/json" -H "Content-Type: application/json" -H "X-Gotify-Key: $GOTIFY_TOKEN" -d "$payload"
|
|
|
|
|
|
|
|
drive1=`lsblk -o NAME,SERIAL | grep -v luks | grep 9RGHYP8C | awk {'print $1'}`
|
|
|
|
drive2=`lsblk -o NAME,SERIAL | grep -v luks | grep 9RGXW93C | awk {'print $1'}`
|
|
|
|
drive3=`lsblk -o NAME,SERIAL | grep -v luks | grep 5QH05G3F | awk {'print $1'}`
|
|
|
|
|
|
|
|
mount1=`mount -l | grep $drive1`
|
|
|
|
mounted1=`echo $?`
|
|
|
|
mount2=`mount -l | grep $drive2`
|
|
|
|
mounted2=`echo $?`
|
|
|
|
mount3=`mount -l | grep $drive3`
|
|
|
|
mounted3=`echo $?`
|
|
|
|
|
|
|
|
echo "status"
|
|
|
|
echo "$drive1 / $mount1 / $mounted1"
|
|
|
|
echo "$drive2 / $mount2 / $mounted2"
|
|
|
|
echo "$drive3 / $mount3 / $mounted3"
|
|
|
|
|
|
|
|
# If any drive isn't found, exit:
|
|
|
|
if [[ $mounted1 != 0 ]] && [[ munted2 != 0 ]] && [[ $mounted3 != 0 ]] ; then
|
|
|
|
payload="{\"message\": \"Data Vol Backup stopped. One or more local drives are not mounted.\nDate: `date`\", \"priority\": 2,\"title\": \"backups-rpi4: Data Vol Backup Stopped\"}"
|
|
|
|
curl -X "POST" "https://gotify.sysctl.io/message" -H "accept: application/json" -H "Content-Type: application/json" -H "X-Gotify-Key: $GOTIFY_TOKEN" -d "$payload"
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Drive 1: $drive1"
|
|
|
|
echo "Drive 2: $drive2"
|
|
|
|
echo "Drive 3: $drive3"
|
|
|
|
|
|
|
|
echo "Checking if the NAS is mounted on nuc-docker01:"
|
|
|
|
check=$(ssh root@nuc-docker01 df -h | grep Storage | wc -l)
|
|
|
|
if [[ $check != 3 ]] ; then
|
|
|
|
payload="{\"message\": \"Data Vol Backup stopped. nuc-docker01 does not have 3 mounts in Storage\nDate: `date`\", \"priority\": 2,\"title\": \"backups-rpi4: Data Vol Backup Stopped\"}"
|
|
|
|
curl -X "POST" "https://gotify.sysctl.io/message" -H "accept: application/json" -H "Content-Type: application/json" -H "X-Gotify-Key: $GOTIFY_TOKEN" -d "$payload"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo -e "======= Time started : `date`"
|
|
|
|
|
|
|
|
if [[ `ps aux | grep rsync | grep -v grep | grep -v rsync-data-vol` ]] ; then
|
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "rsync still running. Exiting...."
|
|
|
|
echo -e "======= Time complete: `date`"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo -e "================================================================================"
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "rsync -avr --delete --exclude=Downloads/ /Storage/Data/ /mnt/$drive1/Data/"
|
|
|
|
rsync -avr --delete --exclude=Downloads/ root@nuc-docker01:/Storage/Data/ /mnt/$drive1/Data/
|
|
|
|
|
|
|
|
echo -e "======= Time: `date`"
|
|
|
|
echo -e "======= Time complete: `date`"
|
|
|
|
|
|
|
|
echo "Sending completion message"
|
|
|
|
payload="{\"message\": \"Data Vol Backup completed\nDate: `date`\n`df -h | grep sda`\n`df -h | grep sdb`\n`df -h | grep sdc`\", \"priority\": 2,\"title\": \"backups-rpi4: Data Vol Backup Complete\"}"
|
|
|
|
curl -X "POST" "https://gotify.sysctl.io/message" -H "accept: application/json" -H "Content-Type: application/json" -H "X-Gotify-Key: $GOTIFY_TOKEN" -d "$payload"
|
|
|
|
|
|
|
|
'';
|
|
|
|
'';
|
2023-12-13 14:58:10 +01:00
|
|
|
in
|
|
|
|
{
|
2023-12-25 12:14:00 +01:00
|
|
|
environment.systemPackages = [
|
|
|
|
rsync-backups
|
|
|
|
rsync-data-vol
|
|
|
|
];
|
2023-12-14 03:50:23 +01:00
|
|
|
|
|
|
|
# Set up the secret for the password:
|
|
|
|
sops.secrets."gotify_token" = {
|
|
|
|
owner = "root";
|
|
|
|
sopsFile = ../../../secrets/backups-rpi4.yaml;
|
|
|
|
};
|
2023-12-13 14:58:10 +01:00
|
|
|
}
|