1
0

backup_server_to_disk.sh 1.7 KB

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