利用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 ...
随机推荐
- 报表UI测试点
1.功能完整性:是否实现了产品需求功能 2.数据准确性:UI显示数据,是否与后端传过来的数据一致 3.页面兼容性:浏览器兼容.布局 4.分页查询 5.数据格式一致性:小数精确位.百分比保留位数等 6. ...
- ADOQuery.Parameters: Property Parameters does not exist
Exception class EReadError with message 'Property Parameters does not exist'. Exception class EReadE ...
- C++Builder debug 程序的时候 structure required
C++Builder debug 程序的时候, deub一个变量 dm->avar; E2288 Pointer to structure required on left side of -& ...
- vue中修改了数据但视图无法更新的情况[转载]
我们有时候常碰到vue中明明修改了数据,但是视图无法更新,因此我总结了一点点碰到此类的情况: 1.v-for遍历的数组,当数组内容使用的是arr[0].xx =xx更改数据,vue无法监测到 数组数据 ...
- Mp4 to Img
# -*- coding: utf-8 -*- """ Created on Thu May 3 16:51:50 2018 """ # 录 ...
- 一个rcu回调导致的简单死锁
在自有模块的处理中,我们设计了一个内核线程去做gc, 但同时,我们又用到了rcu,rcu中也会去抢gc的锁,由于该锁用的spin_lock,而不是spin_lock_bh,并没有关软中断,所以在rcu ...
- 【360】pandas.DataFrame、array、list 之间转换
pandas.DataFrame → array → list values 可以转成 array array.tolist() 可以转成 list >>> c 0 1 2 0 0 ...
- 如何使用JDBC查询指定的记录
//连接数据库 public class JdbcDao { private Connection conn=null; private String strSql=null; publi ...
- oracle函数大全-字符处理函
字符函数——返回字符值 这些函数全都接收的是字符族类型的参数(CHR 除外)并且返回字符值.除了特别说明的之外,这些函数大部分返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据库类 ...
- GIS案例学习笔记-CAD数据分层导入现有模板实例教程
GIS案例学习笔记-CAD数据分层导入现有模板实例教程 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 1. 原始数据: CAD数据 目标模板 2. 任务:分5个图层 ...