android-backup.sh
· 3.9 KiB · Bash
Raw
#!/data/data/com.termux/files/usr/bin/bash
# https://neverpanic.de/blog/2022/01/25/backing-up-your-android-phone-with-borgbackup/
#export HOST=""
#export HOSTNAME=""
#export TARGET="borg@${HOST}:/backup/${HOSTNAME}"
#export BORG_PASSPHRASE=''
#export BORG_RSH=""
source $HOME/backup/env.sh
declare -a TERMUX_NOTIFICATIONS=()
TERMUX_NOTIFICATION_ID="borgbackup-${HOSTNAME}"
set -o pipefail
cleanup() {
for notification in "${TERMUX_NOTIFICATIONS[@]}"; do
termux-notification-remove "$notification"
done
termux-wake-unlock
}
##
# Send a notification to the user.
#
# Usage: echo "message" | notify persist identifier [options...]
#
# If persist is 0, the notification will be removed when the script exits.
# Otherwise, it will be kept (e.g. for warning or error messages).
#
# The identifier can be used to overwrite a previous notification with the same
# identifier. This can be useful for progress messages.
#
# Further options are those supported by termux-notification. The message must
# be passed on stdin.
notify() {
local persist=$1
shift
local id="$1"
shift
local -a args=("--group" "${TERMUX_NOTIFICATION_ID}" "--id" "$id")
if termux-notification "${args[@]}" "$@"; then
if [ "$persist" -eq 0 ]; then
TERMUX_NOTIFICATIONS+=("$id")
fi
fi
}
msg() {
echo "***" "$@"
}
info() {
msg "INFO:" "$@"
termux-toast -s "$*"
}
warn() {
msg "WARN:" "$@"
echo "Warning:" "$@" | \
notify 1 failure \
--title "borgbackup" \
--alert-once \
--priority low
}
err() {
msg "ERROR:" "$@"
echo "Error:" "$@" | \
notify 1 failure \
--title "borgbackup" \
--alert-once \
--priority high
exit 1
}
prepare() {
if ! termux-battery-status | grep "status" | grep -qE '"(CHARGING|FULL)"'; then
warn "Not charging, not performing backup"
return 1
fi
if ! termux-wifi-connectioninfo | grep "supplicant_state" | grep -q "COMPLETED"; then
warn "WiFi not connected, not performing backup"
return 1
fi
if ! ping -w 10 -c 3 "$HOST" >/dev/null; then
warn "Failed to ping target $HOST"
return 1
fi
}
backup() {
local -a flags=()
# enable interactive output
if [ -t 0 ] && [ -t 1 ]; then
flags+=('--stats' '--progress' '--list')
fi
info "Starting backup"
ionice -c 3 \
nice -n20 \
borg create \
--noatime \
--compression='lz4' \
--exclude-caches \
--exclude='fm:/storage/emulated/0/*/.thumbnails' \
--exclude='pp:/data/data/com.termux/files/home/.config/borg' \
--exclude='pp:/storage/emulated/0/Android/data' \
--exclude='pp:/storage/emulated/0/DCIM/Camera' \
--exclude='pp:/storage/emulated/0/Android/obb' \
"${flags[@]}" \
"${TARGET}::${HOSTNAME}-{utcnow:%Y-%m-%dT%H:%M:%S}" \
/storage/emulated/0/ \
/data/data/com.termux/files/home
}
prune() {
local -a flags=()
# enable interactive output
if [ -t 0 ] && [ -t 1 ]; then
flags+=('--stats' '--list')
fi
info "Pruning old backups..."
borg prune \
--prefix="${HOSTNAME}-" \
--keep-within=14d \
--keep-daily=31 \
--keep-weekly=$((6 * 4)) \
--keep-monthly=$(( 2 * 12 )) \
"${flags[@]}" \
"${TARGET}"
}
# Run once per day, unless BORGBACKUP_FORCE=1
MARKER_FILE=~/.borgbackup-"${HOSTNAME}-$(date +%Y-%m-%d)"
if [ "${BORGBACKUP_FORCE:-0}" -eq 0 ]; then
if [ "$(date +%H)" -lt 4 ]; then
echo "Backup not yet due, waiting..."
exit 0
elif [ -f "$MARKER_FILE" ]; then
echo "Backup already ran today"
exit 0
fi
fi
if ! prepare; then
info "Server connectivity or charging status does not meet expectations, skipping backup."
exit 1
fi
rm -f ~/.borgbackup-"${HOSTNAME}"-*
touch "$MARKER_FILE"
trap "cleanup" EXIT
termux-wake-lock
notify 0 progress \
--alert-once \
--ongoing \
--priority low \
--title "borgbackup" \
--content "Running backup for ${HOSTNAME}"
if ! backup; then
err "Backup failed, aborting!"
fi
if ! prune; then
warn "Pruning failed. Continuing anyway."
fi
info "Backup finished successfully"
| 1 | #!/data/data/com.termux/files/usr/bin/bash |
| 2 | # https://neverpanic.de/blog/2022/01/25/backing-up-your-android-phone-with-borgbackup/ |
| 3 | |
| 4 | #export HOST="" |
| 5 | #export HOSTNAME="" |
| 6 | #export TARGET="borg@${HOST}:/backup/${HOSTNAME}" |
| 7 | #export BORG_PASSPHRASE='' |
| 8 | #export BORG_RSH="" |
| 9 | source $HOME/backup/env.sh |
| 10 | |
| 11 | declare -a TERMUX_NOTIFICATIONS=() |
| 12 | TERMUX_NOTIFICATION_ID="borgbackup-${HOSTNAME}" |
| 13 | |
| 14 | set -o pipefail |
| 15 | |
| 16 | cleanup() { |
| 17 | for notification in "${TERMUX_NOTIFICATIONS[@]}"; do |
| 18 | termux-notification-remove "$notification" |
| 19 | done |
| 20 | termux-wake-unlock |
| 21 | } |
| 22 | |
| 23 | ## |
| 24 | # Send a notification to the user. |
| 25 | # |
| 26 | # Usage: echo "message" | notify persist identifier [options...] |
| 27 | # |
| 28 | # If persist is 0, the notification will be removed when the script exits. |
| 29 | # Otherwise, it will be kept (e.g. for warning or error messages). |
| 30 | # |
| 31 | # The identifier can be used to overwrite a previous notification with the same |
| 32 | # identifier. This can be useful for progress messages. |
| 33 | # |
| 34 | # Further options are those supported by termux-notification. The message must |
| 35 | # be passed on stdin. |
| 36 | notify() { |
| 37 | local persist=$1 |
| 38 | shift |
| 39 | local id="$1" |
| 40 | shift |
| 41 | local -a args=("--group" "${TERMUX_NOTIFICATION_ID}" "--id" "$id") |
| 42 | |
| 43 | if termux-notification "${args[@]}" "$@"; then |
| 44 | if [ "$persist" -eq 0 ]; then |
| 45 | TERMUX_NOTIFICATIONS+=("$id") |
| 46 | fi |
| 47 | fi |
| 48 | } |
| 49 | |
| 50 | msg() { |
| 51 | echo "***" "$@" |
| 52 | } |
| 53 | |
| 54 | info() { |
| 55 | msg "INFO:" "$@" |
| 56 | termux-toast -s "$*" |
| 57 | } |
| 58 | |
| 59 | warn() { |
| 60 | msg "WARN:" "$@" |
| 61 | echo "Warning:" "$@" | \ |
| 62 | notify 1 failure \ |
| 63 | --title "borgbackup" \ |
| 64 | --alert-once \ |
| 65 | --priority low |
| 66 | } |
| 67 | |
| 68 | err() { |
| 69 | msg "ERROR:" "$@" |
| 70 | echo "Error:" "$@" | \ |
| 71 | notify 1 failure \ |
| 72 | --title "borgbackup" \ |
| 73 | --alert-once \ |
| 74 | --priority high |
| 75 | exit 1 |
| 76 | } |
| 77 | |
| 78 | prepare() { |
| 79 | if ! termux-battery-status | grep "status" | grep -qE '"(CHARGING|FULL)"'; then |
| 80 | warn "Not charging, not performing backup" |
| 81 | return 1 |
| 82 | fi |
| 83 | if ! termux-wifi-connectioninfo | grep "supplicant_state" | grep -q "COMPLETED"; then |
| 84 | warn "WiFi not connected, not performing backup" |
| 85 | return 1 |
| 86 | fi |
| 87 | if ! ping -w 10 -c 3 "$HOST" >/dev/null; then |
| 88 | warn "Failed to ping target $HOST" |
| 89 | return 1 |
| 90 | fi |
| 91 | } |
| 92 | |
| 93 | backup() { |
| 94 | local -a flags=() |
| 95 | |
| 96 | # enable interactive output |
| 97 | if [ -t 0 ] && [ -t 1 ]; then |
| 98 | flags+=('--stats' '--progress' '--list') |
| 99 | fi |
| 100 | |
| 101 | info "Starting backup" |
| 102 | ionice -c 3 \ |
| 103 | nice -n20 \ |
| 104 | borg create \ |
| 105 | --noatime \ |
| 106 | --compression='lz4' \ |
| 107 | --exclude-caches \ |
| 108 | --exclude='fm:/storage/emulated/0/*/.thumbnails' \ |
| 109 | --exclude='pp:/data/data/com.termux/files/home/.config/borg' \ |
| 110 | --exclude='pp:/storage/emulated/0/Android/data' \ |
| 111 | --exclude='pp:/storage/emulated/0/DCIM/Camera' \ |
| 112 | --exclude='pp:/storage/emulated/0/Android/obb' \ |
| 113 | "${flags[@]}" \ |
| 114 | "${TARGET}::${HOSTNAME}-{utcnow:%Y-%m-%dT%H:%M:%S}" \ |
| 115 | /storage/emulated/0/ \ |
| 116 | /data/data/com.termux/files/home |
| 117 | } |
| 118 | |
| 119 | prune() { |
| 120 | local -a flags=() |
| 121 | |
| 122 | # enable interactive output |
| 123 | if [ -t 0 ] && [ -t 1 ]; then |
| 124 | flags+=('--stats' '--list') |
| 125 | fi |
| 126 | |
| 127 | info "Pruning old backups..." |
| 128 | borg prune \ |
| 129 | --prefix="${HOSTNAME}-" \ |
| 130 | --keep-within=14d \ |
| 131 | --keep-daily=31 \ |
| 132 | --keep-weekly=$((6 * 4)) \ |
| 133 | --keep-monthly=$(( 2 * 12 )) \ |
| 134 | "${flags[@]}" \ |
| 135 | "${TARGET}" |
| 136 | } |
| 137 | |
| 138 | # Run once per day, unless BORGBACKUP_FORCE=1 |
| 139 | MARKER_FILE=~/.borgbackup-"${HOSTNAME}-$(date +%Y-%m-%d)" |
| 140 | if [ "${BORGBACKUP_FORCE:-0}" -eq 0 ]; then |
| 141 | if [ "$(date +%H)" -lt 4 ]; then |
| 142 | echo "Backup not yet due, waiting..." |
| 143 | exit 0 |
| 144 | elif [ -f "$MARKER_FILE" ]; then |
| 145 | echo "Backup already ran today" |
| 146 | exit 0 |
| 147 | fi |
| 148 | fi |
| 149 | |
| 150 | if ! prepare; then |
| 151 | info "Server connectivity or charging status does not meet expectations, skipping backup." |
| 152 | exit 1 |
| 153 | fi |
| 154 | rm -f ~/.borgbackup-"${HOSTNAME}"-* |
| 155 | touch "$MARKER_FILE" |
| 156 | |
| 157 | trap "cleanup" EXIT |
| 158 | termux-wake-lock |
| 159 | notify 0 progress \ |
| 160 | --alert-once \ |
| 161 | --ongoing \ |
| 162 | --priority low \ |
| 163 | --title "borgbackup" \ |
| 164 | --content "Running backup for ${HOSTNAME}" |
| 165 | |
| 166 | if ! backup; then |
| 167 | err "Backup failed, aborting!" |
| 168 | fi |
| 169 | if ! prune; then |
| 170 | warn "Pruning failed. Continuing anyway." |
| 171 | fi |
| 172 | |
| 173 | info "Backup finished successfully" |
push.sh
· 354 B · Bash
Raw
#!/bin/bash
export PATH=/usr/bin
inp=bak
[ -n "$1" ] && inp="$1"
[ -n "$2" ] && param="$2"
sudo find /mnt/EXT4DATA/$inp -type f -mmin -10080 -printf '%P\n' | sudo rclone --config /home/aly/.config/rclone/rclone.conf copy /mnt/EXT4DATA/$inp/ storjs3:ads-$inp/ --fast-list=true --bwlimit=4M --transfers=4 --files-from=- --log-file=/tmp/uploadbak.log $param
| 1 | #!/bin/bash |
| 2 | export PATH=/usr/bin |
| 3 | inp=bak |
| 4 | [ -n "$1" ] && inp="$1" |
| 5 | [ -n "$2" ] && param="$2" |
| 6 | sudo find /mnt/EXT4DATA/$inp -type f -mmin -10080 -printf '%P\n' | sudo rclone --config /home/aly/.config/rclone/rclone.conf copy /mnt/EXT4DATA/$inp/ storjs3:ads-$inp/ --fast-list=true --bwlimit=4M --transfers=4 --files-from=- --log-file=/tmp/uploadbak.log $param |
wsl-backup.sh
· 2.4 KiB · Bash
Raw
#!/bin/bash
# https://artemis.sh/2022/06/22/how-i-use-borg.html
source /backup/env.sh
borg create "$@" \
--stats \
--one-file-system \
--compression auto,zstd,3 \
--exclude /backup \
--exclude /root/.cache \
--exclude /opt/nvidia \
--exclude **/cuda** \
--exclude /var/cache \
--exclude /usr/share \
--exclude **/.pyenv \
--exclude **/.cache \
--exclude **/cache \
--exclude **/Cache \
--exclude **/Code\ Cache \
--exclude **/ccache \
--exclude **/.git \
--exclude **/.env \
--exclude **/.conda \
--exclude **/miniconda3 \
--exclude **/.docker \
--exclude **/.nuget \
--exclude **/User\ Data \
--exclude **/QtWebEngine/Default \
--exclude **/Service\ Worker \
--exclude **/FL\ Studio \
--exclude **/modules \
--exclude **/Lib \
--exclude **/site-packages \
--exclude **/node_modules \
--exclude **/Persistent \
--exclude **/AppData/Roaming/Cakewalk \
--exclude **/AppData/Local/Temp \
--exclude **/Packages \
--exclude **/Package\ Cache \
--exclude /tmp \
--exclude /usr/lib \
--exclude **/MediaCover \
--exclude **venv \
--exclude /home/**/.local/lib \
--exclude **/virtualenv \
--exclude **/virtualenvs \
--exclude **/Temp \
"$REPO::{hostname}-{now:%Y-%m-%d}" \
/ \
/mnt/c/Users \
/mnt/c/ProgramData
| 1 | #!/bin/bash |
| 2 | # https://artemis.sh/2022/06/22/how-i-use-borg.html |
| 3 | source /backup/env.sh |
| 4 | |
| 5 | borg create "$@" \ |
| 6 | --stats \ |
| 7 | --one-file-system \ |
| 8 | --compression auto,zstd,3 \ |
| 9 | --exclude /backup \ |
| 10 | --exclude /root/.cache \ |
| 11 | --exclude /opt/nvidia \ |
| 12 | --exclude **/cuda** \ |
| 13 | --exclude /var/cache \ |
| 14 | --exclude /usr/share \ |
| 15 | --exclude **/.pyenv \ |
| 16 | --exclude **/.cache \ |
| 17 | --exclude **/cache \ |
| 18 | --exclude **/Cache \ |
| 19 | --exclude **/Code\ Cache \ |
| 20 | --exclude **/ccache \ |
| 21 | --exclude **/.git \ |
| 22 | --exclude **/.env \ |
| 23 | --exclude **/.conda \ |
| 24 | --exclude **/miniconda3 \ |
| 25 | --exclude **/.docker \ |
| 26 | --exclude **/.nuget \ |
| 27 | --exclude **/User\ Data \ |
| 28 | --exclude **/QtWebEngine/Default \ |
| 29 | --exclude **/Service\ Worker \ |
| 30 | --exclude **/FL\ Studio \ |
| 31 | --exclude **/modules \ |
| 32 | --exclude **/Lib \ |
| 33 | --exclude **/site-packages \ |
| 34 | --exclude **/node_modules \ |
| 35 | --exclude **/Persistent \ |
| 36 | --exclude **/AppData/Roaming/Cakewalk \ |
| 37 | --exclude **/AppData/Local/Temp \ |
| 38 | --exclude **/Packages \ |
| 39 | --exclude **/Package\ Cache \ |
| 40 | --exclude /tmp \ |
| 41 | --exclude /usr/lib \ |
| 42 | --exclude **/MediaCover \ |
| 43 | --exclude **venv \ |
| 44 | --exclude /home/**/.local/lib \ |
| 45 | --exclude **/virtualenv \ |
| 46 | --exclude **/virtualenvs \ |
| 47 | --exclude **/Temp \ |
| 48 | "$REPO::{hostname}-{now:%Y-%m-%d}" \ |
| 49 | / \ |
| 50 | /mnt/c/Users \ |
| 51 | /mnt/c/ProgramData |