1.异地备份的话,需要做ssh免密登录
在MySQL服务器上操作
ssh-keygen
ssh-copy-id root@192.168.1.4
2. MySQL全备脚本,可以备份多个数据库,单独文件夹,删除十天前的备份,拷贝到其他服务器(scp增量,不会覆盖掉原先文件)
#!/bin/bash
#Mysql dabase information
#主机
find /aebiz/soft/databackup/* -type f -mtime +10 -name "*.gz" -exec rm {} \;
db_host="localhost"
#数据库帐号
db_user="root"
#数据库密码
db_passwd="123456"
#mysql安装目录
MYSQL="/aebiz/soft/mysql"
#mysql命令目录
MYSQLDUMP="/aebiz/soft/mysql/bin/mysqldump"
#Path information
#存放备份文件的路径
BACKUP_DB="/aebiz/soft/databackup"
LogFile=$BACKUP_DB"/bak.log"
#Time information
#时间
time=`date +"%Y-%m-%d-%H-%M-%S"`
day=`date +"%d"`
month=`date +"%Y-%m"`
weekday=`date +"%u"`
#Path enable write
if [ ! -w "$BACKUP_DB" ]; then
chmod -R 700 $BACKUP_DB
fi
#echo "**********************************host info****************************************"
#echo "db_host:"$db_host
#echo "db_user:"$db_user
#echo "db_passwd:"$db_passwd
#echo "MYSQL:"$MYSQL
#echo "MYSQLDUMP:"$MYSQLDUMP
#echo "databaseName:"$databaseName
#echo "BACKUP_DB:"$BACKUP_DB
#echo "**********************************************************************************"
#Mysql Backup
Date=`date +%Y%m%d`
Begin=`date +"%Y-%m-%d %H:%M:%S"`
#echo "start backup database:"$databaseName" "$Begin
#databases
#需要备份的数据库,如果有多个数据库,请用,分隔
databaseList="ceshishop,ceshiydys,testshop0711,testydys0711"
for databaseName in `echo "$databaseList" | sed 's/,/\n/g'`
do
echo $databaseName
#fileName
#生成备份文件名
fileName=$databaseName"-"$time".sql"
#生成备份文件完整路径
BACKUP_DBPATH=$BACKUP_DB"/"$databaseName
#echo "***********************************database Info***********************************************"
#echo "BACKUP_DBPATH:"$BACKUP_DBPATH
#echo "fileName:"$fileName
#echo "**********************************************************************************"
if [ ! -d "$BACKUP_DBPATH" ]; then
mkdir "$BACKUP_DBPATH"
fi
#备份mysql
$MYSQLDUMP -u $db_user -p$db_passwd -h $db_host $databaseName > $BACKUP_DBPATH/$fileName
#打包.sql文件
cd $BACKUP_DBPATH && tar -czf $fileName.tar.gz $fileName && rm -rf $fileName && chmod go-rwx $fileName.tar.gz
Last=`date +"%Y-%m-%d %H:%M:%S"`
#echo "end backup database:"$databaseName" "$Last
#输出日志
echo start:$Begin end:$Last $fileName succ >> $LogFile
done
scp -r /aebiz/soft/databackup root@192.168.1.4:/aebiz/soft
3.Mysql服务器设置crontab定时任务
每隔三天的凌晨三点,执行备份
0 3 */3 * * sh /aebiz/soft/databasebak.sh
4.备份服务器设置crontab定时任务
备份服务器的可以设置保留时间长一些,删除30天前的备份
每隔三天查找备份目录下超过三十天的文件并删除.
0 3 */3 * * find /aebiz/soft/databackup/* -type f -mtime +30 -name "*.gz" -exec rm {} \;
网友评论