记住用户名密码
首先使用 mysql -V 查看服务器是否安装了mysql,没有一定要安装,因为脚本编写中需要mysql命令进行备份。
备份脚本:
#!/bin/bash #设置mysql的登录用户名和密码 mysql_user="用户名" mysql_password="密码" mysql_host="hostname" mysql_port="3306" mysql_charset="utf8" mysql_database="数据库名" # 备份文件存放地址(根据实际情况填写) backup_location=/data1/mysql/data/ # 是否删除过期数据 expire_backup_delete="ON" expire_days=7 backup_time=`date +%Y%m%d%H%M` backup_dir=$backup_location welcome_msg="Welcome to use MySQL backup tools! -- by ennenergy" # 判断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!" exit else echo $welcome_msg fi #备份压缩文件.gz #mysqldump -h$mysql_host -u$mysql_user -p$mysql_password -B $mysql_database | gzip > $backup_dir/$mysql_database-$backup_time.sql.gz 备份sql文件 mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -B $mysql_database> $backup_dir/$mysql_database-$backup_time.sql if [ $flag == "0" ];then echo "database $mysql_database success backup to $backup_dir/$mysql_database-$backup_time.sql.gz" else echo "database $mysql_database backup fail!" fi # 删除过期数据 if [ "$expire_backup_delete" == "ON" -a "$backup_location" != "" ];then `find $backup_location/ -type f -mtime +$expire_days | xargs rm -rf` echo "Expired backup data delete complete!" fi
第二步:添加定时任务
执行命令:crontab -e
i 进行编写,
#每分钟执行备份
*/1 * * * * cd /home/mysql/;sh mysql_backup.sh >> log.txt 2>&1
#每天12:50以及23:50执行备份
50 12,23 * * * cd /home/mysql/;sh mysql_backup.sh >> log.txt 2>&1
添加后esc,输入:wq保存退出,此时定时任务就会执行了。
目前有 0 条留言 其中:访客:0 条, 博主:0 条