小型Drupal数据库备份以及大型站点MySQL备份策略分享
作者:mdxy-dxy 时间:2024-01-28 05:32:50
中小站点简单备份策略
基于drupal的中小行网站,我们可以使用backup_migrate模块,该模块提供了定期备份的功能,备份的时间、保留多少个备份等等设置,设置好之后,定期执行cron即可备份成功。 一般的Drupal小站,我们只需使用svn即可,在服务器端,我们把备份好的数据提交到svn,就可以达到备份的目的。由于Drupal的备份模块可以设置备份保留的文件份数,因此不会造成太多的备份文件,从而导致svn很大。
下面是一个简单的备份脚本,放置到站点根目录,然后加到crontab每天执行即可。
#!/bin/bash
date #start date
DRUSH_PHP=/bin/php #php path
export DRUSH_PHP
drush cron
svn st sites/default/files/backup_migrate/scheduled/ | grep '^!' | awk '{print $2}' | xargs svn delete --force
svn add sites/default/files/backup_migrate/scheduled/*
svn ci sites/default/files/backup_migrate/scheduled/ -m 'add backup files'
date #end date
crontab的设置如下
0 0 * * * cd /www/web/html/ && bash cron.sh > cron.log 2>&1
大型站点MySQL备份策略
如果是数据库稍大的站点,使用svn临时备份就略显单薄,这时需要使用MySQL备份策略,一般情况下我们需要把整个数据库都备份压缩,然后定期转移到备份数据库或者放到其他的云服务器,这里给出一个简单的PHP示例代码。
#!/usr/bin/php -q
<?php
$to = "gaoxinzhao@gmail.com";
$hostname = exec('/bin/hostname');
$mycnf = "/home/robbin/.my.cnf";
$ignore = array('information_schema', 'test', 'mysql', 'wdcpdb');
function trimw($str) {
$str = str_replace(array("n", "r", "t", " ", "o", "xOB"), '', $str);
return $str;
}
if (!file_exists($mycnf)) {
mail($to, "No .my.cnf exists on $hostname", "MySQL cannot dump because .my.cnf is missing on $hostname .") ;
exit("cant get user creds");
}
$myconf = file_get_contents($mycnf) or die( "Failed to open bmesh_admin's .my.cnf" );
preg_match( "/buser(.*)/", $myconf, $matches ) or die( mail($to, "No username in .my.cnf on $hostname", "MySQL cannot dump on $hostname"));
$usr = (explode('=', $matches[0]));
$user = trimw($usr[1]);
preg_match( "/bpassword(.*)/", $myconf, $matches ) or die( mail($to, "No password in .my.cnf on $hostname", "MySQL cannot dump on $hostname"));
$pass = (explode('=', $matches[0]));
$password = trimw($pass[1]);
mysql_connect("localhost",$user,$password) or die ("could not connect: " . mysql_error());
mysql_select_db("mysql");
$result = mysql_query("show databases");
$bpath = "/home/robbin/backup/mysql";
$btime = date("Y-m-d H:i:s");
$bstamp = strtotime($btime);
$byear = date("Y", $bstamp);
$bmonth = date("m", $bstamp);
$bday = date("d", $bstamp);
$btod = date("H-i-s", $bstamp);
while ($res = mysql_fetch_array($result))
{
$myDb = $res["Database"];
if (in_array($myDb, $ignore)) continue;
$mdir = "$bpath/$byear/$bmonth/$bday/$btod/$myDb";
$out = `mkdir -p $mdir`;
$myFile = $myDb . ".sql";
$bldCmd = "cd $mdir ; ";
$bldCmd .= "mysqldump -u$user -p$password --single-transaction --add-drop-table -R -c -Q $myDb > $myFile ;";
//$bldCmd .= "chmod 644 $myFile ; ";
//$bldCmd .= "chown root:root $myFile ; ";
$bldCmd .= "gzip -9 $myFile";
print "Backing up $myDbn";
print "Securing $myDbn";
$out = `$bldCmd`;
}
$out = `chmod 700 $bpath/$byear`;
print "$outn";
print "Backups are in $bpathn";
crontab的设置
0 1 * * * /home/robbin/bin/mysql_backup.php
此外我们需要把备份的数据还要定期传送到其他服务器上,才会避免服务器崩溃而引发数据丢失。备份及时网站才有保证,这里仅仅只是笔者的一点点操作分享,大家有更好的备份策略,欢迎共享。
标签:Drupal数据库备份
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python pexpect模块及shell脚本except原理解析
2022-11-10 18:52:04
Python CSV 文件解析和生成方法示例
2022-12-25 10:52:58
微信小程序地图(map)组件点击(tap)获取经纬度的方法
2023-09-03 21:33:37
![](https://img.aspxhome.com/file/2023/6/56196_0s.gif)
Python 实现向word(docx)中输出
2022-08-25 01:13:13
在SQL Server中处理空值时涉及的三个问题
2009-02-05 15:30:00
![](https://img.aspxhome.com/file/UploadPic/20092/2009210182215203.jpg)
python利用proxybroker构建爬虫免费IP代理池的实现
2021-10-25 21:18:25
![](https://img.aspxhome.com/file/2023/9/130629_0s.jpg)
Python数据可视化之绘制柱状图和条形图
2022-10-19 21:51:46
![](https://img.aspxhome.com/file/2023/8/97338_0s.png)
微信小程序地图定位的实现方法实例
2023-08-25 10:13:10
![](https://img.aspxhome.com/file/2023/7/56187_0s.gif)
pymongo中聚合查询的使用方法
2021-07-07 19:31:23
![](https://img.aspxhome.com/file/2023/6/112186_0s.png)
利用Echarts如何实现多段圆环图
2024-04-28 09:36:22
![](https://img.aspxhome.com/file/2023/1/132881_0s.png)
python deque模块简单使用代码实例
2022-10-16 04:08:12
Django后端接收嵌套Json数据及解析详解
2021-04-24 20:11:38
![](https://img.aspxhome.com/file/2023/1/105711_0s.png)
asp连接mysql的问题(端口)
2009-08-21 13:23:00
php进程daemon化的正确实现方法
2023-10-01 09:14:09
13个你可能未使用过的Python特性分享
2021-03-07 19:14:37
python读取图片的方式,以及将图片以三维数组的形式输出方法
2023-08-05 04:09:26
![](https://img.aspxhome.com/file/2023/5/89065_0s.jpg)
聊聊MySQL中的存储引擎
2024-01-20 08:58:11
![](https://img.aspxhome.com/file/2023/7/104417_0s.png)
使用Python编写Linux系统守护进程实例
2022-06-09 16:50:52
python写的ARP攻击代码实例
2023-05-23 19:45:46
Golang并发之RWMutex的用法详解
2024-05-09 14:52:35
![](https://img.aspxhome.com/file/2023/5/127385_0s.png)