首页 > 网络资讯 > 技术支持 >
使用mysqldump自动备份mysql并上传数据到ftp
分享至:0
2017-05-02 11:12:17     来源:互联网     点击:
导读:
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
#!/bin/bash
 
NOWDATE=`date +%Y-%m-%d`
OLDDATE=`date +%Y-%m-%d -d '-15 days'`
FTPOLDDATE=`date +%Y-%m-%d -d '-30 days'`
NOWTIME=`date +%Y%m%d%H%M%S`
MYSQLDUMP=/usr/bin/mysqldump
SOCKET=/var/lib/mysql/mysql.sock
 
#建立备份基本目录环
BACKUPDIR=/backup/mysqldb
[ -d ${BACKUPDIR} ] || mkdir -p ${BACKUPDIR}
[ -d ${BACKUPDIR}/${NOWDATE} ] || mkdir ${BACKUPDIR}/${NOWDATE}
[ ! -d ${BACKUPDIR}/${OLDDATE} ] || rm -rf ${BACKUPDIR}/${OLDDATE}
 
#mysqldump备份
USERNAME=backup
PASSWORD=backup
DATABASENAME=(mysql test)
 
for DBNAME in ${DATABASENAME[@]};
do
    ${MYSQLDUMP} --opt --add-drop-database --tz-utc=true --flush-logs --events -u${USERNAME}
-p${PASSWORD} -S${SOCKET} ${DBNAME} |
gzip -c -9 > ${BACKUPDIR}/${NOWDATE}/${DBNAME}
-backup-${NOWTIME}.sql.gz
    logger "${DBNAME} has been backup successful - ${NOWDATE}"
    /bin/sleep 5
done
 
#上传备份至FTP yum -y install ftp
 
HOST=1.1.1.1
FTP_USERNAME=backup
FTP_PASSWORD=backup
 
cd ${BACKUPDIR}/${NOWDATE}
 
ftp -i -n -v << EOF
open ${HOST}
user ${FTP_USERNAME} ${FTP_PASSWORD}
bin
cd ${FTPOLDDATE}
mdelete *
cd ..
rmdir ${FTPOLDDATE}
mkdir ${NOWDATE}
cd ${NOWDATE}
mput *
bye
EOF
 
#使用备用方式删除旧备份文件
# find ${BACKUPDIR} -type f -ctime +2 -exec rm -fr {} \;
# find ${BACKUPDIR} -empty -exec rm -fr {} \;