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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#!/bin/sh
# local backup
# Dotfiles are shared across machines, so no fixed device or path names:
# the target is whichever mounted /mnt/*/backup directory exists; failing
# that, a closed LUKS partition is opened and mounted under /mnt, named
# after its filesystem label (mount_luks sets $luks_mount accordingly).
luks_mount=""
backup_path=""
dot_path="${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}"
git_path="$HOME/Private/repos"
pass_path="${PASSWORD_STORE_DIR:-${XDG_DATA_HOME:-${HOME}/.local/share}/.password-store}/exported_keys"
suck_path="${XDG_SOURCES_HOME:-${HOME}/.local/src}/suckless"
user_home=$(eval echo ~"$USER")
# targets
bash_path="${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}/ar/.config/bash"
shell_path="${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}/ar/.config/shell"
vim_path="${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}/ar/.config/vim"
thesiah_path="${THESIAH_WWW:-${HOME}/Private/repos/THESIAH}/public"
lf_path="${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}/ar/.config/lf"
usage() {
echo "Synchronize files and save them in backup path."
echo ""
echo "Usage: ${0##*/} [OPTIONS]"
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo " -r, --root Sync root files only"
echo ""
echo "Example:"
echo " ${0##*/} # Sync all files to backup path"
echo " ${0##*/} --root # Sync root files only"
exit 0
}
error() {
printf "%s\n" "$1" >&2
exit 1
}
# First existing backup/ directory on a mounted drive under /mnt wins.
find_backup_path() {
for _dir in /mnt/*/backup; do
[ -d "$_dir" ] || continue
mountpoint -q "${_dir%/backup}" 2>/dev/null || continue
printf '%s' "$_dir"
return 0
done
return 1
}
# Closed LUKS partition = crypto_LUKS fstype without an opened crypt child.
# Device names differ across machines and boots, so detect by type instead
# of hardcoding paths. Prints the largest candidate, if any.
find_closed_luks() {
lsblk -prbno PATH,FSTYPE,SIZE 2>/dev/null |
awk '$2 == "crypto_LUKS" { print $3, $1 }' |
sort -rn |
while read -r _size _dev; do
lsblk -rno TYPE "$_dev" 2>/dev/null | grep -qx "crypt" && continue
printf '%s\n' "$_dev"
break
done
}
# Open the LUKS partition and mount it under /mnt. The mount point follows
# the inner filesystem's label (same convention as the other /mnt/<label>
# drives); "second" is only a fallback for unlabeled filesystems.
mount_luks() {
_device="$1"
${TERMINAL:-st} -n floatterm -g 60x1 -e sudo cryptsetup open "$_device" "second"
[ -e /dev/mapper/second ] || return 1
_label="$(lsblk -rno LABEL /dev/mapper/second 2>/dev/null | head -n 1)"
luks_mount="/mnt/${_label:-second}"
sudo -A mkdir -p "$luks_mount"
sudo -A mount "/dev/mapper/second" "$luks_mount" -o uid="$(id -u)",gid="$(id -g)" 2>/dev/null || sudo -A mount "/dev/mapper/second" "$luks_mount"
}
# Using a loop over space-separated strings instead of an array
sync_files() {
# top-level targets go directly under the backup path
for source in "$git_path" "$pass_path"; do
rsync -vrazPlu --exclude=".music.txt" --delete "$source" "$backup_path/" >/dev/null 2>&1 || {
echo "Failed to sync $(basename "$source")"
}
done
# dotfiles and suckless are stored under repos/
[ -d "$backup_path/repos" ] || mkdir -p "$backup_path/repos"
for source in "$dot_path" "$suck_path"; do
rsync -vrazPlu --exclude=".music.txt" --delete "$source" "$backup_path/repos/" >/dev/null 2>&1 || {
echo "Failed to sync $(basename "$source")"
}
done
}
sync_root() {
# clean targets
sudo rm -rf /root/.config /root/.bash_history /root/.local/share/history
sudo mkdir -p /root/.config/bash /root/.config/lf /root/.config/shell /root/.config/vim /root/.local/bin /root/.local/share/history/vim_history /root/.local/state
# Root configuration synchronization on local system
sudo rsync -vrazPlu --delete "$vim_path/vimrc" "/root/.config/vim/" >/dev/null 2>&1
sudo rsync -vrazPlu --delete "$lf_path" "/root/.config/" >/dev/null 2>&1
sudo mv -f "/root/.config/lf/rooticons" "/root/.config/lf/icons" >/dev/null 2>&1
sudo rsync -vrazPlu --delete "$bash_path" "/root/.config/" >/dev/null 2>&1
sudo rsync -vrazPlu --delete "$shell_path/inputrc" "/root/.config/shell/" >/dev/null 2>&1
# load shortcuts
shortcuts >/dev/null 2>&1
# Modify root's Bash and LF configuration to include user-specific settings
echo "[ -f \"$user_home/.config/shell/shortcutrc\" ] && source \"$user_home/.config/shell/shortcutrc\"" | sudo tee -a /root/.config/bash/bashrc >/dev/null 2>&1
echo "[ -f \"$user_home/.config/shell/zshnameddirrc\" ] && source \"$user_home/.config/shell/zshnameddirrc\"" | sudo tee -a /root/.config/bash/bashrc >/dev/null 2>&1
sudo sed -i "s|source[[:space:]]*\"\?~/.config/lf/shortcutrc\"\?|source \"$user_home/.config/lf/shortcutrc\"|" /root/.config/lf/lfrc >/dev/null 2>&1
sudo grep -q "source \"\?/root/.config/lf/rootshortcutrc\"\?" /root/.config/lf/lfrc ||
sudo sed -i "\|source \"\?$user_home/.config/lf/shortcutrc\"\?|a source \"/root/.config/lf/rootshortcutrc\"" /root/.config/lf/lfrc
# Final ownership and link adjustments
sudo chown -R root:root /root/.config/ >/dev/null 2>&1
sudo ln -sf /root/.config/bash/bashrc /root/.bashrc >/dev/null 2>&1
sudo ln -sf /root/.config/bash/bash_profile /root/.bash_profile >/dev/null 2>&1
sudo ln -sf /root/.config/shell/inputrc /root/.inputrc >/dev/null 2>&1
sudo ln -sf /root/.config/vim/vimrc /root/.vimrc >/dev/null 2>&1
}
sync_server() {
# clean targets
ssh "$THESIAH_SERVER" "rm -rf /root/.config /var/www/thesiah"
ssh "$THESIAH_SERVER" "mkdir -p /root/.config/bash /root/.config/shell /root/.config/vim /root/.local/bin /root/.local/share /root/.local/state /var/www/thesiah"
# Sync operations with explicit error checking
cd "$THESIAH_WWW" || return 1
[ -d "$thesiah_path" ] || hugo --cleanDestinationDir
rsync -vrazPlu --delete "$thesiah_path/" "$THESIAH_SERVER:/var/www/thesiah/" >/dev/null 2>&1 && rm -rf "$thesiah_path"
rsync -vrazPlu --delete "$vim_path/vimrc" "$THESIAH_SERVER:/root/.config/vim/" >/dev/null 2>&1
rsync -vrazPlu --delete "$shell_path/inputrc" "$THESIAH_SERVER:/root/.config/shell/" >/dev/null 2>&1
sudo cp /root/.config/shell/rootshortcutrc ~/.cache/
sudo chown -R "$USER":wheel ~/.cache/rootshortcutrc
rsync -vrazPlu --remove-source-files "$HOME/.cache/rootshortcutrc" "$THESIAH_SERVER:/root/.config/shell/" >/dev/null 2>&1
# Adding custom shortcuts to root's shell configuration on the remote system
ssh "$THESIAH_SERVER" "echo 'web=\"cd /var/www && ls -A\" \\' >> /root/.config/shell/rootshortcutrc"
ssh "$THESIAH_SERVER" "echo 'wen=\"cd /var/www/nextcloud && ls -A\" \\' >> /root/.config/shell/rootshortcutrc"
ssh "$THESIAH_SERVER" "echo 'wep=\"cd /var/www/prosody && ls -A\" \\' >> /root/.config/shell/rootshortcutrc"
ssh "$THESIAH_SERVER" "echo 'wet=\"cd /var/www/thesiah && ls -A\" \\' >> /root/.config/shell/rootshortcutrc"
ssh "$THESIAH_SERVER" "echo 'gng=\"cd /etc/nginx/sites-available && ls -A\" \\' >> /root/.config/shell/rootshortcutrc"
# Sync Bash configuration
rsync -vrazPlu --delete "$bash_path" "$THESIAH_SERVER:/root/.config/" >/dev/null 2>&1
ssh "$THESIAH_SERVER" "chown -R root:root /var/www/thesiah"
ssh "$THESIAH_SERVER" "chown -R root:root /root/.config/"
ssh "$THESIAH_SERVER" "ln -sf /root/.config/bash/bash_profile /root/.profile"
ssh "$THESIAH_SERVER" "source /root/.profile"
# Sync for Git
ssh "$THESIAH_SERVER" "cp -r /root/.config /var/www/git/"
ssh "$THESIAH_SERVER" "chown -R git:git /var/www/git/.config/"
ssh "$THESIAH_GIT" "ln -sf /var/www/git/.config/bash/bash_profile /var/www/git/.profile"
ssh "$THESIAH_GIT" "source /var/www/git/.profile"
}
sync_nextcloud() {
base="$(basename "$backup_path")"
parent="$(dirname "$backup_path")"
tmpdir="$(mktemp -d)"
cd "$tmpdir" || return 1
tar -C "$parent" -zcf "$base".tar.gz "$base" >/dev/null 2>&1
rsync -vrazPlu --delete "$tmpdir/$base".tar.gz "$THESIAH_SERVER:/var/www/nextcloud/data/si@thesiah.xyz/files/backup/" >/dev/null 2>&1
ssh "$THESIAH_SERVER" "chown -R www-data:www-data /var/www/nextcloud/data/si@thesiah.xyz/files/backup" >/dev/null 2>&1
ssh "$THESIAH_SERVER" "cd /var/www/nextcloud && sudo -u www-data ./occ files:scan --path="/si@thesiah.xyz/files"" >/dev/null 2>&1
rm -r "$tmpdir"
}
handle_long_option() {
case $1 in
help)
usage
;;
root)
echo "Sync root files..."
sync_root && echo "Success to sync root!" && echo "Done!" || error "Failed to back up root"
exit 0
;;
*)
error "Unknown option: --$1"
;;
esac
}
process_options() {
while [ $# -gt 0 ]; do
case $1 in
-h | --help)
usage
;;
-r | --root)
echo "Sync root files..."
sync_root && echo "Success to sync root!" && echo "Done!" || error "Failed to back up root"
exit 0
;;
--*)
handle_long_option "${1#--}"
;;
-*)
error "Unknown option: $1"
;;
*)
break
;;
esac
shift
done
}
# Start script
echo "Backup starts..."
process_options "$@"
# Main script logic
# Resolve the backup target: an existing /mnt/*/backup directory first,
# then a closed LUKS partition as fallback.
backup_available=false
if backup_path="$(find_backup_path)"; then
backup_available=true
echo "Backup target found: $backup_path"
else
echo "No /mnt/*/backup directory found"
luks_device="$(find_closed_luks)"
if [ -n "$luks_device" ]; then
echo "Attempting to mount LUKS drive $luks_device..."
if mount_luks "$luks_device" && mountpoint -q "$luks_mount" 2>/dev/null; then
backup_path="$luks_mount/backup"
backup_available=true
echo "Success to mount luks drive!"
else
echo "Backup drive not available. Skipping file backup..."
fi
else
echo "No closed LUKS partition found either. Skipping file backup..."
echo "(mark a drive as backup target once: mkdir /mnt/<drive>/backup)"
fi
fi
# Sync home files only if backup drive is available
if [ "$backup_available" = true ]; then
[ -d "$backup_path" ] || sudo mkdir -p "$backup_path"
echo "Sync home files..." && sync_files && echo "Success to sync files!" || echo "Warning: Failed to sync some files"
else
echo "Skipping home files sync (no backup drive)"
fi
echo "Sync root files..." && sync_root && echo "Success to sync root!" || echo "Warning: Failed to sync root"
# Sync server files only if the server is reachable without prompting;
# otherwise each of the many ssh/rsync calls in sync_server would ask for
# a password in turn.
server_available=false
if [ -n "$THESIAH_SERVER" ]; then
if ssh -o BatchMode=yes -o ConnectTimeout=5 "$THESIAH_SERVER" true >/dev/null 2>&1; then
server_available=true
echo "Sync server files..." && sync_server && echo "Success to sync server!" || echo "Warning: Failed to sync server"
else
echo "Warning: $THESIAH_SERVER not reachable via non-interactive ssh. Skipping server sync..."
fi
else
echo "Skipping server sync (THESIAH_SERVER not set)"
fi
# Sync to nextcloud only if both backup drive and server are available
if [ "$backup_available" = true ] && [ "$server_available" = true ]; then
echo "Sync files to nextcloud..." && sync_nextcloud && echo "Success to sync nextcloud!" || echo "Warning: Failed to sync nextcloud"
fi
echo "Done!"
|