Chette Soriano
home . about . contact me
Chette Soriano *
chette home
Backing up your website using bash | Print |  E-mail
Written by Chette Soriano   
Tuesday, 16 May 2006

Note: Dedicated to Bombim Cadiz, Linux guru extraordinaire, who could always explain in a few words what pages and pages of websites could not.

If you do frequent backups of your website, especially a Linux-based one at that, I'm sure you also have your share of:

  • Typing the same tar and mysql commands over and over again,
  • Opening all your config files to look for your database usernames and passwords, and
  • Basically going crazy over the numerous command switches & options.

When doing repetitive tasks such as backups, it makes more sense to use something called bash.

Bash stands for Bourne-again shell. It is very much like DOS's batch file -- it allows you to save a set of commands in a single file. This means that in you only need to run this one file, and all the commands you placed there will be executed.

A basic backup bash

1. Create the bash file in your favorite text editor.

Here is a simple bash file:

#!/bin/bash
echo "Let's back up your public_html directory!"
tar -cvzf backup.tar.gz public_html/*
echo "Your directory has been backed up. You may now download the file backup.tar.gz."

2. Save the text file. The filename that we are going to use is "backup." I removed the file extension .txt in order to make it easier to run the bash file.

3. Upload the file to your server, preferably in a directory that cannot be accessed by the public.

4. Make the file executable. You can do this via FTP by right clicking on the file, then changing the file attributes to 755.

Or if you're logged in to your shell via SSH, just enter the following commands:

chmod +x file.sh

5. Whenever you need to backup your site, all you have to do is login to your shell, and run the bash file, like so:

./backup

Spicing it up

Here's a modified backup bash for the adventurous in you:

#!/bin/bash

# Change the variables below, depending on your project.

projectname=MYPROJECT # Name of project
dbuser=myname # Database username
dbpass=mypassword # Database password
dbname=mydatabase # Database name
htmdir=public_html # Where your HTM files are located
backupdir=public_html/backups # Where you save your backup files

# Pause function
Pause()
{
echo
echo -n "Press ENTER to continue."
read
echo
}

# Welcome message
echo "Hi! This will backup the HTM and DB of this website. Simply follow the instructions."
Pause

# Ask for suffix of filename
echo "A suffix is appended to the filenames of the backup files."
echo "This is usually in the form of YYYY-MM-DD-TTTT (TTTT is the 24-hour format time)."
echo "For example: 2006-01-01-1200"
echo
echo "Enter your desired suffix now: "
read suffix
Pause

# Delete backup files
echo "Deleting existing backup files"
rm $backupdir/*
echo
echo "Backup files deleted."
Pause

# Tar HTM and DB
echo "Backing up HTM & DB files"
tar -cvzf $projectname-HTM-$suffix.tar.gz $htmdir/*
mysqldump --opt -Q -u $dbuser -p$dbpass $dbname > dump.sql
tar -cvzf $projectname-DB-$suffix.tar.gz dump.sql
rm dump.sql
echo
echo "All HTM and DB files tarred and gzipped."
Pause

# Move files to backup directory
echo "Moving files to backup directory."
mv $projectname-HTM-$suffix.tar.gz $backupdir
mv $projectname-DB-$suffix.tar.gz $backupdir
echo
echo "Files moved to the backup directory."
echo
echo "Thank you."
echo "You can now download your files from the backup directory."
echo

Trackback(0)
Comments (5)add comment

Lemuel Silva said:

la lang! Hello! nice tutorial!
May 18, 2006

Deathwing Phoenix said:

Wow ang galing naman ni chette sa linux shell. Hehehe!
May 24, 2006

Alain Chapnel said:

Tried the script in "Spicing it Up." Uploaded it in in my server, and voila! Backed up my site automatically!

This is very interesting. I am now thinking of various ways of using bash. Thank you for introducing me to shell scripting.
May 28, 2006

geisha said:

can you teach me how to do looping ... like if I have more than 1 db to backup?
October 15, 2006

mollybolly said:

I finally understood what pause is used for. thanks.
January 03, 2007

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 
< Prev   Next >
* *
Main Menu
chette's articles
photo gallery
photos on the go
.........................
bookmarks
search
about
faq
contact me
Subscribe
About Me
chette

Name: Chette Soriano
Location: Pasig City, Philippines
Quote: The Internet: Where men are men, women are men, and children are FBI agents.

www.flickr.com
This is a Flickr badge showing photos in a set called photos on the go. Make your own badge here.
Latest Articles
*

© Copyright 1997 - 2007 Chette Soriano. All rights reserved. Contact me.

* *