mysql安装方式又多种,自动备份方法一样,仅仅备份脚本语句略有不同!
1.设置两台linux服务器之间免密scp(可选)
如果需要异地备份,需要将N个主机开启免密传输,例如:A数据库服务器,B是备份服务器
操作步骤:
1、在A机器上,执行ssh-keygen -t rsa,一路按Enter,不需要输入任何内容。(如有提示是否覆盖,可输入y后按回车)
2、到/root/.ssh/目录下,查看是否有id_rsa.pub文件生成
3、将A机器生成的id_rsa.pub文件拷贝到B机器的/root/.ssh/下,并将id_rsa.pub改名为authorized_keys(如果B机器已经有了authorized_keys,可以编辑,向下追加ssh-rsa内容即可)
4、好了,现在就可以在A机器上通过scp -r aaaa.txt root@B机IP:/home/xxx/ 命令进行免密远程拷贝了
2.编写备份脚本
根据自己清空修改最上面信息即可
#!/bin/bash
# 以下配置信息请自己修改
mysql_container="04c8f102a21f"# MySQL容器名称(Docker容器安装mysql用到)
mysql_user="root"#MySQL备份用户
mysql_password="TPM123"#MySQL备份用户的密码
mysql_host=localhost
mysql_port=3306mysql_charset="utf8mb4"#MySQL编码
backup_db_arr=("febs_cloud_base") #要备份的数据库名称,多个用空格分开隔开 如("db1" "db2" "db3")
backup_location=/home/mysql/backup #备份数据存放位置,末尾请不要带"/",此项可以保持默认,程序会自动创建文件夹
expire_backup_delete="ON"#是否开启过期备份删除 ON为开启 OFF为关闭
asyn_backup="ON"#是否异步电脑备份
asyn_host="119.45.227.102"#异步电脑ip
asyn_backup_location="/home/test"#异步电脑备份路径
expire_days=30#过期时间天数 默认为三天,此项只有在expire_backup_delete开启时有效
logfile=$backup_location/mysqlbackup.log
logtmp=$backup_location/mybackup.tmp
#文件夹不存在则创建if [ ! -d $backup_location ];then
mkdir -p $backup_locationfi# 本行开始以下不需要修改
backup_time=`date +%Y%m%d%H%M` #定义备份详细时间
backup_Ymd=`date +%Y-%m-%d` #定义备份目录中的年月日时间
backup_3ago=`date -d '15 days ago' +%Y-%m-%d` #3天之前的日期
backup_dir=$backup_location/$backup_Ymd #备份文件夹全路径
welcome_msg="Welcome to use MySQL backup tools!"#欢迎语
# 判断MYSQL是否启动,mysql没有启动则备份退出
mysql_ps=`ps -ef |grep mysql |wc -l`
mysql_listen=`netstat -an |grep LISTEN |grep $mysql_port|wc -l`if [ [$mysql_ps == 0] -o [$mysql_listen == 0] ]; then
echo "ERROR:MySQL is not running! backup stop!" >>$logfile
exitelse
echo $welcome_msg >>$logfilefi# 连接到mysql数据库,无法连接则备份退出
# mysql-h$mysql_host -P$mysql_port <
#systemctl restart mysqld<
#docker exec-it $mysql_container mysql -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password <
#use mysql;
#select host,user from user where user='root' and host='localhost';
#exit
#end
#flag=`echo $?`
flag=0
if [ $flag != "0" ]; then
echo "ERROR:Can't connect mysql server! backup stop!" >>$logfile
exitelse
echo "MySQL connect ok! Please wait......" >>$logfile
# 判断有没有定义备份的数据库,如果定义则开始备份,否则退出备份if [ "$backup_db_arr" != "" ];then#dbnames=$(cut -d ',' -f1-5$backup_database)
#echo "arr is (${backup_db_arr[@]})"
for dbname in${backup_db_arr[@]}do
echo "database $dbname backup start..." >>$logfile
`mkdir -p $backup_dir`
#1.Doecker容器安装mysql备份脚本(此处不要用 -t, 会导致备份文件为空)
`docker exec-i $mysql_container mysqldump -u$mysql_user -p$mysql_password $dbname --default-character-set=$mysql_charset | gzip > $backup_dir/$dbname-$backup_time.sql.gz`
#2.直接安装mysql备份脚本
#`mysqldump-u$mysql_user -p$mysql_password $dbname --default-character-set=$mysql_charset | gzip > $backup_dir/$dbname-$backup_time.sql.gz`
flag=`echo $?`if [ $flag == "0" ];then
echo "database $dbname success backup to $backup_dir/$dbname-$backup_time.sql.gz" >>$logfileelse
echo "database $dbname backup fail!" >>$logfilefi
done# 异地计算机备份if [ "$asyn_backup" == "ON" ];then`scp -r $backup_dir @$asyn_host:$asyn_backup_location`
flag=`echo $?`if [ $flag == "0" ];then
echo "$backup_dir transport complete!"
else
echo "$backup_dir transport failed!"
fi
fi
else
echo "ERROR:No database to backup! backup stop" >>$logfile
exitfi# 如果开启了删除过期备份,则进行删除操作if [ "$expire_backup_delete" == "ON" -a "$backup_location" != "" ];then#`find $backup_location/ -type d -o -type f -ctime +$expire_days -exec rm -rf {} \;`
`find $backup_location/ -type d -mtime +$expire_days | xargs rm -rf`echo "Expired backup data delete complete!" >>$logfilefi
echo "All database backup success! Thank you!" >>$logfile
exitfi
View Code
3.复制文件到服务器并修改权限
这里我以拷贝到/home下为例,文件名为mysqldump.sh
修改权限命令:chmod u+x/home/mysqldump.sh
注:windows编辑sh文件,需要再Linux系统里更改文件格式
1.用vi命令打开文件
2.直接输入
:set ff=unix
4.创建定时任务
这里我以每天凌晨01:00执行为例
命令:00 01 * * * /home/mysqldump.sh
到这里你就完成了mysql的自动备份方法.................................................................