准备工作
- 备份的文件要同步到七牛 所有 注册 七牛的账号 注册地址:https://portal.qiniu.com/signup?code=3lqod8l7its9e
- 创建数据备份目录
cd ~ && mkdir backup && cd backup
mkdir ysqlbackup && mkdir mysql
- 设置七牛 上传账号
七牛上传用命令行工具 qshell :https://developer.qiniu.com/kodo/tools/1302/qshell
设置 qshell 账号
./qshell account AccessKey SecretKey
测试上传
qshell rput <Bucket> <Key> <LocalFile> true
./qshell rput dbbackup test.log test.log true
在 linux 服务器中脚本目录
mkdir /var/lib/gogomysqlbackup
cd /var/lib/gogomysqlbackup
创建shell 脚本文件
- 创建 备份脚本文件 项目名称数据库名称数据库类型_backup.sh
touch project_dbname_dbtype_backup.sh
vi project_dbname_dbtype_backup.sh
脚本文件内容
#!/bin/sh
# mysql data backup script
#
# use mysqldump --help,get more detail.
#
BakDir=/root/back/ysqlbackup
LogFile=/root/back/mysql/mysqlbak.log
DATE=`date +%Y%m%d%H%M%S`
echo " " >> $LogFile
echo " " >> $LogFile
echo "-------------------------------------------" >> $LogFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile
echo "--------------------------" >> $LogFile
cd $BakDir
DumpFile=$DATE.sql
GZDumpFile=$DATE.sql.tar.gz
mysqldump -u username -ppassword dbname -h 127.0.0.1> $DumpFile
echo "Dump Done" >> $LogFile
tar czvf $GZDumpFile $DumpFile >> $LogFile 2>&1
echo "[$GZDumpFile]Backup Success!" >> $LogFile
rm -f $DumpFile
#cd $BakDir/daily
cd $BakDir
echo "Backup Done!" >> $LogFile
/root/back/qshell rput dbbackup $GZDumpFile $GZDumpFile true
echo "copy it to your local disk or ftp to somewhere !!!" >> $LogFile
find $BakDir -ctime +30 -exec rm {} ;
echo "delete file over 30 days" >> $LogFile
执行文件
bash project_dbname_dbtype_backup.sh
修改文件的执行权限
# chmod 777 project_dbname_dbtype_backup.sh
用crontab定时执行备份脚本代码
crontab -e
# 每两个小时执行一次
1 */2 * * * /var/lib/gogomysqlbackup/gogo_vtigercrm_mysql_backup.sh
参考引用
linux中mysql备份shell脚本代码 :www.jb51.net/article/34376.htm
tar命令的详细解释 : http://blog.csdn.net/eroswang/article/details/5555415/