mysql数据库定时自动备份

新建shell脚本文件bak.sh

命令vim /usr/local/dbbak/bak.sh
编辑内容如下

#!/bin/bash

# 数据库备份脚本
# author:chinoukin 2017-09-11

date=$(date +%Y%m%d)

db=jeesite
user=root
pass=

filename=$db'_'$date'.sql'
bakpath=/home/dbbak/$db

keepdays=30


echo "mysqldump start... "

if [ -e $bakpath ]
        then
                echo $bakpath is exists
else
        mkdir -p $bakpath
        echo dir $bakpath created
fi


if [ -d $bakpath ]
        then
                mysqldump -u$user -p$pass $db &> $bakpath/$filename

                find $bakpath -mtime +$keepdays -name '*.sql' -exec rm -rf {} \;

                echo "mysqldump done."

fi

修改脚本执行权限

命令:chmod 755 bak.sh

添加至定时任务

命令:crontab -e 编辑内容如下

#每日凌晨3点触发
0 3 * * * /usr/local/dbbak/bak.sh

你可能感兴趣的:(服务器)