Last active 1716253974

android-backup.sh Raw
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=""
9source $HOME/backup/env.sh
10
11declare -a TERMUX_NOTIFICATIONS=()
12TERMUX_NOTIFICATION_ID="borgbackup-${HOSTNAME}"
13
14set -o pipefail
15
16cleanup() {
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.
36notify() {
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
50msg() {
51 echo "***" "$@"
52}
53
54info() {
55 msg "INFO:" "$@"
56 termux-toast -s "$*"
57}
58
59warn() {
60 msg "WARN:" "$@"
61 echo "Warning:" "$@" | \
62 notify 1 failure \
63 --title "borgbackup" \
64 --alert-once \
65 --priority low
66}
67
68err() {
69 msg "ERROR:" "$@"
70 echo "Error:" "$@" | \
71 notify 1 failure \
72 --title "borgbackup" \
73 --alert-once \
74 --priority high
75 exit 1
76}
77
78prepare() {
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
93backup() {
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
119prune() {
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
139MARKER_FILE=~/.borgbackup-"${HOSTNAME}-$(date +%Y-%m-%d)"
140if [ "${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
148fi
149
150if ! prepare; then
151 info "Server connectivity or charging status does not meet expectations, skipping backup."
152 exit 1
153fi
154rm -f ~/.borgbackup-"${HOSTNAME}"-*
155touch "$MARKER_FILE"
156
157trap "cleanup" EXIT
158termux-wake-lock
159notify 0 progress \
160 --alert-once \
161 --ongoing \
162 --priority low \
163 --title "borgbackup" \
164 --content "Running backup for ${HOSTNAME}"
165
166if ! backup; then
167 err "Backup failed, aborting!"
168fi
169if ! prune; then
170 warn "Pruning failed. Continuing anyway."
171fi
172
173info "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
2# https://artemis.sh/2022/06/22/how-i-use-borg.html
3source /backup/env.sh
4
5borg 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