Adding rsync scripts
authorBrian Flowers <git-admn@bsflowers.net>
Sat, 24 Apr 2021 02:05:54 +0000 (22:05 -0400)
committerBrian Flowers <git-admn@bsflowers.net>
Sat, 24 Apr 2021 02:05:54 +0000 (22:05 -0400)
rsync/description [new file with mode: 0644]
rsync/rsync.env [new file with mode: 0644]
rsync/rsync.sh [new file with mode: 0755]

diff --git a/rsync/description b/rsync/description
new file mode 100644 (file)
index 0000000..9c001e9
--- /dev/null
@@ -0,0 +1 @@
+My nightly backup script
diff --git a/rsync/rsync.env b/rsync/rsync.env
new file mode 100644 (file)
index 0000000..9778134
--- /dev/null
@@ -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: "<path_to_backup>|<destination_folder>"
+LOCAL_PATHS=( "/home/slightlycyberpunk/|home" )
+
+# Array of remote directories to be backed up
+# Format is: "<user@hostname>|<path_to_backup>|<destination_folder>"
+# To give a specific user read-only access to be used for backups, use:
+#  setfacl -R -m user:<username>:rx <directory>
+# Also need to set sudo permissions on 'umount .tmpmnt' in /etc/sudoers
+#  <user> 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 (executable)
index 0000000..79c3754
--- /dev/null
@@ -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