如下的shell 脚本,对mysql数据库进行备份,并删除7天之前的备份文件。
代码:
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
clear;
username=""
passwd=""
mysqlhost=""
backuppath=""
#备份数据库
function mysqlbackup() {
#把数据库赋值给数组变量
mysqldata=`mysql -h$mysqlhost -u$username -p$passwd -e"show databases"|grep -vE "mysql|information_schema|performance_schema|Database"`
for i in ${mysqldata[@]}
do
#先清理空间然后备份
find $backuppath -name $i_*.zip -type f -mtime +7 -exec rm {} ;
#备份后压缩保存
mysqldump --opt -h$mysqlhost -u$username -p$passwd $i |gzip > $backuppath/$i_$(date +%Y%m%d%H%M).zip
done
}
function dataftp() {
ftphost=''
ftpusr=''
ftpd=''
ftp -v -n $ftphost<<!
user $ftpusr $ftpd
binary
passive
lcd $backuppath
cd /
mput *$(date +%Y%m%d)*
close
bye
!
}
#调用mysqlbackup
mysqlbackup
#调用FTP上传备份
dataftp