Just a Tech Guy

How To Automate Ubuntu Linux Backups (part 2)

In this second part of HTAULB we will be visiting the topic of how to automate MySQL database backups. Although I will be the first to admit this is completely simple and a very nice way of accomplishing this task, I will also advise that it is not the most secure. I would not recommend this to anyone sharing a server or its resources with other people. You will be hard coding your SQL password into a script and although you could restrict ‘Read’ permissions from anyone else seeing it, I will advise against it on any shared machine.

Now, on with the show!

If you haven’t read the first part of this series please go back and do so, I’ll wait….

Back? Took you long enough. I had you read that because we will be doing some of the same tasks in order to automate the SQL backups.

You will need to go to the location in which you are keeping your .sh scripts. (We use /bin here at IAmRoberson)

Use the editor of your choice to edit the file using the name of your choice for the script’s name:

Add the following line for each database you wish to make a sql dump from.

mysqldump -u username -h localhost -p(password) databasename | gzip -9 > /var/backups/nameoftargetfile-$(date +%Y%m%d).db.sql.gz

Exit from the editor and chmod the file to 766. (see previous post for exact syntax.)

Add an additional line in the crontab like you did for (part 1) of this exercise.


You are done! Go back up some stuff now!

– Jason


Did this article help you any? Please feel free to leave feedback.

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:

cd /var/backups/
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

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 : 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/

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/

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.


– Jason