Sử dụng Drush để Backup scheme theo plan cho Drupal sites

Sử dụng Drush để Backup scheme theo plan cho Drupal sites

Everyone needs to have a backup plan for their live site. Not only can your server's disk get corrupted, but you can also erroneously overwrite your site with bad code or bad data, or your site can get hacked. Detecting the latter situations takes some time. Hours or days.

For this reason, you should have multiple backup copies at multiple time points.

The most convenient scheme is to have a 7 day sliding backup: that is, you have one backup snapshot for each day of the week, with today's backup overwriting the backup from 8 days ago.

The scheme we describe in this post uses the extremely flexible, popular and useful Drush the command line swiss army knife for Drupal.

It has the following advantage:

No need to know what user, password and host MySQL is running on.

Single self contained all-in-one backup file for database, Drupal and static files.

7 Day sliding backups.

Note: The use of Drush for backup, or any other scheme that uses mysqldump, are suitable for sites that do not have a large database. If your site's database is large, then you may want to explore other backup schemes, such as using MyDumper for Fast Parallel MySQL backups.

Install Drush

You can install Drush using Composer, as discribed on Drush's github. Or you can install it via PHP PEAR.

Installing from PEAR is as follows:

First, install the PHP PEAR package from Ubuntu's repositories.

aptitude install php-pear

Then install dependencies:

pear upgrade --force pear

pear install Console_GetoptPlus

pear install Console_Table

Finally install Drush itself.

pear channel-discover pear.drush.org

pear install drush/drush

Now that you have Drush installed, we proceed for using it for backup.

Create A Backup Directory

We now create a directory to hold the backups, and change its permissions so no one could read it but someone with root access.

sudo mkdir /home/backup

sudo chmod 700 /home/backup

Create a Drush alias file

Then create an aliases file for Drush to know where your site is installed. Change example.com below to your real directory where the site is installed and its real URL.

<?php

# This file should be in ~/.drush/aliases.drushrc.php

$aliases['live'] = array(

  'uri'  => 'http://example.com',

  'root' => '/home/example.com/www',

);

Creating the drush backup script

Now create a file in /usr/local/bin/daily-backup.sh

#!/bin/sh

# Daily backup of the database, using Drush

#

# Backup directory

DIR_BACKUP=/home/backup

log_msg() {

  # Log to syslog

  logger -t `basename $0` "$*"

  # Echo to stdout

  LOG_TS=`date +'%H:%M:%S'`

  echo "$LOG_TS - $*"

}

DAY_OF_WEEK=`date '+%a'`

BACKUP_FILE=$DIR_BACKUP/backup.$DAY_OF_WEEK.tgz

log_msg "Backing up files and database to $BACKUP_FILE ..."

drush @live archive-dump \

  --destination=$BACKUP_FILE \

  --preserve-symlinks \

  --overwrite 

RC=$?

if [ "$RC" = 0 ]; then

  log_msg "Backup completed successfully ..."

else

  log_msg "Backup exited with return code: $RC"

fi

Make the script executable:

chmod 755 /usr/local/bin/daily-backup.sh

Scheduling the daily backup

Now add the script to cron so as to run daily, for example at 2:30 am

30 2 * * * /usr/local/bin/daily-backup.sh

After a few days, you should see backups in the directory, kept for 7 days:

ls /home/backup/

backup.Fri.tgz

backup.Thu.tgz

It is also a good idea to copy the backup files to another server, ideally not in the same datacenter as the live site. At least do this weekly using an addition to the script using scp or rsync.

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Composer, học Composer trong 5 PHP Components của Drupal 8

Composer, học Composer trong 5 PHP Components của Drupal 8

Drupal is not the only system changing though. PHPBB, EZPublish and even non-product PHP systems

6 lý do khiến blog bị đánh giá "thiếu chuyên nghiệp"

6 lý do khiến blog bị đánh giá "thiếu chuyên nghiệp"

Có đôi khi bạn thấy trang blog của mình quá đơn giản, nhưng độc giả lại thấy nó quá phức tạp. Do đó thiết nghĩ phải dành thời gian để chỉnh lí những yếu tố ...

Xôn xao thông tin dịch vụ của Baidu tại Việt Nam tự ý can thiệp vào máy tính của người dùng

Xôn xao thông tin dịch vụ của Baidu tại Việt Nam tự ý can thiệp vào máy tính của người dùng

Do chưa được cấp phép, phải sau 2 tuần nữa, người dùng Việt Nam mới có thể sử dụng những tính năng mới của mạng xã hội “Baidu Trà đá quán”. Trong khi đó, theo phản ánh của nhiều bạn đọc, sau khi truy cập vào một số website của “gã khổng lồ” Baidu đang hoạt động tại Việt Nam, máy tính của họ đã gặp phải những hiện tượng lạ.

Công ty diệt chuột T&C

 

Diet con trung