利用crontab每天定时备份MySQL数据库
当数据库服务器建立并正式投入生产使用后,我们不得不担忧一个问题:当数据库遭到破坏后,怎样安然恢复到最后一次正常的状态,使得数据的损失达到最小。
我这里以本博客的wordpress数据为例,来讨论并实现全自动话的数据备份。
一、配置备份任务
1、建立自动备份脚本
为了使数据库备份和恢复的符合我们的实际要求(备份保留七天,每天凌晨备份一次),用一段符合要求的Shell脚本来实现整个备份过程的自动化。
[root@mysqltest ~]# vim mysql-backup.sh
#!/bin/bash
##作者:Barlow##
##最后修订:2013-6-25##
#脚本作用:备份Mysql数据库
#
#设定备份保留天数K
K=7
#
TODAY=`date '+%Y%m%d'`
KDAY=`date -d "$TODAY - $K day" '+%Y%m%d'`
BACKDIR=/var/mysqlbak/$TODAY
KDAYDIR=/var/mysqlbak/$KDAY
mkdir -p $BACKDIR
#
# The Password of MySQL
ROOTPASS=******* ##将*替换为实际mysql数据库的root密码
#
# Get the Name of Database
DBLIST=`ls -p /var/lib/mysql | grep / | tr -d /`
#
# Backup with Database
for dbname in $DBLIST
do
mysqlhotcopy $dbname -u root -p $ROOTPASS $BACKDIR | logger -t mysqlhotcopy
done
#
#删除过期备份
if [ -d "$KDAYDIR" ];then
rm -rf $KDAYDIR
exit
fi
改变脚本权限,root具有完全权限,其他用户没有任何权限:
[root@mysqltest ~]# chmod 700 mysql-backup.sh
运行一次脚本:
[root@mysqltest ~]# ./mysql-backup.sh
查看运行结果:
[root@mysqltest ~]# ll /var/mysqlbak/20130625/
mysql/ wordpress/
[root@mysqltest ~]# ll /var/mysqlbak/20130625/
总用量 8
drwxr-x---. 2 mysql mysql 4096 6月 25 14:26 mysql
drwxr-x---. 2 mysql mysql 4096 6月 25 14:26 wordpress
可以看到备份已经成功完成。
2、创建自动任务每天运行
[root@mysqltest ~]# crontab -e
00 01 * * * /root/mysql-backup.sh
##每天凌晨1点运行一次
二、测试备份结果
1、创建恢复环境
在另外一台服务器上安装mysql数据库,并创建恢复数据库:
[root@mysql2 ~]# yum -y install mysql-server mysql-devel
[root@mysql2 ~]# mysqladmin -u root password your_password
[root@mysql2 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.1.66 Source distribution
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database wordpress;
Query OK, 1 row affected (0.01 sec)
mysql> use wordpress;
Database changed
mysql> show tables;
Empty set (0.00 sec) ##当前数据库中是没有数据的
mysql> exit
[root@mysql2 ~]# service mysqld restart
2、拷贝备份数据恢复数据库目录
[root@mysql2 ~]# scp -r barlow@mysqltest:/var/mysqlbak/20130625/wordpress /var/lib/mysql/
##目标路径根据安装时指定的数据库存放路径不同可能不同。
[root@mysql2 ~]# chown -R mysql.mysql /var/lib/mysql/wordpress ##修改所有者和属组为mysql
[root@mysql2 ~]# chmod 700 /var/lib/mysql/wordpress ##改变文件夹权限为700
[root@mysql2 ~]# chmod 660 /var/lib/mysql/wordpress/* ##改变文件权限为660
3、测试
[root@mysql2 ~]# service mysqld restart
[root@mysql2 ~]# mysql -u root -p wordpress
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.1.66 Source distribution
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show tables;
+-----------------------+
| Tables_in_wordpress |
+-----------------------+
| wp_commentmeta |
| wp_comments |
| wp_links |
| wp_options |
| wp_postmeta |
| wp_posts |
| wp_term_relationships |
| wp_term_taxonomy |
| wp_terms |
| wp_usermeta |
| wp_users |
+-----------------------+
11 rows in set (0.00 sec)
通过上面的查询,已经可以看到数据中数据已经恢复。
利用crontab每天定时备份MySQL数据库的更多相关文章
- linux下使用crontab定时备份MYSQL数据库的方法:
摘要 linux下使用crontab定时备份MYSQL数据库的方法: 只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: ------------------------ ...
- Linux下定时备份MySQL数据库的Shell脚本
Linux下定时备份MySQL数据库的Shell脚本 对任何一个已经上线的网站站点来说,数据备份都是必须的.无论版本更新还是服务器迁移,备份数据的重要性不言而喻.人工备份数据的方式不单耗费大量时间 ...
- centos7-每天定时备份 mysql数据库
centos7-每天定时备份 mysql数据库 第一步:编写数据库备份脚本database_mysql_shell.sh #!/bin/bash DATE=`date +%Y%m%d%H%M` #ev ...
- 定时备份mysql数据库的shell脚本
最近项目需要定时备份mysql数据库的数据,根据需求写了一份定时备份mysql数据库的脚本. -h mysql的地址 默认为localhost -P 端口号 默认为3306 -u 用户 默认为r ...
- 如何定时备份Mysql数据库
1.创建备份数据库存储目录 cd data/db mkdir backup #创建存储目录 2.添加备份脚本 vim backupdb.sh #创建脚本文件 脚本内容如下: #!/bin/sh db_ ...
- Linux shell实现每天定时备份mysql数据库
每天定时备份mysql数据库任务,删除指定天数前的数据,保留指定天的数据: 需求: 1,每天4点备份mysql数据: 2,为节省空间,删除超过3个月的所有备份数据: 3,删除超过7天的备份数据,保留3 ...
- Centos使用crontab自动定时备份mysql的脚本
在我们网站上线之后免不了需要备份数据库,为什么要备份呢?我给大家列出了3个理由. 1.防止数据丢失 2.防止数据改错了,可以用来恢复 3.方便给客户数据 以 上几点告诉我们要经常备份,当然我今天给大家 ...
- 【mysql】备份篇1:使用系统计划任务+mysqldump 定时备份mysql数据库 不用输入密码自动导出sql文件
项目部署在服务期上之后,有了新的需求,需要每月定时备份mysql数据库的所有数据! 查找了网上的多篇文章之后,自己又对bat文件中的mysqldump语句进行改进,可以实现了不用输入密码就能自动定时备 ...
- Centos定时备份 MySQL数据库
一.编写数据库备份脚本 backupmysql.sh #!/bin/bash # Name:bakmysql.sh # This is a ShellScript For Auto DB Backup ...
随机推荐
- office2016系列产品关闭时卡顿
关闭Print Spooler服务其方法如下: win+r键–>输入services.msc点击确定如下图 单击word中的文件-选项-加载项-COM加载项-转到,将所有加载项前面的勾都去掉(不 ...
- 配置阿里云的金融云上的rsync
论坛里看到易淘发的教程, 转载过来
- 经典论文翻译导读之《Google File System》(转)
[译者预读] GFS这三个字母无需过多修饰,<Google File System>的论文也早有译版.但是这不妨碍我们加点批注.重温经典,并结合上篇Haystack的文章,将GFS.TFS ...
- C语言复习:编译
C接口的封装和设计专题 Win32环境下动态链接库(DLL)编程原理 比较大的应用程序都由很多模块组成,这些模块分别完成相对独立的功能,它们彼此协作来完成整个软件系统的工作.其中可能存在一些模块的 ...
- Dictionary,hashtable, stl:map有什么异同?
相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...
- TypeScript set get
private _id:number; public get id():number{ return this._id; } public set id(value:number){ this._id ...
- delphi Drag and Drop sample 鼠标拖放操作实例
Drag and Drop is a common operation that makes the interface user friendly: a user can drag/drop inf ...
- Hibernate Validator实践之一 入门篇
在后台的业务逻辑中,对数据值的校验在各层都存在(展示层,业务层,数据访问层等),并且各层校验的规则又不尽相同,如下图所示 注:该图片来自于Hibernate Validator官网 在各层中重复的校验 ...
- 关于那个.get .post .ajax ztree 还有后台servlet传递数据
servlet给前台传递data串 用的方法是 PrintWriter out = response.getWriter(); // response.sendRedirect("test. ...
- Unity AssetBundle
Unity AssetBundle爬坑手记 - 夜阑卧听风吹雨 时间 2014-09-15 16:55:00 博客园精华区原文 http://www.cnblogs.com/ybgame/p/39 ...