当数据库服务器建立并正式投入生产使用后,我们不得不担忧一个问题:当数据库遭到破坏后,怎样安然恢复到最后一次正常的状态,使得数据的损失达到最小。
我这里以本博客的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数据库的更多相关文章

  1. linux下使用crontab定时备份MYSQL数据库的方法:

    摘要 linux下使用crontab定时备份MYSQL数据库的方法: 只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: ------------------------ ...

  2. Linux下定时备份MySQL数据库的Shell脚本

    Linux下定时备份MySQL数据库的Shell脚本   对任何一个已经上线的网站站点来说,数据备份都是必须的.无论版本更新还是服务器迁移,备份数据的重要性不言而喻.人工备份数据的方式不单耗费大量时间 ...

  3. centos7-每天定时备份 mysql数据库

    centos7-每天定时备份 mysql数据库 第一步:编写数据库备份脚本database_mysql_shell.sh #!/bin/bash DATE=`date +%Y%m%d%H%M` #ev ...

  4. 定时备份mysql数据库的shell脚本

    最近项目需要定时备份mysql数据库的数据,根据需求写了一份定时备份mysql数据库的脚本. -h mysql的地址  默认为localhost -P 端口号  默认为3306 -u 用户  默认为r ...

  5. 如何定时备份Mysql数据库

    1.创建备份数据库存储目录 cd data/db mkdir backup #创建存储目录 2.添加备份脚本 vim backupdb.sh #创建脚本文件 脚本内容如下: #!/bin/sh db_ ...

  6. Linux shell实现每天定时备份mysql数据库

    每天定时备份mysql数据库任务,删除指定天数前的数据,保留指定天的数据: 需求: 1,每天4点备份mysql数据: 2,为节省空间,删除超过3个月的所有备份数据: 3,删除超过7天的备份数据,保留3 ...

  7. Centos使用crontab自动定时备份mysql的脚本

    在我们网站上线之后免不了需要备份数据库,为什么要备份呢?我给大家列出了3个理由. 1.防止数据丢失 2.防止数据改错了,可以用来恢复 3.方便给客户数据 以 上几点告诉我们要经常备份,当然我今天给大家 ...

  8. 【mysql】备份篇1:使用系统计划任务+mysqldump 定时备份mysql数据库 不用输入密码自动导出sql文件

    项目部署在服务期上之后,有了新的需求,需要每月定时备份mysql数据库的所有数据! 查找了网上的多篇文章之后,自己又对bat文件中的mysqldump语句进行改进,可以实现了不用输入密码就能自动定时备 ...

  9. Centos定时备份 MySQL数据库

    一.编写数据库备份脚本 backupmysql.sh #!/bin/bash # Name:bakmysql.sh # This is a ShellScript For Auto DB Backup ...

随机推荐

  1. DataSnap Server 客户端调用 异常

    No peer with the interface with guid {9BB0BE5C-9D9E-485E-803D-999645CE1B8F} has been registered.

  2. delphi java 日期 转换 获取Unix时间戳

    获取Unix时间戳 http://www.cnblogs.com/findumars/p/4716753.html 最简单准确一句话 Result:=IntToStr(  DateTimeToUnix ...

  3. Others-常用数学符号大全

    常用数学符号读法大全 大写 小写 英文注音 国际音标注音 中文注音 Α α alpha alfa 阿耳法 Β β beta beta 贝塔 Γ γ gamma gamma 伽马 Δ δ deta de ...

  4. css3 之border-radius 属性解析

    在css 设置样式的时候,有时候会用到将元素的边框设置为圆形的样子的时候,一般都是通常直接设置:{border-radius:5px },这样就行了,但是到底是什么意思,一直以来都没有弄明白,只是知道 ...

  5. 处于ESTABLISHED 状态的socket 却没有进程信息

    接<一次docker中的nginx进程响应慢问题定位记录> 在排查这个问题的时候,我先使用netstat 去查看,看到底是内核协议栈的连接请求没给到进程,还是进程accept链路慢了,或者 ...

  6. Haskell语言学习笔记(86)字符串格式化与插值

    String 的格式化 Text.Printf 这个模块用来处理字符串格式化. printf :: PrintfType r => String -> r printf 用于格式化字符串, ...

  7. mac shortcut

    在Windows系统中,如果你想跳到行首.行尾直接点击home.end键就可以了,但MacBook的相关快捷键就有些区别了,相关快捷键如下: Ctrl+A:到行首(达到Home键的效果) .fn键+左 ...

  8. swift和OC中frame的小差异

    //1.0 OC中 CGRect .CGPoint.CGSize 的结构如下: struct CGRect { CGPoint origin; CGSize size; }; struct CGPoi ...

  9. C#整数的三种强制类型转换int、Convert.ToInt32()、int.Parse()的区别

    .int适合简单数据类型之间的转换,C#的默认整型是int32(不支持bool型): .int.Parse(string sParameter)是个构造函数,参数类型只支持string类型: .Con ...

  10. thymeleaf 字符串的拼接