备份脚本
#!/bin/bash
#author cheng
#mysql数据自动备份
mysql_user=“root”
mysql_password=“passwprd”
mysql_host=“localhost”
mysql_port=“3306”
mysql_charset=“utf8mb4”
#备份文件存放地址(根据实际情况填写)
backup_location=/usr/cheng/msg_manager/sql
#是否删除过期数据
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!”
#判断mysql实例是否正常运行
mysql_ps=ps -ef |grep mysql |wc -l
mysql_listen=netstat -an |grep LISTEN |grep $mysql_port|wc -l
if [ [
m
y
s
q
l
p
s
=
=
0
]
−
o
[
mysql_ps == 0] -o [
mysqlps==0]−o[mysql_listen == 0] ]; then
echo “ERROR:MySQL is not running! backup stop!”
exit
else
echo $welcome_msg
fi
#备份指定数据库中数据
mysqldump -h
m
y
s
q
l
h
o
s
t
−
P
mysql_host -P
mysqlhost−Pmysql_port -u
m
y
s
q
l
u
s
e
r
−
p
mysql_user -p
mysqluser−pmysql_password -B msg_manager>
b
a
c
k
u
p
d
i
r
/
m
s
g
m
a
n
a
g
e
r
−
backup_dir/msg_manager-
backupdir/msgmanager−backup_time.sql
flag=echo $?
if [ $flag == “0” ];then
echo “database msg_manager success backup to
b
a
c
k
u
p
d
i
r
/
m
s
g
m
a
n
a
g
e
r
−
backup_dir/msg_manager-
backupdir/msgmanager−backup_time.sql”
else
echo “database msg_manager backup fail!”
fi文章来源:https://www.toymoban.com/news/detail-666147.html
#删除过期数据
if [ “
e
x
p
i
r
e
b
a
c
k
u
p
d
e
l
e
t
e
"
=
=
"
O
N
"
−
a
"
expire_backup_delete" == "ON" -a "
expirebackupdelete"=="ON"−a"backup_location” != “” ];thenfind $backup_location/ -type f -mtime +$expire_days | xargs rm -rf
echo “Expired backup data delete complete!”
fi文章来源地址https://www.toymoban.com/news/detail-666147.html
添加定时任务
crontab -e
# 添加定时任务(每天12:50以及23:50执行备份操作)
50 12,23 * * * /usr/cheng/msg_manager/sql/sh/mysql_backup.sh >> /usr/cheng/msg_manager/sql/sh/exec.log 2>> /usr/cheng/msg_manager/sql/sh/exec_err.log
重启定时任务服务
service crond restart
到了这里,关于mysql自动备份脚本的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!