Last active 1716253974

Revision d055dad2e3d4f280cd684e76bb060ed607e4fb0f

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