crontab凌晨一点备份mysql_利用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)

通过上面的查询,已经可以看到数据中数据已经恢复。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值