EmmonsNet

Geek Stuff

Home Software Linux Backing Up Your Web Directory with Bash in Linux
Backing Up Your Web Directory with Bash in Linux PDF Print E-mail
Written by Frank Emmons   
Saturday, 15 August 2009 15:00

BashAs a web application developer who works numerous websites that are spread across several different servers, it really pays off to invest the time to create scripts that can automagically perform the grunt work of regular maintenance. Especially something as critical as backups.

It can be a pain and more than a just little time consuming to manually back up the code of many different websites. So in comes one of an admins best friends - bash scripting. Within a few minutes, you can have a script created and set up as a daily cron job that will prove your worth to your employers and enable you to have more free time to pursue really important things like facebook and first person shooters.

Here is a simple example bash script to back up your web directory:

#!/bin/bash

backupdate=$(date +%y-%m-%d)
filename=backup-$backupdate.tar
webpath=$HOME/public_html
backup_path=$HOME/backups

tar --create --verbose --recursion --file=$backup_path/$filename $webpath/

cd $backup_path

gzip $filename

exit

Let's go through this to give those who are not up to speed on bash a general idea of what's going on here.

First we have '#!/bin/bash'. You need this at the start of your bash scripts. This is called the 'shebang' line. It tells the shell the full path of your interpreter.

Next we are setting some variables:

backupdate=$(date +%y-%m-%d)
filename=backup-$backupdate.tar
webpath=$HOME/public_html
backup_path=$HOME/backups

We are creating variables and setting them to values that the rest of the script will need. We set the backupdate as the current date, the name of what the back up file will be - filename,  webpath - the full path of where our web directory is or the files was want to back up and finally the backup_path - the path to the directory that we want to save our archived back up files too.

Once our variable are set up, then we get right to the actions of the script.

 tar --create --verbose --recursion --file=$backup_path/$filename $webpath/

We are using the tar utility, passing it the parameters to create a new file in our back up directory from all files it finds recursively from our web directory. Tar has many different parameters than can be used. If you are unfamiliar with tar, then you really need to read up on the tar man page or type 'man tar' at the command line.

Next, the script changes directories to the back up directory and compresses our newly created archive file so we can save disk space

cd $backup_path

gzip $filename

Gzip can be considered to be the *nix equivalent to winzip on windows. It has a lot of options than can be used as well. For more information, check out the gzip man page or type 'man gzip' at the command line.

One you have your script written, save it as something like 'websitebackup.sh' and chmod it to 755. This sets the file as executable can can be run just by typing the name of the file at the command line.

Now that you file is executable you can set it up as a cron job to run at regular intervals without you having to log into the system to run it manually.

The script above is just a basic script for back up directories. There are a million possibilities for what a linux user can do here to expand upon it. I encourage others to experiment and always RTFM!

If you are just beginning to learn about bash scripting or want to know more, the read up at this excellent BASH Programming - Introduction How To.

Last Updated on Saturday, 15 August 2009 14:24
 
Share/Save/Bookmark