-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandroid-bak
More file actions
executable file
·121 lines (102 loc) · 3.08 KB
/
android-bak
File metadata and controls
executable file
·121 lines (102 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
config() {
# run some commands on the remote before the download
# run_remote <command>
# run_remote "rm -r \"/sdcard/DCIM/.thumbnails\" || true"
# register paths to download
# register_path <full-path> <name>
register_path "/sdcard/DCIM" "DCIM"
register_path "/sdcard/Movies" "Movies"
register_path "/sdcard/Pictures" "Pictures"
register_path "/sdcard/Recording" "Recording"
register_path "/sdcard/recordings" "recordings"
register_path "/sdcard/Voice Recorder" "Voice_Recorder"
register_path "/sdcard/Samsung" "Samsung"
register_path "/sdcard/Binance" "Binance"
register_path "/sdcard/beam" "beam"
register_path "/data/data/com.termux/files/home/logdata" "logdata" # files from the logdata script
}
# ------------------------------------------------------------------------------
set -e
if [ -z "$1" ]; then
>&2 echo "alternative usage:"
>&2 echo " android-bak <host> [port (default 8022)] [backup location (default \$HOME)]"
>&2 echo
HOST=$(bcast-ip -l android)
PORT=8022
>&2 echo
else
HOST="$1"
PORT="${2:-8022}"
fi
LOCAL_DIR="${3:-$HOME}/android-bak"
DAY=$(date +%Y%m%d)
DAY_DIR="$LOCAL_DIR/$DAY"
echo "Backup directory: $DAY_DIR"
TEMP_REMOTE_SCRIPT=$(mktemp)
TEMP_LOCAL_SCRIPT=$(mktemp)
cleanup() {
rm "$TEMP_REMOTE_SCRIPT" "$TEMP_LOCAL_SCRIPT"
}
trap cleanup EXIT
cat > "$TEMP_REMOTE_SCRIPT" <<'EOF'
set -e
if ! hash rsync 2>/dev/null; then
echo "Installing rsync..."
pkg install rsync
fi
if ! hash filekeep 2>/dev/null; then
echo "Installing filekeep..."
if [ -d "filekeep" ]; then
git -C "filekeep" fetch -v
else
git clone "https://github.com/goncalomb/filekeep.git"
fi
pip3 install --upgrade "filekeep/"
fi
process_dir() {
if [ -d "$1" ]; then
echo "Processing '$1'..."
D=$(dirname -- "$1")
B=$(basename -- "$1")
cd "$D"
if [ ! -f "$B.filekeep.xml" ]; then
echo "Running filekeep..."
filekeep -d "$B" create
fi
fi
}
echo
EOF
download_dir() {
echo
if echo "if [ -d \"$1\" ]; then true; else false; fi" | ssh -T -p "$PORT" "$HOST" bash; then
echo "Downloading '$1'..."
mkdir -p "$2"
echo "$1"
rsync --info=progress2 --info=name0 -av -e "ssh -p $PORT" "$HOST:'$1'/" "$2"
rsync --info=progress2 --info=name0 -av -e "ssh -p $PORT" "$HOST:'$1.filekeep.xml'" "$2.filekeep.xml"
echo "Checking '$2'..."
D=$(dirname -- "$2")
B=$(basename -- "$2")
(
cd "$D"
filekeep -d "$B" verify && echo -e "\e[1;37;42mFILES OK\e[0m" || echo -e "\e[1;37;41mSOMETHING IS NOT RIGHT, CHECK THE LOG!\e[0m"
)
else
echo "'$1' not found on remote."
fi
}
run_remote() {
echo "$1" >> "$TEMP_REMOTE_SCRIPT"
}
register_path() {
echo "process_dir \"$1\"" >> "$TEMP_REMOTE_SCRIPT"
echo "download_dir \"$1\" \"\$DAY_DIR/$2_\$DAY\"" >> "$TEMP_LOCAL_SCRIPT"
}
# run config
config
# do remote work
cat "$TEMP_REMOTE_SCRIPT" | ssh -T -p "$PORT" "$HOST" bash
# do local work
. "$TEMP_LOCAL_SCRIPT"