forked from magenx/Magento-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_backup.sh
63 lines (54 loc) · 1.34 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
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
### CHECK FOR NCFTP PACKAGE ###
rpm -qa | grep -qw ncftp || yum -y install ncftp
### SYSTEM SETUP ###
BACKUP=/tmp/backup.$$
TIME=$(date +"%H-%M-%S")
DAYS=$(date +"%d-%m-%Y")
DELDATE=$(date -d "-7 days" +"%d-%m-%Y")
### MYSQL SETUP ###
MUSER="xxx"
MPASS="xxx"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
### FTP SETUP ###
FTPD="/backup"
FTPU="xxxxxx"
FTPP="xxxxxx"
FTPS="xxxxxx"
### CALLBACK ###
EMAILID="[email protected]"
### CREATE BACKUP DIRECTORY ###
[ ! -d ${BACKUP} ] && mkdir -p ${BACKUP} || :
### MYSQLDUMP ###
DBS="$(${MYSQL} -u ${MUSER} -h ${MHOST} -p${MPASS} -Bse 'show databases' | grep -v performance_schema)"
for db in ${DBS}
do
FILE=${BACKUP}/mysql-${db}.${DAYS}.${TIME}.sql.gz
${MYSQLDUMP} -u ${MUSER} -h ${MHOST} -p${MPASS} --single-transaction --routines --triggers --events ${db} | ${GZIP} > ${FILE}
done
### FTP UPLOAD ###
ncftp -u ${FTPU} -p ${FTPP} ${FTPS}<<EOF
cd ${FTPD}/${DELDATE}
rm *.gz
cd ${FTPD}
rmdir ${DELDATE}
mkdir ${FTPD}
mkdir ${FTPD}/${DAYS}
cd ${FTPD}/${DAYS}
lcd ${BACKUP}
mput *
quit
EOF
### ALERT IF FAILED ###
if [ "$?" == "0" ]; then
rm -f ${BACKUP}/*
else
CALLBACK=/tmp/backup.failed
echo "Date: ${DAYS}">${CALLBACK}
echo "Hostname: $(hostname)" >>${CALLBACK}
echo "Backup failed" >>${CALLBACK}
mail -s "BACKUP FAILED" "${EMAILID}" <${CALLBACK}
fi