-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path,backup-rsync-ultimate
executable file
·223 lines (190 loc) · 8.67 KB
/
,backup-rsync-ultimate
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
# NOTE: needs a README.md file on the usb to be detected, only when all already exists (personal choice)
# TODO: add sync to network (nfs)
# TODO: add pv for progress bar pipe
# | pv -ptrb --size "$(du -sb "$sync_lock" | awk '{print $1}')
# TODO: refactor finding mountpoints (use 'lsblk -J' piped with jq to query mountpoints)
# TODO: add a dry-run option
usb_mountpoint="/mnt/$(whoami)/ejsmain"
wizardry_sms="$HOME/vault/wizardry/scripts-magic-spells"
dotfiles_sms="$HOME/dotfiles/scripts-magic-spells"
vault="$HOME/vault"
dotfiles="$HOME/dotfiles"
sync_lock="/mnt/hdd/sync/lock"
reddit="$HOME/Sync/reddit"
pkse_desktop="$HOME/Pictures/pkse"
screenshots_desktop="$HOME/Pictures/ScreenShots"
password_store="$HOME/.password-store"
camera="/mnt/hdd/camera"
dlsu="/mnt/hdd/dlsu"
dirs_to_sync_usb=(
"$vault"
"$sync_lock"
"$password_store"
"$dotfiles"
"$reddit"
"$camera"
"$pkse_desktop"
"$screenshots_desktop"
"$wizardry_sms"
"$dlsu"
)
set -e
# ############################ #
# LOCAL SYNC #
# ############################ #
local_sync() {
notify-send "Starting LOCAL SYNC"
# sync scripts magic spells to dotfiles
echo "[ SYNCING ] scripts magic spells..."
if [ ! -d "$dotfiles_sms" ]; then
# if new then add
sudo rsync -auzv "$wizardry_sms" "$dotfiles_sms"
else
# just sync the files
sudo rsync -auzv --delete --progress "$wizardry_sms/" "$dotfiles_sms/"
fi
echo "[ SUCCESS ] Everything perfectly synced!"
notify-send "Backup Complete!" "All Synced!"
}
# ############################ #
# USB SYNC #
# ############################ #
usb_sync() {
# sync usb
echo "[ CHECKING ] is USB mounted?... "
# find command outputs string to stdout, so use -n or -z for conditionals
find_readme_file=$(find "$usb_mountpoint" -type f -name "README.md")
# HACK: since using: if [ ! -d "$usb_mountpoint" ]... is always true
# --> instead find the README.md file in the mountpoint (indicates USB is properly mounted)
if [[ -z "$find_readme_file" ]]; then
echo "[ SKIP ] USB mountpoint not found. Skipping..."
notify-send "SKIPPED" "USB mountpoint not found. Skipping..."
else
notify-send "DETECTED" "USB mountpoint detected."
notify-send "Starting USB SYNC"
for dir in "${dirs_to_sync_usb[@]}"; do
basename_dir=$(basename "$dir")
# if on new usb, copy directly
if [[ ! -d "$dir" ]]; then
echo "[ DETECTED ] NEW USB: Just get some coffee and backseat. This may take a while..."
if [[ "$dir" == "$sync_lock" ]]; then
echo "[ SYNCING USB ] NEW: Lock-encrypt directory..."
sudo rsync -auz "$sync_lock" "$usb_mountpoint"/Lock-encrypt
elif [[ "$dir" == "$vault" ]]; then
echo "[ SYNCING USB ] NEW: VAULT..."
sudo rsync -auzv --delete "$dir" "$usb_mountpoint"
elif [[ "$dir" == "$password_store" ]]; then
echo "[ SYNCING USB ] NEW: PASSWORD STORE..."
sudo rsync -auz --delete "$dir" "$usb_mountpoint"
elif [[ "$dir" == "$dotfiles" ]]; then
echo "[ SYNCING USB ] NEW: Dotfiles..."
sudo rsync -auzv --delete "$dir" "$usb_mountpoint"
elif [[ "$dir" == "$camera" ]]; then
echo "[ SYNCING USB ] NEW: camera pics..."
sudo rsync -auz "$dir" "$usb_mountpoint"
elif [[ "$dir" == "$pkse_desktop" ]]; then
echo "[ SYNCING USB ] NEW: exquisite pics..."
sudo rsync -auz "$dir" "$usb_mountpoint"
elif [[ "$dir" == "$screenshots_desktop" ]]; then
echo "[ SYNCING USB ] NEW: personal screenshots..."
sudo rsync -auz "$dir" "$usb_mountpoint"
elif [[ "$dir" == "$reddit" ]]; then
echo "[ SYNCING USB ] NEW: personal reddit..."
sudo rsync -auzv --delete "$dir" "$usb_mountpoint"
elif [[ "$dir" == "$wizardry_sms" ]]; then
echo "[ SYNCING USB ] NEW: scripts magic spells..."
sudo rsync -auzv --delete "$wizardry_sms" "$usb_mountpoint"/scripts-magic-spells
else
echo "[ SYNCING USB ] NEW: $basename_dir..."
sudo rsync -auzv "$dir" "$usb_mountpoint"
fi
else
# if have README.md file, then usb_mountpoint is mounted properly
if [[ -n "$find_readme_file" ]]; then
# if on usb with existing dirs, then just sync (add "/")
if [[ "$dir" == "$sync_lock" ]]; then
echo "[ SUCCESS ] USB is mounted, now syncing..."
echo "[ SYNCING USB ] Lock-encrypt directory..."
# finds and syncs files added "newer than 3 days" (or files in the last 3 days) with -mtime -3
rsync -auz --progress --files-from=<(find "$sync_lock/" -mtime -30 -type f -exec basename {} \;) "$sync_lock/" "$usb_mountpoint"/Lock-encrypt/ # | pv -ptrb --size "$(du -sb "$sync_lock" | awk '{print $1}'),back
elif [[ "$dir" == "$vault" ]]; then
echo "[ SYNCING USB ] VAULT..."
sudo rsync -auzv --delete "$dir/" "$usb_mountpoint/$basename_dir/"
elif [[ "$dir" == "$password_store" ]]; then
echo "[ SYNCING USB ] PASSWORD STORE..."
sudo rsync -auz --delete "$dir/" "$usb_mountpoint/$basename_dir/"
elif [[ "$dir" == "$dotfiles" ]]; then
echo "[ SYNCING USB ] Dotfiles..."
sudo rsync -auzv --delete "$dir/" "$usb_mountpoint/$basename_dir/"
elif [[ "$dir" == "$camera" ]]; then
echo "[ SYNCING USB ] camera pics..."
sudo rsync -auz "$dir/" "$usb_mountpoint/$basename_dir/"
elif [[ "$dir" == "$pkse_desktop" ]]; then
echo "[ SYNCING USB ] exquisite pics..."
sudo rsync -auz "$dir/" "$usb_mountpoint/$basename_dir/"
elif [[ "$dir" == "$wizardry_sms" ]]; then
echo "[ SYNCING USB ] Scripts / Magic Spells..."
sudo rsync -auzv --delete "$wizardry_sms/" "$usb_mountpoint"/scripts-magic-spells/
elif [[ "$dir" == "$screenshots_desktop" ]]; then
echo "[ SYNCING USB ] exquisite screenshots..."
sudo rsync -auz "$dir/" "$usb_mountpoint/$basename_dir/"
elif [[ "$dir" == "$reddit" ]]; then
echo "[ SYNCING USB ] personal reddit..."
sudo rsync -auzv "$dir/" "$usb_mountpoint/$basename_dir/"
else
echo "[ SYNCING USB ] $basename_dir..."
sudo rsync -auzv "$dir/" "$usb_mountpoint/$basename_dir/"
fi
fi
fi
done
echo "[ SUCCESS ] Everything perfectly synced!"
notify-send "Backup Complete!" "All Synced!"
fi
}
if [ "$#" -eq 0 ]; then
echo "Running this script with no args is equal to running it with -a, -all, all option (default behavior)"
read -rp "Do you want to continue? (y/n) " yn
case $yn in
[Yy]*)
local_sync
usb_sync
;;
[Nn]*) echo "Aborting..." ;;
*) printf " Error: only write 'y' or 'n'\n\n" ;;
esac
else
case "$1" in
-a | --all | all)
notify-send "Backup Started!" "Syncing everything..."
notify-send "Get some coffee" "Automation is peak."
local_sync
usb_sync
;;
-c | --check | check | -n | --dry-run | dry-run)
# TODO: add dry-run, adding flag for rsync -n
notify-send "Backup DRY RUN!" "Checking everything..."
;;
-l | --local | local)
notify-send "Backup Started!" "Syncing everything..."
notify-send "Get some coffee" "Automation is peak."
local_sync
;;
-u | --usb | usb)
notify-send "Backup Started!" "Syncing everything..."
notify-send "Get some coffee" "Automation is peak."
usb_sync
;;
-h | --help | help)
echo -e "Updates [options]
Options:
-a, --all, all Sync to ALL backups (default)
-l, --local, local Start LOCAL-ONLY sync
-u, --usb, usb Start USB sync\n"
;;
*)
echo "Invalid option: Run -h, --help, help to see available flags"
;;
esac
fi