70 lines
2.0 KiB
Bash
Executable File
70 lines
2.0 KiB
Bash
Executable File
#!/data/data/com.termux/files/usr/bin/bash
|
|
# Initialize
|
|
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 in $PWD"; exit 1; }
|
|
|
|
# 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}'
|
|
}
|
|
|
|
# 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
|
|
}
|
|
|
|
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
|
|
|
|
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 nohup sh -c "cd \"$DESTINATION_LOCATION\"; $BORG_COMMAND \"$BORG_REPO::$HOSTNAME-$(uuidgen)\" \"$DESTINATION_LOCATION\"" || return 2
|
|
}
|
|
|
|
# Take wakelock so we don't fall asleep
|
|
termux-wake-lock
|
|
START_TIME="$(date +%s)"
|
|
|
|
# Perform the backup
|
|
backup
|
|
RESULT=$?
|
|
DURATION="$(time_diff "$(date +%s)" "$START_TIME" )"
|
|
|
|
echo "Completed $(date +%F-%T)"
|
|
echo "Duration: $DURATION"
|
|
|
|
if (( RESULT == 0 )); then
|
|
termux-notification --title "Backup completed successfully in $DURATION"
|
|
else
|
|
termux-notification --title "Backup failed in $DURATION"
|
|
fi
|
|
|
|
# Remove our wakelock
|
|
termux-wake-unlock
|