Home>

In order to prevent data loss caused by web server failure,Database backups are very important,In order to avoid major losses.This article analyzes the backup strategy of small drupal sites and the mysql backup strategy of large sites.

Simple backup strategy for small and medium sites

Small and medium-sized website based on drupal,We can use the backup_migrate module, which provides the function of regular backup,How long to back up, how many backups to keep, etc.Once set up,Perform cron regularly to back up successfully. For general drupal stations, we only need to use svn. On the server side,We can submit the backup data to svn to achieve the purpose of backup.Because Drupal's backup module can set the number of files to keep in backup,So it does n’t create too many backup files,As a result, svn is very large.

Here is a simple backup script,Placed in the site root directory,Then add it to crontab and execute it daily.

#!/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 settings are as follows

0 0 * * * cd/www/web/html /&&bash cron.sh&cron.log 2>&1

Large site mysql backup strategy

For sites with a slightly larger database,Using svn temporary backup is a little bit thin,You need to use the mysql backup strategy.In general, we need to back up and compress the entire database.Then periodically transfer to a backup database or other cloud server,Here is a simple php example code.

#!/usr/bin/php -q
<?php
$to="[email protected]";
$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 settings

0 1 * * * /home/robbin/bin/mysql_backup.php