-
Notifications
You must be signed in to change notification settings - Fork 2
/
Telegram-MySQL-Backup.sh
107 lines (89 loc) · 3.36 KB
/
Telegram-MySQL-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
#!/bin/bash
#==============================================================================
#TITLE: Telegram-MySQL-Backup.sh
#DESCRIPTION: Script for automating the daily MySQL backups to Telegram
#AUTHOR: NimaH79 (thanks to tleish)
#USAGE: ./Telegram-MySQL-Backup.sh
#CRON:
# example cron for daily MySQL backup @ 00:00
# min hr mday month wday command
# 0 0 * * * /path/to/Telegram-MySQL-Backup.sh
#RESTORE FROM BACKUP
#$ gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]
#==============================================================================
# directory to put the backup files
BACKUP_DIR=./
# MYSQL Parameters
MYSQL_UNAME=YOUR_MYSQL_USERNAME
MYSQL_PWORD=YOUR_MYSQL_PASSWORD
# Don't backup databases with these names
# Example: starts with mysql (^mysql) or ends with _schema (_schema$)
IGNORE_DB="(^mysql|_schema$)"
# Include mysql and mysqldump binaries for cron bash user
PATH=$PATH:/usr/bin/mysql
# Number of days to keep backups on disk (0 to disable)
KEEP_BACKUPS_FOR=30 # days
# Token of Telegram bot
BOT_TOKEN=YOUR_TELEGRAM_BOT_TOKEN
# chat_id of user who wants to get backup files in Telegram
CHAT_ID=YOUR_CHAT_ID
# Current time in YYYY-MM-DD format
DATE=$(date +%F)
function telegram_send_message() {
curl -F chat_id="$1" -F text="$2" https://api.telegram.org/bot$BOT_TOKEN/sendMessage &> /dev/null
}
function telegram_send_document() {
curl -F chat_id="$1" -F document=@"$2" caption="$3" https://api.telegram.org/bot$BOT_TOKEN/sendDocument &> /dev/null
}
function delete_old_backups() {
if [ $KEEP_BACKUPS_FOR -ne 0 ]; then
find $BACKUP_DIR -type f -name "*.sql.gz" -mtime +$KEEP_BACKUPS_FOR -exec rm {} \;
fi
}
function mysql_login() {
local mysql_login="-u $MYSQL_UNAME"
if [ -n "$MYSQL_PWORD" ]; then
local mysql_login+=" -p$MYSQL_PWORD"
fi
echo $mysql_login
}
function database_list() {
local show_databases_sql="SHOW DATABASES WHERE \`Database\` NOT REGEXP '$IGNORE_DB'"
echo $(mysql $(mysql_login) -e "$show_databases_sql"|awk -F " " '{if (NR!=1) print $1}')
}
function backup_database() {
backup_file="$BACKUP_DIR/$DATE.$database.sql.gz"
mysqldump $(mysql_login) $database | gzip -9 > $backup_file
backup_file_size=$(stat -c%s $backup_file)
if [ $backup_file_size -le 50000000 ]; then
# Send backup file to Telegram
telegram_send_document $CHAT_ID $backup_file
else
# Split backup file, then send to Telegram
zipped_backup_file="$BACKUP_DIR/$DATE.$database"
zip -r -s 50m "$zipped_backup_file.zip" $backup_file
number_of_parts=$(($backup_file_size / 50000000))
telegram_send_document $CHAT_ID "$zipped_backup_file.zip"
if [ $KEEP_BACKUPS_FOR -eq 0 ]; then
rm "$zipped_backup_file.zip"
fi
for i in $(seq -f "%02g" 1 $number_of_parts); do
telegram_send_document $CHAT_ID "$zipped_backup_file.z$i"
if [ $KEEP_BACKUPS_FOR -eq 0 ]; then
rm "$zipped_backup_file.z$i"
fi
done
fi
if [ $KEEP_BACKUPS_FOR -eq 0 ]; then
rm $backup_file
fi
}
function backup_databases() {
# Send current date to Telegram
telegram_send_message $CHAT_ID "MySQL Backup - $DATE:"
for database in $(database_list); do
backup_database
done
}
delete_old_backups
backup_databases