-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.shell_functions
More file actions
398 lines (321 loc) · 11.6 KB
/
Copy path.shell_functions
File metadata and controls
398 lines (321 loc) · 11.6 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#HACK: Ideally this should be an export in '.profile., but it's not working properly on Debian 11.
OS_NAME="$(uname -s)"
backup_file ()
{
cp "$1" "$1.bak"
}
check_network_connection() {
# 1. Try to ping a global IP (Cloudflare DNS) to check raw Internet
# We adjust flags based on OS because 'ping' is not standardized
if [ "$OS_NAME" = "OpenBSD" ]; then
# OpenBSD: -c (count), -w (deadline in seconds)
ping -n -c 1 -w 2 1.1.1.1 >/dev/null 2>&1
else
# Linux/General: -c (count), -W (timeout in seconds)
ping -n -c 1 -W 2 1.1.1.1 >/dev/null 2>&1
fi
# Check the exit status of the ping
if [ $? -eq 0 ]; then
echo "The host is connected to the Internet."
return 0
fi
# 2. If Ping failed, check if we at least have a default gateway
# This identifies if the issue is 'no network' vs 'no internet'
HAS_GATEWAY=1
if [ "$OS_NAME" = "OpenBSD" ]; then
route -n show -gateway | grep -q 'default' || HAS_GATEWAY=0
else
ip route | grep -q 'default' || HAS_GATEWAY=0
fi
if [ "$HAS_GATEWAY" -eq 1 ]; then
echo "The host is connected to a network, but not the Internet."
else
echo "The host is not connected to any network (no default route)."
fi
}
edit_backup_excludes ()
{
TARGET="/usr/local/scripts/backup_job_files/exclude.txt"
"$SU_CMD" "$EDITOR" "$TARGET"
}
edit_backup_includes ()
{
TARGET="/usr/local/scripts/backup_job_files/include.txt"
"$SU_CMD" "$EDITOR" "$TARGET"
}
edit_backup_settings ()
{
SOURCE="$HOME/.local/scripts.noexecute/backup_job_files/settings.sh"
TARGET="/usr/local/scripts/backup_job_files/settings.sh"
if [ -f "$TARGET" ]; then
"$SU_CMD" "$EDITOR" "$TARGET"
else
echo -e "Info: No '${TARGET}' file is present. Initializing new one..."
"$SU_CMD" cp -v "$SOURCE" "$TARGET"
if [ $? -ne 0 ]; then
echo "Error: Cannot copy settings file."
return
fi
"$SU_CMD" "$EDITOR" "$TARGET"
fi
}
reinstall_backup_script ()
{
local SOURCE_SCRIPTS_DIR="$HOME/.local/scripts.noexecute"
local TARGET_SCRIPTS_DIR="/usr/local/scripts"
"$SU_CMD" mkdir -p "$TARGET_SCRIPTS_DIR"
if [ "$OS_NAME" = "OpenBSD" ]; then
local OPTS="-m 0744 -o root -g bin"
else
local OPTS="-vm 0744 -o root -g root"
fi
"$SU_CMD" install $INSTALL_OPTS "${SOURCE_SCRIPTS_DIR}/backup_job.sh" "${TARGET_SCRIPTS_DIR}/backup_job.sh"
"$SU_CMD" cp -v "${SOURCE_SCRIPTS_DIR}/backup_job.sh.version" "${TARGET_SCRIPTS_DIR}"
}
replace_backup_configuration_files ()
{
local SOURCE_SCRIPTS_DIR="$HOME/.local/scripts.noexecute"
local TARGET_SCRIPTS_DIR="/usr/local/scripts"
"$SU_CMD" mkdir -p "$TARGET_SCRIPTS_DIR"
"$SU_CMD" cp -rv "${SOURCE_SCRIPTS_DIR}/backup_job_files" "${TARGET_SCRIPTS_DIR}"
}
restart_network_service ()
{
if [ "$OS_NAME" = "Linux" ]; then
nmcli networking off
nmcli networking on
elif [ "$OS_NAME" = "OpenBSD" ]; then
"$SU_CMD" sh "/etc/netstart"
fi
}
run_backup_script ()
{
if [ -f "/usr/local/scripts/backup_job.sh" ]; then
"$SU_CMD" "/usr/local/scripts/backup_job.sh"
else
echo -e "Error: Your backup job script is not installed properly."
echo -e "Do you want to set it up? If so, enter \"Yes.\" (without quotes):"
echo -en "? "
read answer
echo
if [ "$answer" = "Yes." ]; then
set_up_backup_script
fi
fi
}
set_up_backup_script ()
{
OLD_LOCATION="/usr/local/scripts"
NEW_LOCATION="$HOME/.local/scripts.noexecute"
SCRIPT_VERSION_FILENAME="backup_job.sh.version"
FILES_VERSION_FILENAME="backup_job_files/version"
OLD_BACKUP_SCRIPT_VERSION="$(cat ${OLD_LOCATION}/${SCRIPT_VERSION_FILENAME})"
OLD_BACKUP_FILES_VERSION="$(cat ${OLD_LOCATION}/${FILES_VERSION_FILENAME})"
NEW_BACKUP_SCRIPT_VERSION="$(cat ${NEW_LOCATION}/${SCRIPT_VERSION_FILENAME})"
NEW_BACKUP_FILES_VERSION="$(cat ${NEW_LOCATION}/${FILES_VERSION_FILENAME})"
if [ -f "/usr/local/scripts/backup_job.sh" ]; then
echo -e "Info: It looks like, there's already backup job script installed."
echo -e "Current version is: ${OLD_BACKUP_SCRIPT_VERSION}"
echo -e "The new version is: ${NEW_BACKUP_SCRIPT_VERSION}\n"
echo -e "Do you still want to reinstall/upgrade the script? If so, enter \"Yes.\" (without quotes):"
echo -en "? "
read answer
echo
if [ "$answer" = "Yes." ]; then
echo -e "Reinstalling/Upgrading the script..."
reinstall_backup_script
else
echo -e "Operation canceled by user."
fi
else
echo -e "Installing the script..."
reinstall_backup_script
fi
if [ -d "/usr/local/scripts/backup_job_files" ]; then
echo -e "\n\nInfo: It looks like, a directory with backup job configuration at '/usr/local/scripts/backup_job_files' is already present."
echo -e "Current version is: ${OLD_BACKUP_FILES_VERSION}"
echo -e "The new version is: ${NEW_BACKUP_FILES_VERSION}\n"
echo -e "Do you want to replace these files with new fresh ones?"
echo -e "MAKE SURE YOU KNOW WHAT YOU'RE DOING AND MAKE A COPY OF YOUR EXISTING BACKUP CONFIGURATION DIRECTORY IF NECCESARY!"
echo -e "\nANYWAY, IF YOU WANT TO DO THIS, ENTER \"Yes.\" (without quotes):"
echo -en "? "
read answer
echo
if [ "$answer" = "Yes." ]; then
echo -e "\nReplacing backup configuration files at '/usr/local/scripts/backup_job_files..."
replace_backup_configuration_files
first_conf_setup=1
else
echo -e "Operation canceled by user."
fi
else
echo -e "\nCopying backup configuration files to '/usr/local/scripts/backup_job_files'..."
replace_backup_configuration_files
first_conf_setup=1
fi
if [ -f "/etc/debian_version" ]; then
if [ -f "/etc/cron.daily/backup_job" ]; then
echo -e "\n\nInfo: It looks like, you already have linked backup job script to '/etc/cron.daily/backup_job'."
echo -e "Do you want to relink it? If so, enter \"Yes.\" (without quotes):"
echo -en "? "
read answer
echo
if [ "$answer" = "Yes." ]; then
relink_backup_script
else
echo -e "Operation canceled by user."
fi
else
relink_backup_script
fi
else
echo -e "\nInfo: Unknown operating system. Make sure to add '/usr/local/scripts/backup_job.sh to your system-wide crontab for it to work without user interaction."
fi
if [ "$first_conf_setup" = "1" ]; then
echo -e "\nInfo: For backup system to work, you need to configure your includes, excludes, and settings over in '/usr/local/scripts/backup_job_files' first!"
fi
echo -e "\nInfo: If working over ssh and authentication is needed, you need to make sure you have neccesary keys (by using 'ssh-keygen') and you have copied it to your remote device (by using 'ssh-copy-id')."
#TODO: Make it possible to do it here.
}
# Package updates / System upgrades
su_update_software ()
{
"$SU_CMD" "$HOME/.local/bin/su-update-software.sh"
if [ "$OS_NAME" = "Linux" ]; then
update_software
fi
}
su_upgrade_system ()
{
"$SU_CMD" "$HOME/.local/bin/su-upgrade-system.sh"
}
# Functions available only if doas is installed
if [ "$(command -v doas)" ]; then
doasedit () {
if [ -z "$1" ]; then
echo -e "Usage: doasedit <file>"
return 1
fi
local target=$(realpath "$1")
local tempfile=$(mktemp -t doasedit.XXXXXXXXXX)
trap 'rm -f "$tempfile; return 1"' INT TERM
if doas cat "$target" > "$tempfile" 2>/dev/null; then
$EDITOR "$tempfile"
doas dd if="$tempfile" of="$target" status=none
else
echo -e "Error: Authentication failed or file unreadable."
fi
trap - INT TERM
rm -f "$tempfile"
}
fi
# Functions available only if git is installed
if [ "$(command -v git)" ]; then
git_commit ()
{
local comment="$1"
git add .
git commit -m "$comment"
}
git_push ()
{
local comment="$1"
git_commit "$comment"
git push
}
fi
# Linux-specific functions
if [ "$OS_NAME" = "Linux" ]; then
list_cpus () {
lscpu "$@" | less -FRX
}
list_hardware () {
lshw "$@" | less -FRX
}
list_devices () {
lsdev "$@" | less -FRX
}
# Functions specific to Debian GNU/Linux
if [ -f "/etc/debian_version" ]; then
reconfigure_locales ()
{
"$SU_CMD" dpkg-reconfigure locales
}
relink_backup_script ()
{
local CRON_FILE="/etc/cron.daily/backup_job"
local SCRIPTS_DIR="/usr/local/scripts"
echo -e "\nCreating a daily cron job in '${CRON_FILE}'..."
"$SU_CMD" ln -fsv "${SCRIPTS_DIR}/backup_job.sh" "${CRON_FILE}"
}
fi
# Functions specific to Flatpak (if it's installed)
if [ "$(command -v flatpak)" ]; then
create_flatpak_symlinks ()
{
TARGET_DIR="$HOME/.local/bin/flatpak-executables"
mkdir -p "$TARGET_DIR"
#HACK: We'll use eval here, so OpenBSD's ksh don't complain about this code (even though it is never be executed).
#TODO: Ideally, this should be inside `.shell_functions.linux` file, or something similar.
# This move will eventually happen, in some future reorganization.
eval '
declare -A CUSTOM_NAMES
CUSTOM_NAMES=(
["net._86box._86Box"]="86box"
["org.telegram.desktop"]="telegram"
)
'
flatpak list --app --columns=application | while read -r app_id; do
# Check if we have a custom name defined
if [[ -n "${CUSTOM_NAMES[$app_id]}" ]]; then
script_name="${CUSTOM_NAMES[$app_id]}"
else
# Default logic: get part after last dot and lowercase it
script_name=$(echo "$app_id" | awk -F. '{print $NF}' | tr '[:upper:]' '[:lower:]')
# Remove leading underscores (e.g., _86box -> 86box)
script_name="${script_name#_}"
fi
script_path="$TARGET_DIR/$script_name"
cat <<EOF > "$script_path"
#!/bin/sh
flatpak run $app_id
EOF
chmod +x "$script_path"
echo -e "Mapped: $app_id -> $script_name"
done
echo -e "\nFinished! Scripts are in $TARGET_DIR"
}
list_flatpak_applications ()
{
flatpak list --app --columns=name,version | sort | column -t -s $'\t'
}
fi
# Functions specific to lspcmcia (if it's installed)
if [ "$(command -v lspcmcia)" ]; then
list_pmcmcia_cards ()
{
lspcmcia "$@" | less -FRX
}
fi
# Functions specific to Tailscale (if it's installed)
if [ "$(command -v tailscale)" ]; then
restart_tailscaled_service ()
{
"$SU_CMD" systemctl restart tailscaled
}
restart_all_networks ()
{
restart_network_service
restart_tailscaled_service
}
fi
update_software ()
{
"$HOME/.local/bin/user-update-software.sh"
}
wiki ()
{
local article="$1"
lynx gopher://gopherpedia.com/0/"${article}"
}
fi