2018.02.02-MySql备份
@(技术-Linux)[Linux, MySql]
在Linux服务上使用mysqldump
定时做数据备份,操作步骤如下:
①编辑数据备份脚本mysql.merry.backup.sh:
#!/bin/bash
#设置mysql备份目录
folder=/apps/backups/mysql
cd $folder
day=`date +%Y%m%d`
rm -rf $day
mkdir $day
cd $day
#数据库服务器
host=127.0.0.1
user=test
password=test
db=merry
#数据要保留的天数
days=30
mysqldump -h$host -u$user -p$password $db>merry.sql
zip merry.sql.zip merry.sql
rm merry.sql
cd ..
day=`date -d "$days days ago" +%Y%m%d`
rm -rf $day
echo "backup merry db to $folder/$day/merry.sql.zip successful"
②将脚本mysql.merry.backup.sh添加运行权限:
chmod +x mysql.merry.backup.sh
③将脚本添加到crond任务,定时执行:
echo "00 00 * * * root /apps/mysql.merry.backup.sh" >> /etc/crontab