diff --git a/backup-android.sh b/backup-android.sh index 8428367..88b886d 100755 --- a/backup-android.sh +++ b/backup-android.sh @@ -1,5 +1,6 @@ #!/data/data/com.termux/files/usr/bin/bash # Initialize +# if [ -z "$STY" ]; then exec screen -m -S backup-android /bin/bash "$0"; fi ABSPATH="$(readlink -f "$BASH_SOURCE")" set -x cd "${ABSPATH%/*}" || { echo "Cannot change directory" >&2; exit 1; } @@ -8,48 +9,51 @@ test -f ./env && source ./env || { echo "Unable to source user variables in $PWD # From https://serverfault.com/a/799198 # Since Termux has no uuidgen command, we need to create one randomly function uuidgen() { - od -x /dev/urandom | head -1 | awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}' + od -x /dev/urandom | head -1 | awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}' } # From https://unix.stackexchange.com/a/27014 function time_diff { - # Find the time difference between the two arguments (in seconds) - local T="$(( "$1" - "$2" ))" - local D=$((T/60/60/24)) - local H=$((T/60/60%24)) - local M=$((T/60%60)) - local S=$((T%60)) - (( $D > 0 )) && printf '%dd ' $D - (( $H > 0 )) && printf '%dh ' $H - (( $M > 0 )) && printf '%dm ' $M - (( $D > 0 || $H > 0 || $M > 0 )) && printf 'and ' - printf '%ds\n' $S + # Find the time difference between the two arguments (in seconds) + local T="$(( "$1" - "$2" ))" + local D=$((T/60/60/24)) + local H=$((T/60/60%24)) + local M=$((T/60%60)) + local S=$((T%60)) + (( $D > 0 )) && printf '%dd ' $D + (( $H > 0 )) && printf '%dh ' $H + (( $M > 0 )) && printf '%dm ' $M + (( $D > 0 || $H > 0 || $M > 0 )) && printf 'and ' + printf '%ds\n' $S } function prune() { - test -z "$PRUNE_COMMAND" && return - ./run_remote.sh cd "$DESTINATION_LOCATION" "&&" $PRUNE_COMMAND "$BORG_REPO" + if [ -z "$PRUNE_COMMAND" ]; then + echo "Not pruning" >&2 + return + fi + run_remote "${PRUNE_COMMAND[@]}" "$BORG_REPO" } function backup() { - # First, we need to rsync our changes to the host - echo "Running rsync..." - local RETRIES=10 - while (( RETRIES > 0 )); do - # Try to rsync - $RSYNC_COMMAND "$SOURCE_LOCATION" "$REMOTE_HOST:$DESTINATION_LOCATION" && break - (( RETRIES-- )) - echo "rsync failed. Retrying $RETRIES more times. Sleeping 10s" - sleep 10s - done + # First, we need to rsync our changes to the host + echo "Running rsync..." + local RETRIES=10 + while (( RETRIES > 0 )); do + # Try to rsync + "${RSYNC_COMMAND[@]}" "$SOURCE_LOCATION" "$REMOTE_HOST:$DESTINATION_LOCATION" && break + (( RETRIES-- )) + echo "rsync failed. Retrying $RETRIES more times. Sleeping 10s" + sleep 10s + done - if (( RETRIES == 0 )); then - # We exceeded the retry limit. Fail - return 1 - fi + if (( RETRIES == 0 )); then + # We exceeded the retry limit. Fail + return 1 + fi - # Next, instruct the host to create a borg - echo "Running borg..." - ./run_remote.sh cd "$DESTINATION_LOCATION" "&&" pwd "&&" $BORG_COMMAND "$BORG_REPO::$HOSTNAME-$(uuidgen)" . || return 2 + # Next, instruct the host to create a borg + echo "Running borg..." + run_remote "${CREATE_COMMAND[@]}" "$BORG_REPO::$HOSTNAME-$(uuidgen)" . || return 2 } # Take wakelock so we don't fall asleep @@ -68,10 +72,12 @@ echo "Completed $(date +%F-%T)" echo "Duration: $DURATION" if (( RESULT == 0 )); then - termux-notification --title "Backup completed successfully in $DURATION" + termux-notification --title "Backup completed successfully in $DURATION" else - termux-notification --title "Backup failed in $DURATION" + termux-notification --title "Backup failed in $DURATION" fi # Remove our wakelock termux-wake-unlock +printf "^c to exit" +cat diff --git a/env.dist b/env.dist index 957a519..a8bdbb6 100644 --- a/env.dist +++ b/env.dist @@ -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/" HOSTNAME="android-phone" REMOTE_HOST="user@example.com" 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" -# 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" "&&" "${@}" +} diff --git a/run_remote.sh b/run_remote.sh deleted file mode 100755 index d829c81..0000000 --- a/run_remote.sh +++ /dev/null @@ -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" -- "$@"