-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart-backup-from-all-databases.sh
87 lines (61 loc) · 1.84 KB
/
start-backup-from-all-databases.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
#!/usr/bin/env bash
# shellcheck disable=SC1091
cd "$(dirname "${BASH_SOURCE[0]}")"
# include yaml reader script
source script/yaml.sh
# include telegram reader script
source script/telegram.sh
# Simple bash script to backup sql database into file with mysqldump and transfer it to another server
#
#
# Execute yaml reader
create_variables conf/config[$(hostname)].me.yaml
#define server name
if [ ! $server_name ]; then
server_name=`hostname`
fi
SECONDS=0
BUSY=busy.log
if test -f "$BUSY"; then
echo "it's busy from last action on db!"
mkdir -p log
echo "it's busy from last action on db!" >> log/$(date +%Y%m%d-%H:%M)-db-busy.log
telegram_send "🧨 $server_name busy from last opr on db backup! $BACKUP_FOLDER"
fi
# save log
echo 'backup db --> '$(date +%Y%m%d-%H:%M:%S)' --> start' >> $BUSY
# save start date for backup
NOTIF="<b>"$server_title"</b> <u>$BACKUP_FOLDER</u>%0A"
NOTIF+=$(date +%Y/%m/%d)" "$(date +%H:%M:%S)"%0A"
#file name for backup
FILE_UNIQUE_NAME="NA"
case $BACKUP_FOLDER in
hourly)
FILE_UNIQUE_NAME=-h$(date +%H)
;;
daily)
FILE_UNIQUE_NAME=-d$(date +%d)
;;
monthly)
FILE_UNIQUE_NAME=-m$(date +%m)
;;
*)
echo -n "*** unknown backup mode - create now"
FILE_UNIQUE_NAME=-now
BACKUP_FOLDER=now-$(date +%Y%m%d-%H%M%S)
;;
esac
#define file name
FILENAME=backup[$server_name]-alldb$FILE_UNIQUE_NAME.sql.gz
#define file path
FOLDER_PATH="$(pwd)/$BACKUP_FOLDER"
# create folder if not exist
mkdir -p $FOLDER_PATH
# create file full path
FILEPATH=$FOLDER_PATH/$FILENAME
# create a dump from all database
echo "*** START DUMP DATABASE"
mysqldump --quick --single-transaction --column-statistics=0 --verbose --all-databases | gzip > $FILEPATH
echo 'backup db --> '$(date +%Y%m%d-%H:%M:%S)' --> finish' >> $BUSY
NOTIF+="🆗 $(date +%M:%S) <code>DB Backup</code>%0A"
echo "*** Backup Complete"