btrbk-archive-rsync 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env bash
  2. # Copyright 2023 Bryan C. Roessler
  3. #
  4. # This script replaces btrfs send|receive with rsync for resumable btrbk backups
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. TESTING=1
  18. main() {
  19. # The source subvolume to backup
  20. SRC_SUBVOLUME="$1"
  21. # The location of the intermediate btrfs image file
  22. # This must be at least the size of SRC (including snapshots) and ideally on fast storage
  23. SRC_TMPDIR="$2"
  24. # The remote destination
  25. DEST_HOST="$3"
  26. DEST_TMPDIR="$4"
  27. DEST_DIR="$5"
  28. # Get rid of garbled backups
  29. sudo btrbk clean
  30. # Create raw archive file
  31. # TODO Encryption/compression
  32. sudo btrbk archive "$SRC_SUBVOLUME" "$SRC_TMPDIR" --raw
  33. # Retern the latest image
  34. # "<snapshot-name>.<timestamp>[_N].btrfs[.gz|.bz2|.xz][.gpg]"
  35. #SRC_TMPFILENAME=$(find "$SRC_TMPDIR" -type f -regex '.*/*.btrfs' -printf "%f\n" | tail -1)
  36. SRC_FILEPATH="$SRC_TMPDIR/$SRC_FILENAME"
  37. DEST_TMPDIR="$DEST_TMPDIR/${SRC_FILENAME%%'.btrfs'}"
  38. DEST="$DEST_DIR/${SRC_FILENAME%%'.btrfs'}"
  39. # shellcheck disable=SC2029
  40. sudo rsync \
  41. --append-verify \
  42. --remove-source-files \
  43. "$SRC_FILEPATH" "$DEST:$DEST_TMP" &&
  44. ssh "$DEST_HOST" "
  45. btrfs receive --verbose --progress -f $DEST_TMP $DEST &&
  46. rm -f $DEST_TMPFILE
  47. "
  48. }
  49. # For testing
  50. if (( TESTING )); then
  51. main "${1:"/home/bryan"}" \
  52. "${2:"/mnt/temp"}" \
  53. "${3:"router"}" \
  54. "${4:"/mnt/backup"}" \
  55. "${5:"/mnt/backup/workstation/home"}" # TODO Need a separate SSD :-/ # i.e. root@router.lan from ~/.ssh/config
  56. else
  57. # For deploy
  58. main "$@"
  59. fi
  60. exit