backup.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/bin/sh
  2. targetnode="/dev/disk/by-uuid/MY-UUID"
  3. targetmp="/mnt/backup/target"
  4. targetsub="datensicherung"
  5. sourcepath="/mnt/backup/snapshots/root"
  6. btrfs=1
  7. ####
  8. die()
  9. {
  10. local msg1="$1"
  11. local msg2="$2"
  12. if [ -n "$msg1" ]; then
  13. echo "$msg1" >&2
  14. fi
  15. echo >&2
  16. echo >&2
  17. echo >&2
  18. echo >&2
  19. echo "### FEHLER! FEHLER! FEHLER! ###" >&2
  20. echo "###" >&2
  21. if [ -n "$msg2" ]; then
  22. echo "### $msg2" >&2
  23. else
  24. echo "### Die Datensicherung wurde abgebrochen und ist unvollständig!" >&2
  25. fi
  26. echo "###" >&2
  27. echo "### FEHLER! FEHLER! FEHLER! ###" >&2
  28. echo >&2
  29. read -p "" x
  30. exit 1
  31. }
  32. # Become root.
  33. mypath="$(realpath -e "$0")"
  34. if [ "$(id -u)" != "0" ]; then
  35. if [ "$1" = "SECOND_STAGE" ]; then
  36. die "Second stage failed."
  37. else
  38. exec sudo "$mypath" SECOND_STAGE
  39. exit 1
  40. fi
  41. fi
  42. # Setup cleanup handler.
  43. cleanup()
  44. {
  45. umount -f "$targetmp" >/dev/null 2>&1
  46. if [ $btrfs -ne 0 ]; then
  47. btrfs subvolume delete "$sourcepath" >/dev/null 2>&1
  48. fi
  49. }
  50. trap cleanup EXIT
  51. # Create the snapshot
  52. if [ $btrfs -ne 0 ]; then
  53. btrfs subvolume delete "$sourcepath" >/dev/null 2>&1
  54. btrfs subvolume snapshot -r / "$sourcepath" ||\
  55. die "btrfs snapshot failed"
  56. fi
  57. # Mount the backup drive.
  58. mkdir -p "$targetmp" || die "mkdir target failed."
  59. if ! [ -b "$targetnode" ]; then
  60. die "dev node not present" "Die Backup-Festplatte ist nicht angeschlossen!"
  61. fi
  62. umount -f "$targetnode" >/dev/null 2>&1
  63. umount -f "$targetmp" >/dev/null 2>&1
  64. mount "$targetnode" "$targetmp" || die "target mount failed"
  65. # Sync the backup drive with the source drive.
  66. mkdir -p "$targetmp/$targetsub" || die "mkdir target sub failed"
  67. while true; do
  68. rsync -aHAX --inplace --delete-before --progress \
  69. "$sourcepath"/ \
  70. "$targetmp/$targetsub"
  71. res=$?
  72. [ $res -eq 24 ] && continue
  73. [ $res -ne 0 ] && die
  74. break
  75. done
  76. if [ $btrfs -ne 0 ]; then
  77. mkdir -p "$targetmp/$targetsub"_boot || die "mkdir target sub (boot) failed"
  78. while true; do
  79. rsync -aHAX --inplace --delete-before --progress \
  80. /boot/ \
  81. "$targetmp/$targetsub"_boot
  82. res=$?
  83. [ $res -eq 24 ] && continue
  84. [ $res -ne 0 ] && die
  85. break
  86. done
  87. fi
  88. umount "$targetmp" || die "umount failed"
  89. if [ $btrfs -ne 0 ]; then
  90. btrfs subvolume delete "$sourcepath" ||\
  91. die "btrfs snapshot delete failed"
  92. fi
  93. # Create time stamp.
  94. mkdir -p /var/lib/simplebackup || die "mkdir /var/lib/simplebackup failed"
  95. date --utc '+%s' > /var/lib/simplebackup/stamp || die "Failed to create time stamp"
  96. echo
  97. echo
  98. echo
  99. echo "########################################################"
  100. echo "### Alles Ok! ###"
  101. echo "### Die Festplatte kann jetzt abgesteckt werden. ###"
  102. echo "########################################################"
  103. read -p "" x
  104. exit 0