Compare commits

...

18 Commits

Author SHA1 Message Date
758e27bd51 Rewrite backup-android to use bash arrays 2019-11-29 11:03:15 -05:00
a38b691403 Add commands 2019-09-15 10:55:36 -04:00
2f990bb643 Use actual exclude paths 2019-06-30 11:11:02 -04:00
04a02e91a5 Merge branch 'master' of https://gitea.austenwares.com/stonewareslord/borg-remote-android 2019-06-30 11:06:33 -04:00
ae0b90be8b Use HOSTNAME over LOCAL_HOSTNAME 2019-06-30 11:05:52 -04:00
fbcd73545e Add env.dist 2019-03-30 23:37:45 -04:00
c6d76d94c7 Add set -x 2018-11-26 19:31:35 -05:00
1c955a3b95 Merge branch 'master' of https://gitea.austenwares.com/stonewareslord/borg-remote-android 2018-11-26 19:31:20 -05:00
fd3a634f47 Add one line installation and better other installation 2018-09-27 15:59:39 -04:00
730cebc636 Merge branch 'master' of https://gitea.austenwares.com/stonewareslord/borg-remote-android 2018-09-24 17:44:41 -04:00
f44f4db3e1 Change to correct directory for backup 2018-09-24 17:44:06 -04:00
01ce48def4 Merge branch 'master' of https://gitea.austenwares.com/stonewareslord/borg-remote-android 2018-09-24 13:07:40 -04:00
9fc4d4843f Use local path for borg backups 2018-09-24 13:07:18 -04:00
d96bab1ebf Merge branch 'master' of https://gitea.austenwares.com/stonewareslord/borg-remote-android 2018-09-23 13:58:51 -04:00
1c9186aa1c Retry rsync up to 10 times 2018-09-23 13:58:41 -04:00
4dd579cc6f Merge branch 'master' of https://gitea.austenwares.com/stonewareslord/borg-remote-android 2018-09-23 13:42:01 -04:00
763ed42d0d Changes 2018-09-23 13:30:40 -04:00
d632ebedcd Add base files 2018-09-22 23:24:32 -04:00
4 changed files with 109 additions and 30 deletions

View File

@ -17,18 +17,25 @@ and it will notify you when complete.
Alternatively, you can add a Termux widget to your home screen (`./setup.sh` auto-installs in the `.shortcuts` directory), so backups can be made with a single tap from the home screen.
## Installation
Quick, 4 line installation:
1 line installation:
```bash
apt update
apt install -y git rsync openssh
curl -Ls https://linx.austenwares.com/selif/0182lycx.sh | bash
```
Manual 4 line installation:
```bash
# Install rsync/git/ssh only if they aren't already present
{ command -v rsync && command -v git && command -v ssh; } >/dev/null || { apt update && apt install -y git rsync openssh; }
git clone https://gitea.austenwares.com/stonewareslord/borg-remote-android
env bash borg-remote-android/setup.sh
```
Same installation as a one-liner
Manual one-liner:
```bash
apt update && apt install -y git rsync openssh && git clone https://gitea.austenwares.com/stonewareslord/borg-remote-android && env bash borg-remote-android/setup.sh
{ command -v rsync && command -v git && command -v ssh; } >/dev/null || { apt update && apt install -y git rsync openssh; } && git clone https://gitea.austenwares.com/stonewareslord/borg-remote-android && env bash borg-remote-android/setup.sh
```
[QR code installation (paste into Termux)](https://linx.austenwares.com/selif/w1x8uezq.png)
## Updating
The entire config is stored in `env`, which is in `.gitignore`, so running a simple `git pull` will update without losing custom changes.

View File

@ -1,38 +1,59 @@
#!/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; }
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}'
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() {
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..."
$RSYNC_COMMAND "$SOURCE_LOCATION" "$REMOTE_HOST:$DESTINATION_LOCATION" || return 1
# 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
# 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
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 "${CREATE_COMMAND[@]}" "$BORG_REPO::$HOSTNAME-$(uuidgen)" . || return 2
}
# Take wakelock so we don't fall asleep
@ -44,14 +65,19 @@ backup
RESULT=$?
DURATION="$(time_diff "$(date +%s)" "$START_TIME" )"
# Prune
prune
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

View File

@ -0,0 +1,51 @@
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_REPO="runner2:borg/$HOSTNAME"
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" -- "$@"