From d9968a5fe799ed5cb7eb9466ac5219212487bb7b Mon Sep 17 00:00:00 2001
From: Matthew Saunders Brown <msb@stackaas.com>
Date: Fri, 5 Aug 2022 08:31:07 -0700
Subject: [PATCH] add subdir option

---
 sbin/bashup.sh | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/sbin/bashup.sh b/sbin/bashup.sh
index bea2cb1..97f1a91 100755
--- a/sbin/bashup.sh
+++ b/sbin/bashup.sh
@@ -19,7 +19,12 @@ backup_storage_dir='/mnt/backups';
 bashup_jobs=('files' 'mysql');
 
 # directories to be backed up by files
-backup_dirs=('/etc' '/home' '/root' '/srv' '/usr/local' '/var/www');
+backup_dirs=('/etc' '/home/' '/root' '/srv/' '/usr/local' '/var/www/');
+# to back up an entire directory do *not* add a trailing slash
+# if you add a trailing slash then instead of backing up that directory itself
+# a list of all subdirectories is generated and added to the array
+# note that with a trailing slash files in the top level directory itself will *not* be backed up,
+# only the subdirectories within it
 
 # mysql config file that contains 'host' 'user' 'password' vars
 defaults_extra_file='/etc/mysql/debian.cnf';
@@ -39,6 +44,27 @@ if [ "${EUID}" -ne 0 ]; then
   exec sudo -u root --shell /bin/bash $0 $@
 fi
 
+# check backup_dirs for tailing slash (subdir option)
+i=0
+for dir in "${backup_dirs[@]}"; do
+  if grep -qs '/$' <<< "$dir"; then
+    # found match, first remove dir from array
+    unset "backup_dirs[$i]"
+    # check for subdirs
+    if [[ -d $dir ]]; then
+      subdirs=(`ls -1 $dir`)
+      if [[ ${#subdirs[@]} -gt 0 ]]; then
+        for subdir in "${subdirs[@]}"; do
+          if [[ -d "$dir$subdir" ]]; then
+            backup_dirs+=("$dir$subdir")
+          fi
+        done
+      fi
+    fi
+  fi
+  ((i++))
+done
+
 # check for backup storage directory and mount if need be
 if [ -d $backup_storage_dir ]; then
   # check if backup_storage_dir is a mount in fstab