pub / rsync_encrypted_backup

Encrypted backups based on gocryptfs and rsync
git clone https://https://src.jayvii.de/pub/rsync_encrypted_backup.git
Home | Log | Files | Exports | Refs | README | RSS

backup.sh (2285B)


      1 #!/usr/bin/env bash
      2 
      3 # Define functions -------------------------------------------------------------
      4 function send_notify {
      5     gdbus call --session \
      6         --dest=org.freedesktop.Notifications \
      7         --object-path=/org/freedesktop/Notifications \
      8         --method=org.freedesktop.Notifications.Notify \
      9         "$1" $2 "$3" "$1" "$4" "$5" \
     10         '{"category": <"im.received">}' 3000
     11 }
     12 
     13 # Configuration ----------------------------------------------------------------
     14 
     15 # Extra Options for rsync
     16 EXOP=""
     17 # EXOP+="-e 'ssh -p23'" # use ssh via port 23
     18 
     19 # Excluded Directories
     20 EXCL="--exclude=.cache/*"
     21 EXCL+=" --exclude=.var/*"
     22 EXCL+=" --exclude=.local/share/Trash/*"
     23 
     24 # Define Source and Target directories
     25 SOURCE_PLAIN="${1}/"
     26 SOURCE_CRYPT="$(mktemp --directory)/"
     27 TARGET="${2}:${HOSTNAME}_$(date +%m)/"
     28 
     29 # Ask User for password of remote storage --------------------------------------
     30 echo "PLEASE TYPE IN PASSWORD FOR REMOTE STORAGE"
     31 read -s PASSWORD
     32 
     33 # Mount Source directory as encrypted ------------------------------------------
     34 echo "[INFO] Attempting to mount source as encrypted dir." 
     35 gocryptfs --ro --reverse "$SOURCE_PLAIN" "$SOURCE_CRYPT" || exit 1;
     36 
     37 # Start Backup Procedure -------------------------------------------------------
     38 send_notify \
     39     "BackUpr" \
     40     0 \
     41     "document-send" \
     42     "Starting backup procedure to $TARGET" \
     43     "[]"
     44     
     45 SSHPASS="$PASSWORD" sshpass -e \
     46     rsync \
     47     --archive \ # recursively and retaining user permissions
     48     --update \ # only write if file is different to version on target
     49     --verbose \ # print output to console
     50     --progress \ # show progress of transmission
     51     ${EXCL} \ # insert previously defined exclusions
     52     ${EXOP} \ # extra options
     53     "$SOURCE_CRYPT" \ # encrypted source directory
     54     "$TARGET" # target directory
     55 
     56 # Send Status-Message on exit --------------------------------------------------
     57 if [[ "$?" == "0" ]]; then
     58     send_notify "BackUpr" 0 "document-send" "Backup finished successfully." "[]"
     59 else
     60     send_notify  "BackUpr" 0 "document-send" "Backup failed!" "[]"
     61 fi
     62 
     63 # Unmount encrypted Source directory again -------------------------------------
     64 fusermount -u "$SOURCE_CRYPT"
     65 
     66 # Exit Program -----------------------------------------------------------------
     67 echo "[INFO] Done."