Add rsync-pi script

This commit is contained in:
iFargle 2023-12-13 22:58:10 +09:00
parent c78653d7c1
commit c094d3c9b5
2 changed files with 134 additions and 0 deletions

View file

@ -0,0 +1,132 @@
{ config, lib, pkgs, ... }:
let
ifExists = groups: builtins.filter (group: builtins.hasAttr group config.users.groups) groups;
rsync-backup = pkgs.writeScriptBin "rsync-backup" ''
#!${pkgs.stdenv.shell}
# Send the gotify notification:
payload="{\"message\": \"Backup has started\nDate: `date`\", \"priority\": 2,\"title\": \"japan-rpi4: Backup Started\"}"
curl -X 'POST' 'https://gotify.sysctl.io/message' -H 'accept: application/json' -H 'Content-Type: application/json' -H 'X-Gotify-Key: ${sops.secrets.gotify-key}' -d "$payload" | jq
drive1=`lsblk -o NAME,SERIAL | grep -v crypt | grep 9RGHYP8C | awk {'print $1'}`
drive2=`lsblk -o NAME,SERIAL | grep -v crypt | grep 9RGXW93C | awk {'print $1'}`
drive3=`lsblk -o NAME,SERIAL | grep -v crypt | grep 5QH05G3F | awk {'print $1'}`
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\": \"Backup stopped. nuc-docker01 does not have 3 mounts in Storage\nDate: `date`\", \"priority\": 2,\"title\": \"japan-rpi4: Backup Stopped\"}"
curl -X 'POST' 'https://gotify.sysctl.io/message' -H 'accept: application/json' -H 'Content-Type: application/json' -H 'X-Gotify-Key: ${sops.secrets.gotify-key}' -d "$payload" | jq
exit
fi
RESTORE='\033[0m'
RED='\033[00;31m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
PURPLE='\033[00;35m'
CYAN='\033[00;36m'
LIGHTGRAY='\033[00;37m'
LRED='\033[01;31m'
LGREEN='\033[01;32m'
LYELLOW='\033[01;33m'
LBLUE='\033[01;34m'
LPURPLE='\033[01;35m'
LCYAN='\033[01;36m'
WHITE='\033[01;37m'
/bin/echo -e "${CYAN}======= Time started : `date`${RESTORE}"
if [[ `ps aux | grep rsync | grep -v grep | grep -v rsync-all` ]] ; then
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}rsync still running. Exiting....${RESTORE}"
/bin/echo -e "${CYAN}======= Time complete: `date`${RESTORE}"
exit
fi
/bin/echo -e "${LGREEN}rsync -avr --delete --exclude=luks.img /Storage/Backups/Manual/Computers/ /mnt/$drive1/Backups/Manual/Computers/${RESTORE}"
/usr/bin/rsync -avr --delete --exclude=luks.img root@nuc-docker01:/Storage/Backups/Manual/Computers/ /mnt/$drive1/Backups/Manual/Computers/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete /Storage/Backups/Backblaze/ /mnt/$drive1/Backups/Backblaze/${RESTORE}"
/usr/bin/rsync -avr --delete root@nuc-docker01:/Storage/Backups/Backblaze/ /mnt/$drive1/Backups/Backblaze/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete /Storage/Backups/Logs/ /mnt/$drive1/Backups/Backblaze/${RESTORE}"
/usr/bin/rsync -avr --delete root@nuc-docker01:/Storage/Backups/Logs/ /mnt/$drive1/Backups/Logs/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete /Storage/Backups/Docker/ /mnt/$drive1/Backups/Docker/${RESTORE}"
/usr/bin/rsync -avr --delete root@nuc-docker01:/Storage/Backups/Docker/ /mnt/$drive1/Backups/Docker/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete /Storage/Video/Pictures/ /mnt/$drive1/Pictures/${RESTORE}"
/usr/bin/rsync -avr --delete root@nuc-docker01:/Storage/Video/Pictures/ /mnt/$drive1/Pictures/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete /Storage/Video/Books/ /mnt/$drive1/Books/${RESTORE}"
/usr/bin/rsync -avr --delete root@nuc-docker01:/Storage/Video/Books/ /mnt/$drive1/Books/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete /Storage/Video/Music/ /mnt/$drive1/Music/${RESTORE}"
/usr/bin/rsync -avr --delete root@nuc-docker01:/Storage/Video/Music/ /mnt/$drive1/Music/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete --exclude=Downloads/ /Storage/Data/ /mnt/$drive1/Data/${RESTORE}"
/usr/bin/rsync -avr --delete --exclude=Downloads/ root@nuc-docker01:/Storage/Data/ /mnt/$drive1/Data/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete /Storage/Video/Movies/ /mnt/$drive2/Movies/${RESTORE}"
/usr/bin/rsync -avr --delete root@nuc-docker01:/Storage/Video/Movies/ /mnt/$drive2/Movies/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete /Storage/Video/TV_Shows/[0-9]* /mnt/$drive1/TV_Shows/${RESTORE}"
/usr/bin/rsync -avr --delete root@nuc-docker01:/Storage/Video/TV_Shows/[0-9]* /mnt/$drive1/TV_Shows/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete /Storage/Video/TV_Shows/[A-K]* /mnt/$drive1/TV_Shows/${RESTORE}"
/usr/bin/rsync -avr --delete root@nuc-docker01:/Storage/Video/TV_Shows/[A-E]* /mnt/$drive1/TV_Shows/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete /Storage/Video/TV_Shows/[L-M]* /mnt/$drive2/TV_Shows/${RESTORE}"
/usr/bin/rsync -avr --delete root@nuc-docker01:/Storage/Video/TV_Shows/[F-M]* /mnt/$drive2/TV_Shows/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete /Storage/Video/TV_Shows/[N-Z]* /mnt/$drive3/TV_Shows/${RESTORE}"
/usr/bin/rsync -avr --delete root@nuc-docker01:/Storage/Video/TV_Shows/[N-Z]* /mnt/$drive3/TV_Shows/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${LGREEN}rsync -avr --delete /Storage/Video/YouTube /mnt/$drive3/YouTube/${RESTORE}"
/usr/bin/rsync -avr --delete root@nuc-docker01:/Storage/Video/YouTube/ /mnt/$drive3/YouTube/
/bin/echo -e "${PURPLE}================================================================================${RESTORE}"
/bin/echo -e "${RED}======= Time: `date`${RESTORE}"
/bin/echo -e "${CYAN}======= Time complete: `date`${RESTORE}"
/bin/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\": \"rocky-rpi4: Backup Complete\"}"
curl -X 'POST' 'https://gotify.sysctl.io/message' -H 'accept: application/json' -H 'Content-Type: application/json' -H 'X-Gotify-Key: ${sops.secrets.gotify-key}' -d "$payload" | jq
'';
in
{
config.environment.systemPackages = [ rsync-backup ];
}

View file

@ -5,6 +5,8 @@
imports = [
inputs.nixos-hardware.nixosModules.raspberry-pi-4
(modulesPath + "/installer/scan/not-detected.nix")
./backup-script.nix
./cron.nix
];
# Enable distributed Builds
nix.distributedBuilds = true;