Rewrite backup-android to use bash arrays

This commit is contained in:
Austen Adler 2019-11-29 11:03:15 -05:00
parent a38b691403
commit 758e27bd51
3 changed files with 85 additions and 41 deletions

View File

@ -1,5 +1,6 @@
#!/data/data/com.termux/files/usr/bin/bash #!/data/data/com.termux/files/usr/bin/bash
# Initialize # Initialize
# if [ -z "$STY" ]; then exec screen -m -S backup-android /bin/bash "$0"; fi
ABSPATH="$(readlink -f "$BASH_SOURCE")" ABSPATH="$(readlink -f "$BASH_SOURCE")"
set -x set -x
cd "${ABSPATH%/*}" || { echo "Cannot change directory" >&2; exit 1; } cd "${ABSPATH%/*}" || { echo "Cannot change directory" >&2; exit 1; }
@ -27,8 +28,11 @@ function time_diff {
} }
function prune() { function prune() {
test -z "$PRUNE_COMMAND" && return if [ -z "$PRUNE_COMMAND" ]; then
./run_remote.sh cd "$DESTINATION_LOCATION" "&&" $PRUNE_COMMAND "$BORG_REPO" echo "Not pruning" >&2
return
fi
run_remote "${PRUNE_COMMAND[@]}" "$BORG_REPO"
} }
function backup() { function backup() {
# First, we need to rsync our changes to the host # First, we need to rsync our changes to the host
@ -36,7 +40,7 @@ function backup() {
local RETRIES=10 local RETRIES=10
while (( RETRIES > 0 )); do while (( RETRIES > 0 )); do
# Try to rsync # Try to rsync
$RSYNC_COMMAND "$SOURCE_LOCATION" "$REMOTE_HOST:$DESTINATION_LOCATION" && break "${RSYNC_COMMAND[@]}" "$SOURCE_LOCATION" "$REMOTE_HOST:$DESTINATION_LOCATION" && break
(( RETRIES-- )) (( RETRIES-- ))
echo "rsync failed. Retrying $RETRIES more times. Sleeping 10s" echo "rsync failed. Retrying $RETRIES more times. Sleeping 10s"
sleep 10s sleep 10s
@ -49,7 +53,7 @@ function backup() {
# Next, instruct the host to create a borg # Next, instruct the host to create a borg
echo "Running borg..." echo "Running borg..."
./run_remote.sh cd "$DESTINATION_LOCATION" "&&" pwd "&&" $BORG_COMMAND "$BORG_REPO::$HOSTNAME-$(uuidgen)" . || return 2 run_remote "${CREATE_COMMAND[@]}" "$BORG_REPO::$HOSTNAME-$(uuidgen)" . || return 2
} }
# Take wakelock so we don't fall asleep # Take wakelock so we don't fall asleep
@ -75,3 +79,5 @@ fi
# Remove our wakelock # Remove our wakelock
termux-wake-unlock termux-wake-unlock
printf "^c to exit"
cat

View File

@ -1,8 +1,51 @@
RSYNC_COMMAND="rsync --partial --info=progress2 --progress --verbose --no-i-r -a --exclude=/syncthing --exclude=/Movies --exclude=.thumbnails --exclude=/Android --exclude=/Download/Images --delete" EXCLUDES=(
"/syncthing"
"/Movies"
".thumbnails"
"/Android"
"/Download/Images"
)
RSYNC_COMMAND=(
rsync
--partial
--info=progress2
--verbose
--no-i-r
-a
--delete
"${EXCLUDES[@]/#/--exclude=}"
)
SOURCE_LOCATION="/storage/emulated/0/" SOURCE_LOCATION="/storage/emulated/0/"
HOSTNAME="android-phone" HOSTNAME="android-phone"
REMOTE_HOST="user@example.com" REMOTE_HOST="user@example.com"
DESTINATION_LOCATION="~/tmp/borg/$HOSTNAME/" DESTINATION_LOCATION="~/tmp/borg/$HOSTNAME/"
BORG_COMMAND="borg create --verbose --progress --stats --one-file-system --exclude-caches --compression auto,lzma"
BORG_REPO="runner2:borg/$HOSTNAME" BORG_REPO="runner2:borg/$HOSTNAME"
# PRUNE_COMMAND="borg prune -v --list --keep-daily 7 --keep-weekly 4 --keep-monthly 2" BORG_COMMAND=(
borg
--remote-path borg1
--verbose
--progress
)
CREATE_COMMAND=(
"${BORG_COMMAND[@]}"
create
--stats
--one-file-system
--exclude-caches
--compression=auto,lzma
)
PRUNE_COMMAND=(
"${BORG_COMMAND[@]}"
prune
--list
--keep-daily=7
--keep-weekly=4
--keep-monthly=2
)
run_remote() {
if (( $# == 0 )); then
echo "No arguments given to run_remote" >&2
return 1
fi
ssh "$REMOTE_HOST" -- "cd" "$DESTINATION_LOCATION" "&&" "pwd" "&&" "${@}"
}

View File

@ -1,5 +0,0 @@
#!/data/data/com.termux/files/usr/bin/bash
ABSPATH="$(readlink -f "$BASH_SOURCE")"
cd "${ABSPATH%/*}" || { echo "Cannot change directory" >&2; exit 1; }
test -f ./env && source ./env || { echo "Unable to source user variables"; exit 1; }
ssh "$REMOTE_HOST" -- "$@"