-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvps_backup
executable file
·103 lines (86 loc) · 2.71 KB
/
vps_backup
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
#!/usr/bin/env bash
# Backs up cache/credential/data files
set -u
set -o pipefail
# require that a executable be installed
require() {
if [[ ! $(command -v "$1") ]]; then
printf "requires %s, Install that and re-run\n" "$1"
exit 1
fi
}
# mkdir if first arg doesn't exist
mkdir_if_not_exists() {
if [[ ! -d "$1" ]]; then
mkdir -p "$1"
fi
}
# make sure a file exists or exit
expect_file_and_copy() {
if [[ ! -f "$1" ]]; then
printf "Fatal error: Expected file at %s\n" "$1"
exit 1
fi
cp "$1" "$2"
}
expect_dir_and_copy() {
if [[ ! -d "$1" ]]; then
printf "Fatal Error: no such directory %s\n" "$1"
exit 1
fi
set +u
if [[ -n "$2" ]]; then
target="$BACKUP_DIR/$2"
mkdir -p "$target"
cp -R "$1" "$target" 2>/dev/null
else
cp -R "$1" "$BACKUP_DIR" 2>/dev/null
fi
set -u
}
optional_copy() {
if [[ -f "$1" ]]; then
cp "$1" "$2"
else
printf "Warning: Expected file at %s\n" "$1"
fi
}
delete_if_exists() {
[[ -d "$1" || -f "$1" ]] && rm -rf "$1" && printf "Removed '%s'\n" "$1"
}
require tar
require realpath
VPS_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")/..")"
cd "$VPS_DIR" || exit $?
source "${VPS_DIR}/directories"
BACKUP_DIR_NAME="backup_dir"
BACKUP_DIR="${VPS_DIR}/${BACKUP_DIR_NAME}"
BACKUP_COUNTDOWN="${BACKUP_DIR}"/countdown
BACKUP_NOTIFY="${BACKUP_DIR}"/notify
BACKUP_TAR_NAME="${BACKUP_DIR_NAME}.tar.gz"
delete_if_exists "$BACKUP_DIR"
delete_if_exists "$BACKUP_TAR_NAME"
mkdir_if_not_exists "$BACKUP_COUNTDOWN"
mkdir_if_not_exists "$BACKUP_NOTIFY"
# save data from my website
curl -s 'https://purarue.xyz/api/gb_comment' >"$BACKUP_DIR/gb_comment.json"
curl -s 'https://purarue.xyz/api/page_hit' >"$BACKUP_DIR/page_hit.json"
expect_file_and_copy "$NOTIFY_BOT/token.yaml" "$BACKUP_NOTIFY"
expect_file_and_copy "$NOTIFY_BOT/old" "$BACKUP_NOTIFY"
expect_file_and_copy "$NOTIFY_BOT/export.json" "$BACKUP_NOTIFY"
expect_file_and_copy "$COUNTDOWN_BOT/token.yaml" "$BACKUP_COUNTDOWN"
optional_copy "$COUNTDOWN_BOT/server.db" "$BACKUP_COUNTDOWN"
optional_copy "$HOME/.bashrc" "$BACKUP_DIR"
optional_copy "$HOME/.bash_ext" "$BACKUP_DIR"
optional_copy "$HOME/.bash_profile" "$BACKUP_DIR"
optional_copy "$HOME/.bash_history" "$BACKUP_DIR"
fd . ~/code/ --type d | xargs -P 5 -I {} find {} -maxdepth 1 -name '.env' | rsync -vh --files-from=- / "$BACKUP_DIR"
expect_dir_and_copy "/etc/netdata"
expect_dir_and_copy "/etc/nginx"
expect_dir_and_copy "$HOME/shorturls"
expect_dir_and_copy "$CHECKER_MAL/data" checker_mal_data
expect_dir_and_copy "$GLUE/data/" glue_data
expect_dir_and_copy "$FILMSWAP/backups" filmswap_backups
expect_file_and_copy "$FILMSWAP/filmswap.db" "$BACKUP_DIR/filmswap_backups"
tar cvfz "$BACKUP_TAR_NAME" "$BACKUP_DIR_NAME" && printf "Created '%s'\n" "$BACKUP_TAR_NAME"
delete_if_exists "$BACKUP_DIR"