create-backup-rsync-net.sh 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. # Script to backup personal files to the external USB drive.
  4. # Specify the mount point here (DO NOT end mount_point with a forward-slash).
  5. mount_point='/media/Buffalo_backup'
  6. echo "#####"
  7. echo ""
  8. # Check whether target volume is mounted, and mount it if not.
  9. if ! mountpoint -q ${mount_point}/; then
  10. echo "Mounting the external USB drive."
  11. echo "Mountpoint is ${mount_point}"
  12. if ! mount ${mount_point}; then
  13. echo "An error code was returned by mount command!"
  14. exit 5
  15. else echo "Mounted successfully.";
  16. fi
  17. else echo "${mount_point} is already mounted.";
  18. fi
  19. # Target volume **must** be mounted by this point. If not, die screaming.
  20. if ! mountpoint -q ${mount_point}/; then
  21. echo "Mounting failed! Cannot run backup without backup volume!"
  22. exit 1
  23. fi
  24. echo "Preparing to transfer differences using rsync."
  25. # Use the year to create a new backup directory each year.
  26. current_year=`date +%Y`
  27. # Now construct the backup path, specifying the mount point followed by the path
  28. # to our backup directory, finishing with the current year.
  29. # (DO NOT end backup_path with a forward-slash.)
  30. backup_path=${mount_point}'/rsync-backup/'${current_year}
  31. echo "Backup storage directory path is ${backup_path}"
  32. echo "Starting backup of /home/bob . . . "
  33. # Create the target directory path if it does not already exist.
  34. mkdir --parents ${backup_path}/home/bob/
  35. # Use rsync to do the backup, and pipe output to tee command (so it gets saved
  36. # to file AND output to screen).
  37. # Note that the 2>&1 part simply instructs errors to be sent to standard output
  38. # so that we see them in our output file.
  39. sudo rsync --archive --verbose --human-readable --itemize-changes --progress \
  40. --delete --delete-excluded \
  41. --exclude='/.gvfs/' --exclude='/Examples/' --exclude='/.local/share/Trash/' \
  42. --exclude='/.thumbnails/' --exclude='/transient-items/' \
  43. /home/bob/ ${backup_path}/home/bob/ 2>&1 | tee /home/bob/rsync-output.txt
  44. echo "Starting backup of /var/www . . . "
  45. mkdir --parents ${backup_path}/var/www/
  46. # This time use the -a flag with the tee command, so that it appends to the end
  47. # of the rsync-output.txt file rather than start a new file from scratch.
  48. sudo rsync --archive --verbose --human-readable --itemize-changes --progress \
  49. --delete --delete-excluded \
  50. --exclude='/.Trash-1000/' \
  51. /var/www/ ${backup_path}/var/www/ 2>&1 | tee -a /home/bob/rsync-output.txt
  52. # Ask user whether target volume should be unmounted.
  53. echo -n "Do you want to unmount ${mount_point} (no)"
  54. read -p ": " unmount_answer
  55. unmount_answer=${unmount_answer,,} # make lowercase
  56. if [ "$unmount_answer" == "y" ] || [ "$unmount_answer" == "yes" ]; then
  57. if ! umount ${mount_point}; then
  58. echo "An error code was returned by umount command!"
  59. exit 5
  60. else echo "Dismounted successfully.";
  61. fi
  62. else echo "Volume remains mounted.";
  63. fi
  64. echo ""
  65. echo "####"