backup_server_to_disk.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/bash
  2. SERVER=${1:-"cnew"}
  3. DISK=${2:-"/Volumes/Server"}
  4. BWLIMIT=${3:-"0"}
  5. ONLY_RASPI=${4:-"N"}
  6. echo "Backing up server $SERVER to disk $DISK."
  7. if [[ "$BWLIMIT" != "0" ]]; then
  8. echo "I/O bandwidth limit (rsync --bwlimit) is $BWLIMIT"
  9. fi
  10. if [[ "$ONLY_RASPI" == "Y" ]]; then
  11. echo "Skipping main backup. Only backing up raspi"
  12. fi
  13. read -n 1 -p "Press Y to continue: " userinput
  14. echo
  15. if [[ "$userinput" != "Y" ]]; then
  16. echo "Abort."
  17. exit
  18. else
  19. echo "Backing up."
  20. fi
  21. # Basic snapshot-style rsync backup script
  22. date=`date "+%Y-%m"`
  23. # Run rsync to create snapshot
  24. # exclude backups on the remote to prevent filling with hardlinks
  25. if [[ "$ONLY_RASPI" != "Y" ]]; then
  26. rsync -aPhRz \
  27. --bwlimit=$BWLIMIT \
  28. --exclude "*-backup*/*" --exclude="*cache*" --rsync-path "sudo rsync" \
  29. --link-dest=$DISK/last $SERVER:/etc :/home :/var :/root $DISK/$date
  30. else
  31. echo "Skipped main backup"
  32. fi
  33. # copy lastest backups by following symlinks
  34. # need to explicitly include each level of directories before adding a global
  35. # */last pattern
  36. rsync -aPhRzLK \
  37. --bwlimit=$BWLIMIT \
  38. --include "var/" --include "*-backups/" \
  39. --exclude ".npm/*" --exclude "*cache*/*" \
  40. --include "home/" --include "home/raspi-backup/" \
  41. --include "*-backup*/last" --include "*-backup*/last/**" --exclude="*" --rsync-path "sudo rsync" \
  42. --link-dest=$DISK/last $SERVER:/home/raspi-backup :/var $DISK/$date
  43. # Remove symlink to previous snapshot
  44. rm -f $DISK/last
  45. # Create new symlink to latest snapshot for the next backup to hardlink
  46. ln -s $DISK/$date $DISK/last