Mini-Drupal Database backup and a large site MySQL backup strategy tutorial

Source: Internet
Author: User
Tags chmod explode line web php sample code svn drupal mysql backup

Small and medium site simple backup policy

The small and Medium line Web site based on Drupal, we can use the Backup_migrate module, which provides the function of regular backups, the time of backup, how many backups to keep, and so on, Once you've set it up, you can backup successfully by performing Cron regularly. The General Drupal Station, we only need to use SVN can, on the server side, we put back the good data to SVN, we can achieve the purpose of the backup. Because Drupal's backup module can set up the number of copies that are reserved for backups, it does not cause too many backup files, resulting in svn being large.

The following is a simple backup script, placed in the root directory of the site, and then added to the crontab daily execution.

The code is as follows Copy Code
#!/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 $} ' | 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



The crontab settings are as follows

The code is as follows Copy Code

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



MySQL backup strategy for large site

If the database is slightly larger site, using SVN temporary backup is slightly thin, then need to use the MySQL backup strategy, we need to backup the entire database compression, and then periodically transferred to the backup database or to other cloud servers, here a simple PHP sample code.

The code is as follows Copy Code

#!/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 are missing on $hostname.") ;
Exit ("Cant get user creds");
}

$myconf = file_get_contents ($mycnf) or Die ("Failed to open bmesh_admin '. 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 $bpath/$byear ';
print "$outn";
Print "Backups are in $bpathn";



Settings for Crontab

The code is as follows Copy Code

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



In addition, we need to transfer the backed-up data to other servers on a regular basis to prevent the server from crashing and causing data loss. Backup timely site only to ensure that this is just the author of a little bit of operation to share, we have a better backup strategy, welcome to share.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.