Note: Thank you 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
Leave a Reply