From 6a4e6ae5ecdcfa8118e2e3f7c6f8040a92d7fb34 Mon Sep 17 00:00:00 2001 From: Brian Flowers Date: Fri, 23 Apr 2021 22:05:54 -0400 Subject: [PATCH] Adding rsync scripts --- rsync/description | 1 + rsync/rsync.env | 16 +++++++++++++ rsync/rsync.sh | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 rsync/description create mode 100644 rsync/rsync.env create mode 100755 rsync/rsync.sh diff --git a/rsync/description b/rsync/description new file mode 100644 index 0000000..9c001e9 --- /dev/null +++ b/rsync/description @@ -0,0 +1 @@ +My nightly backup script diff --git a/rsync/rsync.env b/rsync/rsync.env new file mode 100644 index 0000000..9778134 --- /dev/null +++ b/rsync/rsync.env @@ -0,0 +1,16 @@ +# Host and path of the backup location -- this can be localhost +BACKUP_USER="slightlycyberpunk" +BACKUP_HOST="192.168.42.42" +BACKUP_LOCATION="/home/slightlycyberpunk/media/backups" + +# Array of directories on the local system to back up +# Format is: "|" +LOCAL_PATHS=( "/home/slightlycyberpunk/|home" ) + +# Array of remote directories to be backed up +# Format is: "||" +# To give a specific user read-only access to be used for backups, use: +# setfacl -R -m user::rx +# Also need to set sudo permissions on 'umount .tmpmnt' in /etc/sudoers +# ALL = (root) NOPASSWD: /usr/bin/umount .tmpmnt +#REMOTE_PATHS=( "slightlycyberpunk@remote.slightlycyberpunk.com|/home/slightlycyberpunk/|remoteannex" ) diff --git a/rsync/rsync.sh b/rsync/rsync.sh new file mode 100755 index 0000000..79c3754 --- /dev/null +++ b/rsync/rsync.sh @@ -0,0 +1,68 @@ +#!/bin/bash +cwd=`dirname $0` +. ${cwd}/../etc/rsync.env + + +YEAR=$(date +"%Y") +MON=$(date +"%m") +DAY=$(date +"%d") + +####################################### +#### LOCAL FOLDERS #### +####################################### + + +for path in ${LOCAL_PATHS[@]}; do + # Cut up the path and extract each element + src=`echo $path | cut -d'|' -f 1` + dst=`echo $path | cut -d'|' -f 2` + + # Make required directories on the remote server + ssh ${BACKUP_USER}@${BACKUP_HOST} \ + "mkdir -p ${BACKUP_LOCATION}/${dst}/full" + ssh ${BACKUP_USER}@${BACKUP_HOST} \ + "mkdir -p ${BACKUP_LOCATION}/${dst}/incr/$YEAR/$MON/$DAY" + + # Run the rsync command and log to the remote server + rsync -avslhibx --inplace --delete \ + --backup --backup-dir=../incr/$YEAR/$MON/$DAY \ + $src \ + ${BACKUP_USER}@${BACKUP_HOST}:${BACKUP_LOCATION}/${dst}/full | \ + ssh ${BACKUP_USER}@${BACKUP_HOST} \ + "cat > ${BACKUP_LOCATION}/${dst}/incr/rsync.${YEAR}${MON}${DAY}.log" +done + +####################################### +#### REMOTE FOLDERS #### +####################################### + +for path in ${REMOTE_PATHS[@]}; do + # Cut up the path and extract each element + login=`echo $path | cut -d'|' -f 1` + src=`echo $path | cut -d'|' -f 2` + dst=`echo $path | cut -d'|' -f 3` + + # Make required directories on the remote server + ssh ${BACKUP_USER}@${BACKUP_HOST} \ + "mkdir -p ${BACKUP_LOCATION}/${dst}/full" + ssh ${BACKUP_USER}@${BACKUP_HOST} \ + "mkdir -p ${BACKUP_LOCATION}/${dst}/incr/$YEAR/$MON/$DAY" + + # Temporarily mount the remote source filesystem + mkdir .tmpmnt + sshfs ${login}:${src} .tmpmnt + + if [ $? -ne 0 ]; then + echo "Failed to mount remote path ${login}:${src}" + continue + fi + + # Run the rsync command and log to the remote server + rsync -avslhib --inplace --delete \ + --backup --backup-dir=../incr/$YEAR/$MON/$DAY \ + .tmpmnt/ \ + ${BACKUP_USER}@${BACKUP_HOST}:${BACKUP_LOCATION}/${dst}/full | \ + ssh ${BACKUP_USER}@${BACKUP_HOST} \ + "cat > ${BACKUP_LOCATION}/${dst}/incr/rsync.${YEAR}${MON}${DAY}.log" + sudo umount .tmpmnt +done -- 1.8.3.1