From 10f127264579d5bc3c89a45c509e2f07346785d9 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 8 Dec 2015 17:41:37 +0000 Subject: [PATCH] Create directories if needed --- src/freedombone-backup-local | 6 +- src/freedombone-backup-remote | 143 +++++++++------------------------- 2 files changed, 40 insertions(+), 109 deletions(-) diff --git a/src/freedombone-backup-local b/src/freedombone-backup-local index f0ab8bdb..34db2413 100755 --- a/src/freedombone-backup-local +++ b/src/freedombone-backup-local @@ -137,9 +137,9 @@ function backup_directory_to_usb { exit 6 fi MY_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}') - if [ ! -d $USB_MOUNT/backup/${2} ]; then - mkdir -p $USB_MOUNT/backup/${2} - fi + if [ ! -d $USB_MOUNT/backup/${2} ]; then + mkdir -p $USB_MOUNT/backup/${2} + fi obnam force-lock -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1} obnam backup -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1} obnam forget --keep=30d -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID diff --git a/src/freedombone-backup-remote b/src/freedombone-backup-remote index b391ab2f..b770a3d7 100755 --- a/src/freedombone-backup-remote +++ b/src/freedombone-backup-remote @@ -71,6 +71,9 @@ function backup_directory_to_friend { exit 43382 fi ADMIN_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}') + if [ ! -d $SERVER_DIRECTORY/backup/${2} ]; then + mkdir -p $SERVER_DIRECTORY/backup/${2} + fi obnam force-lock -r $SERVER_DIRECTORY/backup/${2} --encrypt-with $ADMIN_BACKUP_KEY_ID ${1} obnam backup -r $SERVER_DIRECTORY/backup/${2} --encrypt-with $ADMIN_BACKUP_KEY_ID ${1} obnam forget --keep=30d -r $SERVER_DIRECTORY/backup/${2} --encrypt-with $ADMIN_BACKUP_KEY_ID @@ -123,36 +126,24 @@ for d in /home/*/ ; do # personal settings if [ -d /home/$USERNAME/personal ]; then echo $"Backing up personal settings for $USERNAME" - if [ ! -d $SERVER_DIRECTORY/backup/personal/$USERNAME ]; then - mkdir -p $SERVER_DIRECTORY/backup/personal/$USERNAME - fi backup_directory_to_friend /home/$USERNAME/personal personal/$USERNAME fi # gpg keys if [ -d /home/$USERNAME/.gnupg ]; then echo $"Backing up gpg keys for $USERNAME" - if [ ! -d $SERVER_DIRECTORY/backup/gnupg/$USERNAME ]; then - mkdir -p $SERVER_DIRECTORY/backup/gnupg/$USERNAME - fi backup_directory_to_friend /home/$USERNAME/.gnupg gnupg/$USERNAME fi # ssh keys if [ -d /home/$USERNAME/.ssh ]; then echo $"Backing up ssh keys for $USERNAME" - if [ ! -d $SERVER_DIRECTORY/backup/ssh/$USERNAME ]; then - mkdir -p $SERVER_DIRECTORY/backup/ssh/$USERNAME - fi backup_directory_to_friend /home/$USERNAME/.ssh ssh/$USERNAME fi # config files if [ -d /home/$USERNAME/.config ]; then echo $"Backing up config files for $USERNAME" - if [ ! -d $SERVER_DIRECTORY/backup/config/$USERNAME ]; then - mkdir -p $SERVER_DIRECTORY/backup/config/$USERNAME - fi backup_directory_to_friend /home/$USERNAME/.config config/$USERNAME fi @@ -166,9 +157,6 @@ for d in /home/*/ ; do if [ -f /etc/Muttrc ]; then cp /etc/Muttrc /home/$USERNAME/tempbackup fi - if [ ! -d $SERVER_DIRECTORY/backup/mutt/$USERNAME ]; then - mkdir -p $SERVER_DIRECTORY/backup/mutt/$USERNAME - fi backup_directory_to_friend /home/$USERNAME/tempbackup mutt/$USERNAME fi @@ -179,18 +167,12 @@ for d in /home/*/ ; do mkdir -p /home/$USERNAME/tempbackup fi cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup - if [ ! -d $SERVER_DIRECTORY/backup/procmail/$USERNAME ]; then - mkdir -p $SERVER_DIRECTORY/backup/procmail/$USERNAME - fi backup_directory_to_friend /home/$USERNAME/tempbackup procmail/$USERNAME fi # spamassassin settings if [ -d /home/$USERNAME/.spamassassin ]; then echo $"Backing up spamassassin settings for $USERNAME" - if [ ! -d $SERVER_DIRECTORY/backup/spamassassin/$USERNAME ]; then - mkdir -p $SERVER_DIRECTORY/backup/spamassassin/$USERNAME - fi backup_directory_to_friend /home/$USERNAME/.spamassassin spamassassin/$USERNAME fi @@ -202,9 +184,6 @@ for d in /home/*/ ; do fi tar -czvf /root/backupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir echo $"Backing up emails for $USERNAME" - if [ ! -d $SERVER_DIRECTORY/backup/mail/$USERNAME ]; then - mkdir -p $SERVER_DIRECTORY/backup/mail/$USERNAME - fi backup_directory_to_friend /root/backupemail/$USERNAME mail/$USERNAME fi fi @@ -246,9 +225,6 @@ fi # backup owncloud if [ -d /etc/owncloud ]; then - if [ ! -d $SERVER_DIRECTORY/backup/owncloud2 ]; then - mkdir -p $SERVER_DIRECTORY/backup/owncloud2 - fi backup_database_to_friend owncloud backup_directory_to_friend /root/tempownclouddata ownclouddata echo $"Backing up Owncloud data" @@ -258,12 +234,6 @@ fi # backup gogs if [ -d /home/git/go/src/github.com/gogits ]; then - if [ ! -d $SERVER_DIRECTORY/backup/gogsrepos ]; then - mkdir -p $SERVER_DIRECTORY/backup/gogsrepos - fi - if [ ! -d $SERVER_DIRECTORY/backup/gogsssh ]; then - mkdir -p $SERVER_DIRECTORY/backup/gogsssh - fi backup_database_to_friend gogs backup_directory_to_friend /root/tempgogsdata gogsdata echo $"Obtaining Gogs settings backup" @@ -276,12 +246,6 @@ if [ -d /home/git/go/src/github.com/gogits ]; then fi if [ -d /etc/dokuwiki ]; then - if [ ! -d $SERVER_DIRECTORY/backup/wiki ]; then - mkdir -p $SERVER_DIRECTORY/backup/wiki - fi - if [ ! -d $SERVER_DIRECTORY/backup/wiki2 ]; then - mkdir -p $SERVER_DIRECTORY/backup/wiki2 - fi echo $"Backing up wiki" backup_directory_to_friend /var/lib/dokuwiki wiki backup_directory_to_friend /etc/dokuwiki wiki2 @@ -291,9 +255,6 @@ fi if grep -q "Blog domain" $COMPLETION_FILE; then FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}') if [ -d /var/www/${FULLBLOG_DOMAIN_NAME} ]; then - if [ ! -d $SERVER_DIRECTORY/backup/blog ]; then - mkdir -p $SERVER_DIRECTORY/backup/blog - fi echo $"Backing up blog" backup_directory_to_friend /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs blog else @@ -305,45 +266,30 @@ fi # Backup certificates if [ -d /etc/ssl ]; then echo $"Backing up certificates" - if [ ! -d $SERVER_DIRECTORY/backup/ssl ]; then - mkdir -p $SERVER_DIRECTORY/backup/ssl - fi backup_directory_to_friend /etc/ssl ssl fi # Backup the public mailing list if [ -d /var/spool/mlmmj ]; then echo $"Backing up the public mailing list" - if [ ! -d $SERVER_DIRECTORY/backup/mailinglist ]; then - mkdir -p $SERVER_DIRECTORY/backup/mailinglist - fi backup_directory_to_friend /var/spool/mlmmj mailinglist fi # Backup xmpp settings if [ -d /var/lib/prosody ]; then echo $"Backing up the XMPP settings" - if [ ! -d $SERVER_DIRECTORY/backup/xmpp ]; then - mkdir -p $SERVER_DIRECTORY/backup/xmpp - fi backup_directory_to_friend /var/lib/prosody xmpp fi # Backup web sites if [ -d /etc/nginx ]; then echo $"Backing up web settings" - if [ ! -d $SERVER_DIRECTORY/backup/web ]; then - mkdir -p $SERVER_DIRECTORY/backup/web - fi backup_directory_to_friend /etc/nginx/sites-available web fi # Backup admin user README file if [ -f /home/$ADMIN_USERNAME/README ]; then echo $"Backing up README" - if [ ! -d $SERVER_DIRECTORY/backup/readme ]; then - mkdir -p $SERVER_DIRECTORY/backup/readme - fi if [ ! -d /home/$ADMIN_USERNAME/tempbackup ]; then mkdir -p /home/$ADMIN_USERNAME/tempbackup fi @@ -354,18 +300,12 @@ fi # Backup IPFS if [ -d /home/$ADMIN_USERNAME/.ipfs ]; then echo $"Backing up IPFS" - if [ ! -d $SERVER_DIRECTORY/backup/ipfs ]; then - mkdir -p $SERVER_DIRECTORY/backup/ipfs - fi backup_directory_to_friend /home/$ADMIN_USERNAME/.ipfs ipfs fi # Backup DLNA cache if [ -d /var/cache/minidlna ]; then echo $"Backing up DLNA cache" - if [ ! -d $SERVER_DIRECTORY/backup/dlna ]; then - mkdir -p $SERVER_DIRECTORY/backup/dlna - fi backup_directory_to_friend /var/cache/minidlna dlna fi @@ -378,18 +318,12 @@ if [ -f /etc/mumble-server.ini ]; then cp -f /etc/mumble-server.ini /root/tempvoipbackup cp -f /var/lib/mumble-server/mumble-server.sqlite /root/tempvoipbackup cp -f /etc/sipwitch.conf /root/tempvoipbackup - if [ ! -d $SERVER_DIRECTORY/backup/voip ]; then - mkdir -p $SERVER_DIRECTORY/backup/voip - fi backup_directory_to_friend /root/tempvoipbackup voip fi # Backup Tox node settings if [ -d /var/lib/tox-bootstrapd ]; then echo "Backing up Tox node settings" - if [ ! -d $SERVER_DIRECTORY/backup/tox ]; then - mkdir -p $SERVER_DIRECTORY/backup/tox - fi if [ -d /var/lib/tox-bootstrapd/Maildir ]; then rm -rf /var/lib/tox-bootstrapd/Maildir fi @@ -399,9 +333,6 @@ fi # MariaDB settings if [ ${#DATABASE_PASSWORD} -gt 1 ]; then - if [ ! -d $SERVER_DIRECTORY/backup/mariadb ]; then - mkdir -p $SERVER_DIRECTORY/backup/mariadb - fi if [ ! -d /root/tempmariadb ]; then mkdir /root/tempmariadb fi @@ -434,45 +365,45 @@ do echo "$NOW Starting backup to $REMOTE_SERVER" >> /var/log/remotebackups.log # Social key management - for d in /home/*/ ; do - USERNAME=$(echo "$d" | awk -F '/' '{print $3}') - if [[ $USERNAME != "git" ]]; then - if [ -d /home/$USERNAME/.gnupg_fragments ]; then - if [ $REMOTE_DOMAIN ]; then - cd /home/$USERNAME/.gnupg_fragments - no_of_shares=$(ls -afq keyshare.asc.* | wc -l) - if (( no_of_shares > 0 )); then - # Pick a share index based on the domain name - # This ensures that the same share is always given to the same domain - sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN") - share_index=$(echo $((0x${sharenumstr%% *} % ${no_of_shares})) | tr -d -) + for d in /home/*/ ; do + USERNAME=$(echo "$d" | awk -F '/' '{print $3}') + if [[ $USERNAME != "git" ]]; then + if [ -d /home/$USERNAME/.gnupg_fragments ]; then + if [ $REMOTE_DOMAIN ]; then + cd /home/$USERNAME/.gnupg_fragments + no_of_shares=$(ls -afq keyshare.asc.* | wc -l) + if (( no_of_shares > 0 )); then + # Pick a share index based on the domain name + # This ensures that the same share is always given to the same domain + sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN") + share_index=$(echo $((0x${sharenumstr%% *} % ${no_of_shares})) | tr -d -) - # get the share filename - share_files=(/home/$USERNAME/.gnupg_fragments/keyshare.asc.*) - share_filename=${share_files[share_index]} + # get the share filename + share_files=(/home/$USERNAME/.gnupg_fragments/keyshare.asc.*) + share_filename=${share_files[share_index]} - # create a temp directory containing the share - mkdir -p /home/$USERNAME/tempkey/.gnupg_fragments_$USERNAME - cp $share_filename /home/$USERNAME/tempkey/.gnupg_fragments_$USERNAME/ + # create a temp directory containing the share + mkdir -p /home/$USERNAME/tempkey/.gnupg_fragments_$USERNAME + cp $share_filename /home/$USERNAME/tempkey/.gnupg_fragments_$USERNAME/ - # copy the fragments directory to the remote server - /usr/bin/sshpass -p $REMOTE_PASSWORD scp -r -P $REMOTE_SSH_PORT /home/$USERNAME/tempkey/.gnupg_fragments_$USERNAME $REMOTE_SERVER - if [ ! "$?" = "0" ]; then - # Send a warning email - echo "Key share to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS - fi + # copy the fragments directory to the remote server + /usr/bin/sshpass -p $REMOTE_PASSWORD scp -r -P $REMOTE_SSH_PORT /home/$USERNAME/tempkey/.gnupg_fragments_$USERNAME $REMOTE_SERVER + if [ ! "$?" = "0" ]; then + # Send a warning email + echo "Key share to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS + fi - # remove the temp file/directory - shred -zu /home/$USERNAME/tempkey/.gnupg_fragments_$USERNAME/* - rm -rf /home/$USERNAME/tempkey + # remove the temp file/directory + shred -zu /home/$USERNAME/tempkey/.gnupg_fragments_$USERNAME/* + rm -rf /home/$USERNAME/tempkey - # Send a confirmation email - echo "Key shared to $REMOTE_SERVER" | mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS - fi - fi - fi - fi - done + # Send a confirmation email + echo "Key shared to $REMOTE_SERVER" | mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS + fi + fi + fi + fi + done rsync -ratlzv --rsh="/usr/bin/sshpass -p $REMOTE_PASSWORD ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $SERVER_DIRECTORY/backup $REMOTE_SERVER if [ ! "$?" = "0" ]; then