利用crontab每天定时备份MySQL数据库

时间:2022-11-22 10:25:39

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

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