1
0

backup_server_to_disk.sh 2.0 KB

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