-
Notifications
You must be signed in to change notification settings - Fork 5
/
backup.sh
105 lines (81 loc) · 3.01 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
#!/bin/bash
ENV_PATH="/opt/marzban/.env"
DB_NAME="marzban"
CONTAINER_NAME="mysql"
BACKUP_DIR="/opt/marzban/backup"
DOCKER_PATH="/opt/marzban/docker-compose.yml"
CERTS="/var/lib/marzban/certs"
TEMPLATES="/var/lib/marzban/templates"
# Extract environment variables
get_env_var() {
grep "$1" "$ENV_PATH" | cut -d '=' -f2 | tr -d '"'
}
SQLALCHEMY_DATABASE_URL=$(get_env_var 'SQLALCHEMY_DATABASE_URL')
XRAY_CONFIG=$(get_env_var 'XRAY_JSON')
BOT_TOKEN=$(get_env_var 'TELEGRAM_BACKUP_TOKEN')
CHAT_ID=$(get_env_var 'TELEGRAM_ADMIN_ID')
DISCORD_BACKUP_URL=$(get_env_var 'DISCORD_BACKUP_URL')
BACKUP_INTERVAL_TIME=$(get_env_var 'BACKUP_INTERVAL_TIME')
# Calculate sleep time in seconds
SLEEP_TIME=$((BACKUP_INTERVAL_TIME * 60))
# Log function for better visibility
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] - $1"
}
# Function to send a file to Telegram
send_backup_to_telegram() {
echo
echo "Sending 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 messege = "here is your back up"
echo
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
echo "Backup successfully sent to Discord"
}
# Backup function for SQLite
backup_sqlite() {
log "Starting SQLite backup..."
FILE_NAME="$DB_NAME-$(date '+%Y-%m-%d_%H:%M').tar.gz"
# Backup SQLite database file
cp "$SQLALCHEMY_DATABASE_URL" "$BACKUP_DIR/$DB_NAME.sqlite3"
# Create a tar archive with all backup files, including the directories
tar czvf "$BACKUP_DIR/$FILE_NAME" "$BACKUP_DIR/$DB_NAME.sqlite3" "$ENV_PATH" "$DOCKER_PATH" "$CERTS" "$TEMPLATES"
# Send backup to Telegram bot
send_backup_to_telegram "$BACKUP_DIR/$FILE_NAME"
send_backup_to_discord "$BACKUP_DIR/$FILE_NAME"
# Cleanup
rm "$BACKUP_DIR/$DB_NAME.sqlite3"
rm "$BACKUP_DIR/$FILE_NAME"
log "SQLite backup completed!"
}
# Backup function for MySQL
backup_mysql() {
log "Starting MySQL backup..."
FILE_NAME="$DB_NAME-$(date '+%Y-%m-%d_%H:%M').tar.gz"
# Create MySQL database backup
docker compose -f "$DOCKER_PATH" exec "$CONTAINER_NAME" mysqldump -u root -p"$DB_PASSWORD" "$DB_NAME" > "$BACKUP_DIR/db_backup.sql"
# Create tar archive with all backup files, including the directories
tar czvf "$BACKUP_DIR/$FILE_NAME" "$BACKUP_DIR/db_backup.sql" "$ENV_PATH" "$DOCKER_PATH" "$CERTS" "$TEMPLATES"
# Send backup to Telegram bot
send_backup_to_telegram "$BACKUP_DIR/$FILE_NAME"
send_backup_to_discord "$BACKUP_DIR/$FILE_NAME"
rm "$BACKUP_DIR/$FILE_NAME"
log "MySQL backup completed!"
}
# Main loop
while true; do
if [[ $SQLALCHEMY_DATABASE_URL == sqlite* ]]; then
backup_sqlite
else
DB_PASSWORD=$(get_env_var 'MYSQL_ROOT_PASSWORD')
backup_mysql
fi
log "Sleeping for $SLEEP_TIME seconds..."
sleep "$SLEEP_TIME"
done