backup_server_to_disk.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. --numeric-ids \
  28. --rsh="ssh -F $HOME/.ssh/config_root" \
  29. --delete \
  30. --delete-excluded \
  31. --bwlimit=$BWLIMIT \
  32. --exclude "*-backup*/*" \
  33. --exclude="*cache*" \
  34. --include="/var/lib/docker/" \
  35. --include="/var/lib/docker/volumes" \
  36. --include="/var/lib/docker/volumes/**" \
  37. --exclude="/var/lib/docker/*" \
  38. --rsync-path "sudo rsync" \
  39. --link-dest=$DISK/last $SERVER:/etc :/home :/var :/root :/opt :/sbin :/shared :/srv :/usr $DISK/$DATE
  40. # Remove symlink to previous snapshot
  41. rm -f $DISK/last
  42. # Create new symlink to latest snapshot for the next backup to hardlink
  43. ln -s $DISK/$DATE $DISK/last
  44. else
  45. echo "Skipped main backup"
  46. fi
  47. # copy lastest backups by following symlinks
  48. # need to explicitly include each level of directories before adding a global
  49. # */last pattern
  50. rsync -aPhRzLK \
  51. --numeric-ids \
  52. --rsh="ssh -F $HOME/.ssh/config_root" \
  53. --delete \
  54. --delete-excluded \
  55. --bwlimit=$BWLIMIT \
  56. --include "var/" \
  57. --include "*-backups/" \
  58. --exclude ".npm/*" \
  59. --exclude "*cache*/*" \
  60. --include "home/" \
  61. --include "home/raspi-backup/" \
  62. --include "*-backup*/last" \
  63. --include "*-backup*/last/**" \
  64. --exclude="*" \
  65. --rsync-path "sudo rsync" \
  66. --link-dest=$DISK/raspi-backup/last $SERVER:/home/raspi-backup $DISK/raspi-backup/$DATE
  67. # Remove symlink to previous snapshot
  68. rm -f $DISK/raspi-backup/last
  69. # Create new symlink to latest snapshot for the next backup to hardlink
  70. ln -s $DISK/raspi-backup/$DATE $DISK/raspi-backup/last