-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_backup.sh
50 lines (40 loc) · 1.25 KB
/
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
#!/bin/bash
# Variables
S3FILESYSLOCATION="s3://YOUR_S3_BUCKET/"
mysqlfolder="databases/"
MUSER="YOUR_DB_USER"
MPASS="YOUR_DB_PASS"
MHOST="YOUR_DB_HOST"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
# Server Folders
MYSQLTMPDIR="/backups/mysql"
# Dump MySQL Databases
BACKUPMYSQL=1
if [[ -n "$BACKUPMYSQL" && "$BACKUPMYSQL" -gt 0 ]]; then
# Error handling
if [[ -n "$BACKUPMYSQL" && "$BACKUPMYSQL" -gt 0 ]]; then
if [[ -z "$MYSQL" || -z "$MYSQLTMPDIR" || -z "$MYSQLDUMP" || -z "$GZIP" ]]; then
echo "Not all MySQL commands found."
exit 2
fi
fi
# Get all database names
DBS="$($MYSQL -u$MUSER -p$MPASS -h$MHOST -Bse 'show databases')"
# Dump databases
for db in $DBS
do
if [ "$db" != "information_schema" ]; then
echo mysqldump $db
$MYSQLDUMP --single-transaction --opt --net_buffer_length=75000 -u$MUSER -p$MPASS -h$MHOST $db | $GZIP -9 > $MYSQLTMPDIR/$(date +"%Y.%m.%d_%H.%M")--$db.sql.gz
fi
done
# Backup databases
echo $MYSQLTMPDIR $S3FILESYSLOCATION/$mysqlfolder
# Sync the file to amazon
s3cmd sync -r $MYSQLTMPDIR/ $S3FILESYSLOCATION/$mysqlfolder
# Cleanup local MySQL dump folder, delete all files older than 3 days
echo "Cleaning up old MySQL Dumps"
find $MYSQLTMPDIR -type f -mtime +3 -delete
fi