blob: d2dc610c2e5268012cf46757f257f9a8e2e5e810 (
plain)
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
|
#!/bin/sh
# local backup
backup_path="/mnt/second/backup"
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
}
mount_luks() {
if ! mount | grep -q " /mnt/second "; then
size_nvme0=$(sudo blockdev --getsize64 /dev/nvme0n1p1)
size_nvme1=$(sudo blockdev --getsize64 /dev/nvme1n1p1)
[ "$size_nvme1" -lt "$size_nvme0" ] && target_device="/dev/nvme0n1p1" || target_device="/dev/nvme1n1p1"
${TERMINAL:-st} -n floatterm -g 60x1 -e sudo cryptsetup open "$target_device" "second"
sudo -A mount "/dev/mapper/second" "/mnt/second" -o uid="$(id -u)",gid="$(id -g)" 2>/dev/null || sudo -A mount "/dev/mapper/second" "/mnt/second"
fi
}
# Using a loop over space-separated strings instead of an array
sync_files() {
for source in "$dot_path" "$git_path" "$pass_path" "$suck_path"; do
rsync -vrazPlu --exclude=".music.txt" --delete "$source" "$backup_path/" >/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" || exit 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" || exit
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 to $backup_path..."
process_options "$@"
# Main script logic
mount_luks && echo "Mount backup drive... " && echo "Success to mount luks drive!" || error "Failed to mount $backup_path"
[ -d "$backup_path" ] || sudo mkdir -p "$backup_path"
echo "Sync home files..." && sync_files && echo "Success to sync files!" || error "Failed back up files"
echo "Sync root files..." && sync_root && echo "Success to sync root!" || error "Failed back up root"
echo "Sync server files..." && sync_server && echo "Success to sync server!" || error "Failed back up server"
echo "Sync files to nextcloud..." && sync_nextcloud && echo "Success to sync nextcloud!" || error "Failed back up nextcloud"
echo "Done!"
|