Drupal: Shell-Script for Creating Full Backup Copy
In this article I’ll tell you something about Shell-script. You can use it to create a full backup copy of your site. It can be used as in Unix-systems as well in Windows with using of Cygwin. You can also use this script if there is no access to SSH (shared-hosting).
What does this scrip do?
A full backup copy of Drupal includes dump of a database and a file system.
Script for creating backup copy creates a dump of a file system using tar command, and dump of a database using mysqldump utility. Both of these dumps are compiled to one tar-file . Tar-file is compressed and its name includes a date when backup copy is created.
Using of the script
Before using the script, specify items you wish to backup in “Configuration” block.
By default, file name contains from value of tarnamebase variable + date.
The default value of tarnamebase variable is fullsitebackup. For instance: fullsitebackup-2006-04-14.tgz. You can redefine this by specifying the file name in P1 parameter.
How to run the script
$ sh fullsitebackup.sh
or
$ bash fullsitebackup.sh
You will be offered to enter password to the database.
Backup file is located in a folder where script was run.
Multisiting
If there are a few sites in the same system (multisiting), create an individual script for every site. Give the scripts clear names. (Example – mysite1fullbackup.sh for the mysite1 site).
You should also adjust tarnamebase variable for the full site description.
Usage notes and warnings:
- This script backs up every file and directory in the Drupal root directory. If you have a hosted site with subdomain websites contained in subdirectories a backup of the main website directory will include the subdomain websites. For backups this will only bloat your backup file. For restores this could be disastrous, since you would restoring the file systems for ALL of the website. If you have this kind of configuration you must customize the tar command to make a selective backup.
- This script does not understand Drupal multisite configurations
- This script does not lock out users while the backup is in progress
- Read access is required on all files and directories (this is sometimes a problem in hosted configurations)
- The mysqldump utility uses the –add-drop-table option so that a restore automatically replaces tables. You may wish to modify this to add an element of safety to your restore process. (e.g., tables must be manually dropped, script will not replace)
- Commands may need to be modified for your operating system (e.g., The default tar command in Solaris does not support compression)
- Because the backup is done in stages enough temporary space must be available to build the backup files
Script code
Copy the text to the file and name it fullsitebackup.sh.
Make sure that special symbols like apostrophe (“`”) and quotes are stored correctly.
#!/bin/bash
#
# fullsitebackup.sh V1.1
#
# Full backup of website files and database content.
#
# A number of variables defining file location and database connection
# information must be set before this script will run.
# Files are tar’ed from the root directory of the website. All files are
# saved. The MySQL database tables are dumped without a database name and
# and with the option to drop and recreate the tables.
#
# ———————-
# March 2007 Updates
# – Updated script to resolve minor path bug
# – Added mysql password variable (caution – this script file is now a security risk – protect it)
# – Generates temp log file
# – Updated backup and restore scripts have been tested on Ubunutu Edgy server w/Drupal 5.1
#
# – Enjoy! BristolGuy
#———————–
#
## Parameters:
# tar_file_name (optional)
#
#
# Configuration
#
# Database connection information
dbname=”drupal” # (e.g.: dbname=drupaldb)
dbhost=”localhost”
dbuser=”” # (e.g.: dbuser=drupaluser)
dbpw=”” # (e.g.: dbuser password)
# Website Files
webrootdir=”/var/www/drupal” # (e.g.: webrootdir=/home/user/public_html)
#
# Variables
#
# Default TAR Output File Base Name
tarnamebase=sitebackup-
datestamp=`date +’%m-%d-%Y’`
# Execution directory (script start point)
startdir=`pwd`
logfile=$startdir”/fullsite.log” # file path and name of log file to use
# Temporary Directory
tempdir=$datestamp
#
# Input Parameter Check
#
if test “$1″ = “”
then
tarname=$tarnamebase$datestamp.tgz
else
tarname=$1
fi
#
# Begin logging
#
echo “Beginning drupal site backup using fullsitebackup.sh …” > $logfile
#
# Create temporary working directory
#
echo ” Creating temp working dir …” >> $logfile
#mkdir $tempdir
/bin/mkdir $startdir/$tempdir
#
# TAR website files
#
echo ” TARing website files into $webrootdir …” >> $logfile
cd $webrootdir
tar cf $startdir/$tempdir/filecontent.tar .
#
# sqldump database information
#
echo ” Dumping drupal database, using …” >> $logfile
echo ” user:$dbuser; database:$dbname host:$dbhost ” >> $logfile
cd $startdir/$tempdir
mysqldump –user=$dbuser –host=$dbhost –password=$dbpw –add-drop-table $dbname > dbcontent.sql
#
# Create final backup file
#
echo ” Creating final compressed (tgz) TAR file: $tarname …” >> $logfile
tar czf $startdir/$tarname filecontent.tar dbcontent.sql
#
# Cleanup
#
echo ” Removing temp dir $tempdir …” >> $logfile
cd $startdir
rm -r $tempdir
#
# Exit banner
#
endtime=`date`
echo “Backup completed $endtime, TAR file at $tarname. ” >> $logfile
How to use the script on a hosting without SSH
You have to create the php-script and run it through your browser:
<?php system(“fullsitebackup.sh”); ?>
Rotation of backup copies files
By default, files of the following kind are created: sitename-25-08-2011.tgz. Available space on your disc can be exhausted fast. One of the simplest way to solve this problem is to define datestamp variable in the script:
datestamp=`date +’%u’`
Then the files will look like:
drupalcookbook-1.tgz – for Monday,
drupalcookbook-3.tgz – for Wednesday
etc.
These files will be rewritten with the most recent backup version in a week.
pleas,help me write a shell script which make a backup of /policy directory in the /backup directory.