123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- #!/usr/bin/env bash
- # Copyright 2023 Bryan C. Roessler
- #
- # This script replaces btrfs send|receive with rsync for resumable btrbk backups
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- TESTING=1
- main() {
- # The source subvolume to backup
- SRC_SUBVOLUME="$1"
- # The location of the intermediate btrfs image file
- # This must be at least the size of SRC (including snapshots) and ideally on fast storage
- SRC_TMPDIR="$2"
- # The remote destination
- DEST_HOST="$3"
- DEST_TMPDIR="$4"
- DEST_DIR="$5"
- # Get rid of garbled backups
- sudo btrbk clean
- # Create raw archive file
- # TODO Encryption/compression
- sudo btrbk archive "$SRC_SUBVOLUME" "$SRC_TMPDIR" --raw
- # Retern the latest image
- # "<snapshot-name>.<timestamp>[_N].btrfs[.gz|.bz2|.xz][.gpg]"
- #SRC_TMPFILENAME=$(find "$SRC_TMPDIR" -type f -regex '.*/*.btrfs' -printf "%f\n" | tail -1)
- SRC_FILEPATH="$SRC_TMPDIR/$SRC_FILENAME"
- DEST_TMPDIR="$DEST_TMPDIR/${SRC_FILENAME%%'.btrfs'}"
- DEST="$DEST_DIR/${SRC_FILENAME%%'.btrfs'}"
- # shellcheck disable=SC2029
- sudo rsync \
- --append-verify \
- --remove-source-files \
- "$SRC_FILEPATH" "$DEST:$DEST_TMP" &&
- ssh "$DEST_HOST" "
- btrfs receive --verbose --progress -f $DEST_TMP $DEST &&
- rm -f $DEST_TMPFILE
- "
- }
- # For testing
- if (( TESTING )); then
- main "${1:"/home/bryan"}" \
- "${2:"/mnt/temp"}" \
- "${3:"router"}" \
- "${4:"/mnt/backup"}" \
- "${5:"/mnt/backup/workstation/home"}" # TODO Need a separate SSD :-/ # i.e. root@router.lan from ~/.ssh/config
- else
- # For deploy
- main "$@"
- fi
- exit
|