-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbackup.sh
250 lines (195 loc) · 7.18 KB
/
backup.sh
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
#!/bin/bash
CONFIG_FILE="config.json"
get_config() {
jq -r "$1 // \"\"" "$CONFIG_FILE"
}
get_env_var() {
local env_file="$1"
shift
if [[ ! -f "$env_file" ]]; then
echo "Error: Environment file '$env_file' not found." >&2
return 1
fi
while [[ $# -gt 0 ]]; do
local var_name="$1"
local var_value=$(grep -E "^$var_name\s*=" "$env_file" | sed -E 's/^[^=]*=\s*//; s/^"//; s/"$//; s/^'"'"'//; s/'"'"'$//')
if [[ -n "$var_value" ]]; then
DB_URL="$var_value"
return 0
fi
shift
done
echo "Error: No non-empty variable found in the environment file." >&2
DB_URL=""
return 1
}
# Global configurations
BACKUP_DIR=$(get_config '.backup_dir')
BACKUP_INTERVAL_TIME=$(get_config '.backup_interval_time')
BOT_TOKEN=$(get_config '.telegram.bot_token')
CHAT_ID=$(get_config '.telegram.chat_id')
DISCORD_BACKUP_URL=$(get_config '.discord.backup_url')
SLEEP_TIME=$((BACKUP_INTERVAL_TIME * 60))
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] - $1"
}
send_backup_to_telegram() {
local file_path="$1"
curl -F chat_id="$CHAT_ID" -F document=@"$file_path" "https://api.telegram.org/bot$BOT_TOKEN/sendDocument"
}
send_backup_to_discord() {
local file_path="$1"
local message="Here is your backup"
echo
echo "Sending Backup To Discord"
curl -X POST -H "Content-Type: multipart/form-data" -F "content=$messege" -F "file=@$file_path" $DISCORD_BACKUP_URL
}
backup_sqlite() {
local backup_name=$1
local db_path=$2
shift
local additional_files=("$@")
log "Starting SQLite backup for $backup_name..."
FILE_NAME="$backup_name-$(date '+%Y-%m-%d_%H:%M').tar.gz"
cp "$db_path" "$BACKUP_DIR/$backup_name.sqlite3"
tar czvf "$BACKUP_DIR/$FILE_NAME" "$BACKUP_DIR/$backup_name.sqlite3" "${additional_files[@]}"
send_backup_to_telegram "$BACKUP_DIR/$FILE_NAME"
send_backup_to_discord "$BACKUP_DIR/$FILE_NAME"
rm "$BACKUP_DIR/$backup_name.sqlite3"
rm "$BACKUP_DIR/$FILE_NAME"
log "SQLite backup for $db_name completed!"
}
backup_mysql() {
local db_name=$1
local backup_name=$2
local container_name=$3
local docker_path=$4
local user=$5
local password=$6
shift
local additional_files=("$@")
log "Starting MySQL backup for $db_name..."
FILE_NAME="$backup_name-$(date '+%Y-%m-%d_%H:%M').tar.gz"
if ! output=$(docker compose -f "$docker_path" exec "$container_name" mysqldump -u root -p"$password" "$db_name" 2>&1 > "$BACKUP_DIR/db_backup.sql"); then
if [[ "$output" == *"Enter password:"* || "$output" == *"Access denied"* ]]; then
log "Error: Authentication failed for MySQL backup. Please check credentials."
return 1
else
log "Error during MySQL backup: $output"
return 1
fi
fi
tar czvf "$BACKUP_DIR/$FILE_NAME" "$BACKUP_DIR/db_backup.sql" "$docker_path" "${additional_files[@]}"
send_backup_to_telegram "$BACKUP_DIR/$FILE_NAME"
send_backup_to_discord "$BACKUP_DIR/$FILE_NAME"
rm "$BACKUP_DIR/db_backup.sql"
rm "$BACKUP_DIR/$FILE_NAME"
log "MySQL backup for $db_name completed!"
return 0
}
backup_mariadb() {
local db_name=$1
local backup_name=$2
local container_name=$3
local docker_path=$4
local user=$5
local password=$6
shift
local additional_files=("$@")
log "Starting MariaDB backup for $db_name..."
FILE_NAME="$backup_name-$(date '+%Y-%m-%d_%H:%M').tar.gz"
if ! output=$(docker compose -f "$docker_path" exec "$container_name" mariadb-dump -u"$user" -p"$password" "$db_name" 2>&1 > "$BACKUP_DIR/db_backup.sql"); then
if [[ "$output" == *"Enter password:"* || "$output" == *"Access denied"* ]]; then
log "Error: Authentication failed for MariaDB backup. Please check credentials."
return 1
else
log "Error during MariaDB backup: $output"
return 1
fi
fi
tar czvf "$BACKUP_DIR/$FILE_NAME" "$BACKUP_DIR/db_backup.sql" "$env_path" "$docker_path" "${additional_files[@]}"
send_backup_to_telegram "$BACKUP_DIR/$FILE_NAME"
send_backup_to_discord "$BACKUP_DIR/$FILE_NAME"
rm "$BACKUP_DIR/db_backup.sql"
rm "$BACKUP_DIR/$FILE_NAME"
log "MariaDB backup for $db_name completed!"
return 0
}
parse_sqlalchemy_url() {
local input_string="$1"
local credentials=$(echo "$input_string" | sed -n 's/.*mysql:\/\/\(.*\)@.*/\1/p')
local username=$(echo "$credentials" | cut -d ':' -f 1)
local pass=$(echo "$credentials" | cut -d ':' -f 2)
local db=$(echo "$input_string" | sed -n 's/.*\/\([^/]*\)$/\1/p')
user="$username"
password="$pass"
database="$db"
}
parse_gorm_url() {
local input_string="$1"
local username=$(echo "$input_string" | cut -d ':' -f 1)
local username=$(echo "$input_string" | sed -n 's/^\([^:]*\):.*@tcp.*/\1/p')
local pass=$(echo "$input_string" | sed -n 's/^[^:]*:\([^@]*\)@tcp.*/\1/p')
local db=$(echo "$input_string" | sed -n 's/.*\/\([^?]*\).*/\1/p' | cut -d '?' -f 1)
db=$(echo "$db" | cut -d '?' -f 1)
user="$username"
password="$pass"
database="$db"
}
process_database() {
local index=$1
local DB_NAME=$(get_config ".databases[$index].db_name")
local DB_TYPE=$(get_config ".databases[$index].type")
local ENV_PATH=$(get_config ".databases[$index].env_path")
local CONTAINER_NAME=$(get_config ".databases[$index].container_name")
local DOCKER_PATH=$(get_config ".databases[$index].docker_path")
local URL_FORMAT=$(get_config ".databases[$index].url_format")
get_env_var $ENV_PATH "DATABASE_URL" "SQLALCHEMY_DATABASE_URL"
local EXTERNAL_PATHS=$(jq -r ".databases[$index].external | join(\" \")" "$CONFIG_FILE")
if [[ $DB_TYPE == "sqlite" ]]; then
DB_URL="${DB_URL#sqlite:///}"
else
parse_sqlalchemy_url "$DB_URL"
fi
if [[ $DB_TYPE == "sqlite" ]]; then
DB_URL="${DB_URL#sqlite:///}"
else
case $URL_FORMAT in
"sqlalchemy")
parse_sqlalchemy_url "$DB_URL"
;;
"gorm")
parse_gorm_url "$DB_URL"
;;
*)
log "Unsupported database type: $DB_TYPE"
;;
esac
fi
if [[ -z "$DB_NAME" ]]; then
DB_NAME="$database"
fi
case $DB_TYPE in
"sqlite")
backup_sqlite "$DB_NAME" "$SQLALCHEMY_DATABASE_URL" "$ENV_PATH" "$DOCKER_PATH" $EXTERNAL_PATHS
;;
"mysql")
backup_mysql "$database" "$DB_NAME" "$CONTAINER_NAME" "$DOCKER_PATH" "$user" "$password" "$ENV_PATH" $EXTERNAL_PATHS
;;
"mariadb")
backup_mariadb "$database" "$DB_NAME" "$CONTAINER_NAME" "$DOCKER_PATH" "$user" "$password" "$ENV_PATH" $EXTERNAL_PATHS
;;
*)
log "Unsupported database type: $DB_TYPE"
;;
esac
}
# Main loop
while true; do
DATABASE_COUNT=$(jq '.databases | length' "$CONFIG_FILE")
for ((i = 0; i < DATABASE_COUNT; i++)); do
process_database "$i"
done
log "Sleeping for $SLEEP_TIME seconds..."
sleep "$SLEEP_TIME"
done