We often come across situations where we have to backup our website data
and often we want to automate the process.
Who this script is for
Here I am suggesting a common methodology where the requirements of a webmaster are matching all or some of the following criterian:
- You are using PHP and MYSQL based website. The script is fully applicable to Joomla, Drupal, Oscommerce, Zencart etc.
- You want to automate the backup process through cron jobs.
- You have lots of domains and you want to replicate the script with minimum efforts.
- You are having lots of subdomains under the main directory and you want to minimize the effort to backup subdomains.
- You want to exclude some files and directories.
- You want to see the logs also.
- You want to name the backups automatically according to the day, date etc of the year/month/week etc.
The script works in unix based servers. It DOES NOT WORK in windows environment.
Although almost all hosting providers provide linux environment. But you need to ensure the environment provided.
Normally in the backup process, we often forget to backup the shell script itself.
The article address all of the issues in the simple shell script. I just start explaining the parts of the shell script one by one.
Web Hosting Basics
Now let me clear the basics about hosting.
1. Mostly hosting service providers provide you a user name to log in to their system.
That login account will be situated in /home/your_user_name.
You can see this information after logging into cpanel of your site.
For that you simply login to the cpanel and see at the left or top, what you see.
For example my login name is ganeshgd so my home directory is:
/home1/ganeshgd
at some hosting servers it may be: /home/ganeshgd
This serves root of your system. Means every path will be relevant to this.
This will be referred to by: / or $HOME
So, we’ll start by taking this as root.
Also your www directory which is present at: /home/ganeshgdwww is also referred to by: /www or /public_html
2. You create a database, database user and database password through cpanel.
You need all these informations to connect to the database.
Now you need the following information about your database:
a. Your mysql database host. Most probably its localhost.
b. Your database name. Most probably its “your_user_name”_”your_database_name”.
For example my username is: ganeshgd and my database is techline so my database name will be: ganeshgd_techline .
c. Your database user name. Log into your cpanel interface to see what is the username. Most probably its: “your_user_name”_”first_7_characters_of_chosen_user_name”.
e.g I chose username as ganeshgds so it became: ganeshgd_ganeshg
You have to check for yours from your hosting account.
In cpanel you click on “MySQL Databases”. You’ll see a list of usernames, databases and the relevant sizes.
d. Password to connect to database. It should be: “bl@h…bl@h..bl@h..very..secure..password…”.
I mean very complicated to be cracked.
3. The conventions I follow for the backup are as follow:
a. The name of the files will be like as follows:
“categoriser”_”subdomain_or_db_name”_”function_of_file”.”appropriate_extension_if_any”
For example my categoriser is: wiw
subdomain/dbname is: techline
function is: backup
extension is sh
so file name will be: wiw_techline_backup.sh
Similarly rest of the names will be: “$SITE”techline_mon.tgz, “$SITE”ganeshgd_techline_db.sql etc.
b. Finally following files will be produced:
1. /home1/ganeshgd/backups/wiw_techline_Sun.tgz
2. /home1/ganeshgd/db_backups/wiw_ganeshgd_techline_Sun.sql
3. /home1/ganeshgd/log_backups/wiw_techline_Sun.log
4. /home1/ganeshgd/excluded_files/wiw_techline_excluded_files
5. /home1/ganeshgd/backup_scripts/wiw_techline_backup.sh
Do the ground work
Now, the first things first. First create the following directories. So, ganeshgd will be replaced by your account name.
a. /home1/ganeshgd/backups
This is a contains the backup archives.
b. /home1/ganeshgd/db_backups
This contains the dumps of mysql databases.
c. /home1/ganeshgd/log_backups
This contains the logs of your backusp.
d. /home1/ganeshgd/excluded_files
This contains the files which contain full paths of files to be excluded from backup for that subdomain, one per line in the files in this directory.
e. /home1/ganeshgd/backup_scripts
This contains the actual backup scripts which are responsible for taking the backups.
Step by step instructions
With this informatin in hand, now let’s go step by step:
1. You want to start at the root of your home directory so define your root as follows:
##############
###This script is for daily website Live Demo Directory Backup.
##############
ROOT=”$HOME”
2. You may want to categorise your backups in case you host more domains in the same hosting account.
I strongly suggest this as you may need it in future.
so use a categoriser: e.g in my case my site is: World is Welcome so I’ve chosen my categoriser as wiw.
We’ll define it as a site.
SITE=”wiw”
3. Now let’s say you have a number of subdomains. The subdomain directories are in: /www/your_subdomain_folder
so define the name of the subdomain folder by SUBDOMAIN. For example my subdomain is: techline.
This is main site as well. Although its main site but take it as subdomain. To ease the automation.
SUBDOMAIN=”techline”
4. Now you need to define the following variables:
####These are the directories for backup files.
BACKUP_DIR=”$ROOT/backups”
DB_BACKUP_DIR=”$ROOT/db_backups”
LOG_DIR=”$ROOT/log_backups”
EXCLUDED_FILES_DIR=”$ROOT/excluded_files”
BACKUP_SCRIPTS_DIR=”$ROOT/backup_scripts”
Now if its main site then you will define: MAIN_BACKUP_DIR as
MAIN_BACKUP_DIR=”$ROOT/public_html”
otherwise define it as
MAIN_BACKUP_DIR=”$ROOT/public_html/$SUBDOMAIN”
5. Now define the database variables:
DB_HOST=”localhost”
DB_NAME=”ganeshgd_$SUBDOMAIN”
DB_USER=”ganeshgd_ganeshg”
DB_PASS=”bl@h…bl@h…bl@h…very_complicated_password”
—-
— Now, after this you don’t need to make any changes to your backup shell script.
—-
6. I am also defining TODAY variable because I want to name my backup files automatically as Mon, Tue etc..
TODAY=`date +%a`
7. Now is the turn to define the variables which help to generate the file names automatically.
#### These are the file names including their absolute paths.
EXCLUDED_FILES=$EXCLUDED_FILES_DIR/”$SITE”_”$SUBDOMAIN”_excluded_files
BACKUP_SCRIPT=$BACKUP_SCRIPTS_DIR/”$SITE”_”$SUBDOMAIN”_backup.sh
DB_FILE=$DB_BACKUP_DIR/”$SITE”_”$DB_NAME”_”$TODAY”.”sql”
BACKUP_FILE=$BACKUP_DIR/”$SITE”_”$SUBDOMAIN”_”$TODAY”.”tgz”
LOG_FILE=$LOG_DIR/”$SITE”_”$SUBDOMAIN”_”$TODAY”.”log”
touch $EXCLUDED_FILES
This creates the file if it does not exist.
Later on you can add the files and directories in this file one per line which you want to exclude from backup.
8. The file EXCLUDED_FILES contains absolute paths of all the files one per line to be excluded from the backup archive.
So, files and directories mentioned in this file will not be backed up.
9. Now begins the actual program. The following lines generate the logs in log file defined above.
This helps to ensure that you are using the parameters and variables correctly.
### Here begins the program
echo “The backup directory is: $BACKUP_DIR .” > $LOG_FILE
echo “The Database is: $DB_NAME .” >> $LOG_FILE
echo “The Database User is: $DB_USER.” >> $LOG_FILE
echo “THe Pass is: $DB_PASS .” >> $LOG_FILE
echo “The day today is $TODAY .” >> $LOG_FILE
echo “The DB file is: $DB_FILE .” >> $LOG_FILE
echo “The Backup file is: $BACKUP_FILE .” >> $LOG_FILE
10. Now first mysql database dump is taken. The command used to take the database backup will be purely of your choice.
I use the below command.:
### Taking Mysql dump
echo “Phase I” >> $LOG_FILE
echo “Starting mysql database dump…” >> $LOG_FILE
mysqldump –host=”$DB_HOST” –user=”$DB_USER” –password=”$DB_PASS” “$DB_NAME” > $DB_FILE
if [ $? -ne 0 ]; then
echo “Some problem occurred in database backup. The error code returned is: $? .” >> $LOG_FILE
echo “Let’s see if something is there in $DB_FILE :” >> $LOG_FILE
cat $DB_FILE >> $LOG_FILE
else
echo “Database backup successful…” >> $LOG_FILE
fi
In the above code, we use $? shell variable to check the exit status of the previous commands. So that in case of failure your log file logs it down.
11. Now is the task to take the tar backup of all the files of your website directory and the mysql dump.
### Taking tgz backup
echo “Phase II” >> $LOG_FILE
echo “Starting tgz backup of entire directory…” >> $LOG_FILE
tar -czf $BACKUP_FILE -X $EXCLUDED_FILES $MAIN_BACKUP_DIR $DB_FILE $BACKUP_SCRIPT $LOG_FILE
if [ $? -ne 0 ]; then
echo “Some problem occurred in tar backup. The error code returned is $? .” >> $LOG_FILE
else
echo “tar backup successful…” >> $LOG_FILE
fi
The “-X $EXCLUDED_FILES” parameter tells tar command to exclude the files specified in the file named: $EXCLUDED_FILES
This file will be having list of files and directories specified in the $EXCLUDED_FILES file, one per line.
We are tarring our backup directory, database dump file, backup script itself and the log file generated.
z parameter to tar specifies that the archived is to be gzipped as well. So output will be .tgz instead of .tar .
Here also we use $? to check whether the backup is successful or not.
12. Now we want to see the statistics of the files generated during backups. You can see these as below:
### Printing Final Outputs
echo “########################Started###########”
echo “Here starts the Backup Statistics.”
echo “The database files stats are: ”
cd “$DB_BACKUP_DIR”
ls -lat “$SITE”_”$DB_NAME”_???”.sql”
echo “######################################”
echo “The backup files stats are: ”
cd “$BACKUP_DIR”
ls -lat “$SITE”_”$SUBDOMAIN”_???”.tgz”
echo “########Finished#######################”
The above stats will be mailed to you at the e-mail address specified in cron jobs manager.
13. Click on “Cron Jobs” in cpanel under “Advanced” section. and click on “Standard” button.
There you specify the name of the file as
/home1/ganeshgd/backup_scripts/”$SITE”techline_backup.sh
14. That’s all folks. You can also download the script and paste the contents to your webserver.
Backup Script as a whole
##############Full Script###############
##############
###This script is for daily website Live Demo Directory Backup.
##############
ROOT=”$HOME”
SITE=”wiw”
SUBDOMAIN=”techline”
####These are the directories for backup files.
BACKUP_DIR=”$ROOT/backups”
DB_BACKUP_DIR=”$ROOT/db_backups”
LOG_DIR=”$ROOT/log_backups”
EXCLUDED_FILES_DIR=”$ROOT/excluded_files”
BACKUP_SCRIPTS_DIR=”$ROOT/backup_scripts”
MAIN_BACKUP_DIR=”$ROOT/public_html/$SUBDOMAIN”
DB_HOST=”localhost”
DB_NAME=”ganeshgd_$SUBDOMAIN”
DB_USER=”ganeshgd_ganeshg”
DB_PASS=”bl@h…bl@h…bl@h…very_complicated_password”
### Produces Mon, Tue, Wed etc.
TODAY=`date +%a`
#### These are the file names including their absolute paths.
EXCLUDED_FILES=$EXCLUDED_FILES_DIR/”$SITE”_”$SUBDOMAIN”_excluded_files
BACKUP_SCRIPT=$BACKUP_SCRIPTS_DIR/”$SITE”_”$SUBDOMAIN”_backup.sh
DB_FILE=$DB_BACKUP_DIR/”$SITE”_”$DB_NAME”_”$TODAY”.”sql”
BACKUP_FILE=$BACKUP_DIR/”$SITE”_”$SUBDOMAIN”_”$TODAY”.”tgz”
LOG_FILE=$LOG_DIR/”$SITE”_”$SUBDOMAIN”_”$TODAY”.”log”
### Here begins the program
echo “The backup directory is: $BACKUP_DIR .” > $LOG_FILE
echo “The Database is: $DB_NAME .” >> $LOG_FILE
echo “The Database User is: $DB_USER.” >> $LOG_FILE
echo “THe Pass is: $DB_PASS .” >> $LOG_FILE
echo “The day today is $TODAY .” >> $LOG_FILE
echo “The DB file is: $DB_FILE .” >> $LOG_FILE
echo “The Backup file is: $BACKUP_FILE .” >> $LOG_FILE
### Taking Mysql dump
echo “Phase I” >> $LOG_FILE
echo “Starting mysql database dump…” >> $LOG_FILE
mysqldump –host=”$DB_HOST” –user=”$DB_USER” –password=”$DB_PASS” “$DB_NAME” > $DB_FILE
if [ $? -ne 0 ]; then
echo “Some problem occurred in database backup. The error code returned is: $? .” >> $LOG_FILE
echo “Let’s see if something is there in $DB_FILE :” >> $LOG_FILE
cat $DB_FILE >> $LOG_FILE
else
echo “Database backup successful…” >> $LOG_FILE
fi
### Taking tgz backup
echo “Phase II” >> $LOG_FILE
echo “Starting tgz backup of entire directory…” >> $LOG_FILE
tar -czf $BACKUP_FILE -X $EXCLUDED_FILES $MAIN_BACKUP_DIR $DB_FILE $BACKUP_SCRIPT $LOG_FILE
if [ $? -ne 0 ]; then
echo “Some problem occurred in tar backup. The error code returned is $? .” >> $LOG_FILE
else
echo “tar backup successful…” >> $LOG_FILE
fi
### Printing Final Outputs
echo “########################Started#########”
echo “Here starts the Backup Statistics.”
echo “The database files stats are: ”
cd “$DB_BACKUP_DIR”
ls -lat “$SITE”_”$DB_NAME”_???”.sql”
echo “#############################”
echo “The backup files stats are: ”
cd “$BACKUP_DIR”
ls -lat “$SITE”_”$SUBDOMAIN”_???”.tgz”
echo “###Finished#########################”
Log File Output
#########The log file will be############
The backup directory is: /home1/ganeshgd/backups .
The Database is: ganeshgd_articles .
The Database User is: ganeshgd_ganeshg.
THe Pass is: ‘what you entered’.
The day today is Sun .
The DB file is: /home1/ganeshgd/db_backups/wiw_ganeshgd_techline_Sun.sql .
The Backup file is: /home1/ganeshgd/backups/wiw_techline_Sun.tgz .
Phase I
Starting mysql database dump…
Database backup successful…
Phase II
Starting tgz backup of entire directory…
tar backup successful…
The script is ready for download in the downloads section of this site. You can download it and modify it as per your choice.