点击登录
  • 欢迎访问无限星辰技术博客,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏无限星辰吧
  • 好集导航开张了,传送门:好集导航

linux下mysql自动备份配置方法

LINUX服务器 crx349 4767次浏览 0个评论 扫描二维码

本教程有无限星辰工作室 www.xmspace.net 独家整理发布 请转载时注明源地址和作者,谢谢!

1.先运行crontab -l 查看计划任务服务是否存在,如果没有运行
yum install crond

2.在home目录(空间比较大的目录目录名自选)下新建立两个目录,一个user和mybackup,并赋予权限。
mkdir /home/user
mkdir /home/mybackup
chmod -R 755 /home/user/
chmod -R 777 /home/mybackup

3.新建 运行脚本 并保存
vim /home/user/mysql_backup.sh
并设置运行权限
chmod -R 755 mysql_backup.sh

脚本内容:

#!/bin/sh
# mysql_backup.sh: backup mysql databases and keep newest 5 days backup.
#
# Last updated: 20 March 2006
# ----------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2006 Sam Tang
# Feedback/comment/suggestions : http://www.real-blog.com/
# ----------------------------------------------------------------------
# your mysql login information
# db_user is mysql username
# db_passwd is mysql password
# db_host is mysql host
# -----------------------------
db_user="root"
db_passwd="password"
db_host="localhost"
# the directory for story your backup file.
backup_dir="/home/mybackup"
# date format for backup file (dd-mm-yyyy)
time="$(date +"%d-%m-%Y")"
# mysql, mysqldump and some other bin's path
MYSQL="/usr/local/mysql/bin/mysql"
MYSQLDUMP="/usr/local/mysql/bin/mysqldump"
MKDIR="/bin/mkdir"
RM="/bin/rm"
MV="/bin/mv"
GZIP="/bin/gzip"
# check the directory for store backup is writeable
test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0
# the directory for story the newest backup
test ! -d "$backup_dir/backup.0/" && $MKDIR "$backup_dir/backup.0/"
# get all databases
all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"
for db in $all_db
do
$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd --skip-lock-tables $db | $GZIP -9 > "$backup_dir/backup.0/$time.$db.gz"
done
# delete the oldest backup
test -d "$backup_dir/backup.5/" && $RM -rf "$backup_dir/backup.5"
# rotate backup directory
for int in 4 3 2 1 0
do
if(test -d "$backup_dir"/backup."$int")
then
next_int=`expr $int + 1`
$MV "$backup_dir"/backup."$int" "$backup_dir"/backup."$next_int"
fi
done
exit 0;

或者下载:

mybackup 下载自行改名为mysql_backup.sh

4.添加计划任务
crontab -e
在打开的文件里面添加
0 05 * * 3,6 /home/user/mysql_backup.sh

说明:每周3和6 的5点执行一次。

参考语法:
43 21 * * * 21:43 执行
15 05 * * *    05:15 执行
0 17 * * * 17:00 执行
0 17 * * 1 每周一的 17:00 执行
0,10 17 * * 0,2,3 每周日,周二,周三的 17:00和 17:10 执行
0-10 17 1 * * 毎月1日从 17:00到7:10 毎隔1分钟 执行
0 0 1,15 * 1 毎月1日和 15日和 一日的 0:00 执行
42 4 1 * *     毎月1日的 4:42分 执行
0 21 * * 1-6   周一到周六 21:00 执行
0,10,20,30,40,50 * * * * 每隔10分 执行
*/10 * * * *        每隔10分 执行
* 1 * * *         从1:0到1:59 每隔1分钟 执行
0 1 * * *         1:00 执行
0 */1 * * *        毎时0分 每隔1小时 执行
0 * * * *         毎时0分 每隔1小时 执行
2 8-20/3 * * *      8:02,11:02,14:02,17:02,20:02 执行
30 5 1,15 * *       1日 和 15日的 5:30 执行

5.重启计划任务服务:
service crond restart

6.完成。

可能出现的问题:在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory
解决:1)在windows下转换:
利用一些编辑器如UltraEdit或EditPlus等工具先将脚本编码转换,再放到Linux中执行。转换方式如下(UltraEdit):File–>Conversions–>DOS->UNIX即可。
2)也可在Linux中转换:
首先要确保文件有可执行权限
#sh>chmod a+x filename
然后修改文件格式
#sh>vi filename
利用如下命令查看文件格式
:set ff 或 :set fileformat
可以看到如下信息
fileformat=dos 或 fileformat=unix
利用如下命令修改文件格式
:set ff=unix 或 :set fileformat=unix
:wq (存盘退出)
最后再执行文件
#sh>./filename

本教程有无限星辰工作室 www.xmspace.net 独家整理发布 请转载时注明源地址和作者,谢谢!


无限星辰 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明linux下mysql自动备份配置方法!
喜欢 (1)
[]
分享 (0)

您必须 登录 才能发表评论!