How To Automate Ubuntu Linux Backups (part 1)
I recently redesigned my home setup and wanted to automate my backups for my web servers and SQL server. I run Ubuntu 9.10 using a semi-typical LAMP configuration. For those of you unfamiliar with LAMP it is an acronym for Linux, Apache, MySQL, and PHP. Your first step is to determine what you want backed up. For me the /var/www and /etc/apache2 directories were my primary targets. After that you need to write a Shell script to tar/gz that directory and put it into a folder where you wish for your backups to reside. I will say the smartest thing to do is to eventually copy these files to a different location than the drive/box you’re backing up so a physical drive failure won’t take your backups with them and make this entire process moot. Sounds easy right? It is, and I’ll show you just how easy it really is.
After you’ve determined what you want backed up you need to write that shell script. Here is how to accomplish that:
From a command console on your Linux box, change to the directory you wish for your .sh script to reside. For example: /bin/
Fire up your CLI editor of choice and edit a file that will become your shell script. For me this is VIM so I would type the following:
Add the following lines, substituting my locations and file names with your own:
tar -czf web_backup.$(date +%Y%m%d).tar.gz -C / var/www/ -C / etc/apache2/
The first line changes the directory to where you will drop the backup file. I know you can do this several ways but this way you can see each step of what is being accomplished.
The second line creates a tar.gz named web_backup(today’s date).tar.gz from the /var/www and /etc/apache2 directories.
Exit and save your script.
You will need to change the permissions on the file in order to run it so type the following: (again subbing ‘script_name’ with your scripts name)
|chmod 766 script_name.sh|
You can type the script name and it should execute. Once this has been tested and you see that a tar.gz is created in the directory you’ve specified you can move to the next step, which is to automate it with Cron.
Everything work? Hope so, because we’re moving on regardless.
Next you’ll want to add this script to your crontab.
Type crontab -l to get a list of all jobs listed in your crontab. Don’t worry if it says something about one not existing, You probably don’t have one set up yet.
Type crontab -e to edit the file.
There is a great write up that Kevin van Zonneveld did at : http://kevin.vanzonneveld.net that explains how the crontab scheduling works. I’ll give you a quick review but I recommend heading over there and checking his site out to learn more.
The crontab is set up very simply. The first part of the line is a series of numbers to regulate the schedule part and the second part is the execution you would like Cron to perform at those increments.
The default syntax is : * * * * * /blah/blah.sh
The 5 * each stand for something. They are (respectively) minute | hour | day of month * (every day of month) | of month * (every month)| weekday (0-6 Sunday being 0)
So in order to perform the task desired for example every Friday at 0100 (am) you would use:
|30 1 * * 5 /bin/backup_script.sh|
Clear as mud right? 😉 You get it, I know you do. Again, go see Kevin’s site for more explanation and tips/tricks.
You could schedule it for a few minutes ahead of the current time and wait to see if it works correctly before you start depending on it.
Next post will be how to automate SQL database backups with mysqldump and Cron.
Till next time, Peace Out Yo.