POLL: POWERSHELL VS. GUI - DO YOU WANT TO BE A DEVOP OR AN ADMIN?

Automatic backup of your weblog or your CMS

This article describes how to automatically backup a weblog system or CMS which uses MySQL. A shell script backups all your data on the server, and a batch file will automatically download it all to your local PC.

spacer spacer By Michael Pietroforte | Mon, March 20, 2006 - 0 comments

Michael Pietroforte is a Microsoft Most Valuable Professional (MVP) with more than 28 years of experience in system administration. g+

Many bloggers invest a lot of time in their weblogs. Usually they don’t care much about backup because they think that their hosting company is responsible for this. This is a bit careless since there are many cases where this doesn’t help at all. Let’s say that you upgraded your weblog software or you installed a malfunctioning plug-in and then finding out, nothing works anymore afterwards. It could also happen that you want to go back to an earlier version of your configuration. Or maybe some bad guy got access to your site and deleted everything. This article describes how to backup your weblog or your website automatically without any kind of user intervention.

Some weblog systems have a “backup function”. Usually they export all your entries to a text file. I wouldn’t consider this a real backup. All settings, your templates and the images are not secured. This kind of backup needs to be run manually, which is a disadvantage because in my experience, most people forget to do this regularly. Any kind of backup which doesn’t run automatically is not really a backup solution worth considering in my view.

This guide assumes that you have access to the shell of the machine where your weblog is hosted. If this isn’t the case, I recommend changing your web hosting package. Another requirement is that your blog or CMS uses MySQL as database system which is the case for most Open Source solutions.

The backup procedure discussed here has two parts. One part runs on the server and the other part on your desktop computer. The server part is a shell script:
#!/bin/bash
DATE=`date +"%Y%m%d"`
tar -c $HTML | gzip -c > backup/$DATE.tar.gz
mysqldump --add-drop-table -h localhost --user=$USER --password=$PW -B $DBNAME | gzip -c > backup/$DATE.sql.gz

$HTML is the name of the folder where all files of your weblog or website are stored. Replace $USER, $DBNAME and $PW with the details of your MySQL system. Copy the script to a file named “backup.sh” and upload it to your server. The file has to be executable. You can do this on the shell on your server:
chmod 740 backup.sh

You also have to create a subfolder named “backup”. The backup script has to run automatically at least once a day. For this you have to setup a cron job:
crontab -e

Move the cursor to the end of the last line and then press “a”. Copy this line:
* 7 * * * $HOME/backup.sh

Press ESC and then SHIFT+ZZ.

Every morning at 7 the backup script will be started. All files of your website are copied to one compressed archive file. The name of the backup files will start with the date of the day where the script starts. This way, you can easily restore your system to the state of a certain date.

Now comes the next part of this backup procedure. The backup files have to be copied to your desktop computer. If you keep them on your server only, the backup becomes useless when someone deletes them there. You could copy them manually to your PC, but I don’t recommend this. It is easy to do this automatically:

First of all, you need two programs for your Windows computer: pscp and plink. Both are free tools and can be downloaded here. Create a new folder named backup on your hard drive and then create a batch file named “backup.bat”. Copy these lines to the file:
pscp -pw PASSWORD USER_NAME@MY_DOMAIN.com:/backup/* c:\backup
plink MY_DOMAIN.com -l USER_NAME -pw PASSWORD rm backup/*

Replace PASSWORD, USER_NAME and MY_DOMAIN with your data. This batch file will download all backup files to your computer. Afterwards it deletes them on the server.

Now we have to find a way to start the backup batch file automatically. One way is to run the backup script every time your PC starts. Create a link to the backup batch file: Right click on backup.bat and then drag and drop it to the folder below selecting “Create Shortcuts Here” in the context menu:
C:\Documents and Settings\YOUR_USERNAME\Start Menu\Programme\Startup\

If you don’t have a permanent internet conenction, you can schedule a task under Windows. Microsoft Technet has a detailed description about it.

Congrats! You are done! You have a fully automated backup solution for your weblog now. I recommend checking the backup files every now and then. To open the compressed files on your Windows computer you need a file archiver like 7-zip.

I also recommend trying a restore on a test system. Upload the files and then uncompress them:
gzip -d $DATE.tar.gz
tar xvf $DATE.tar
gzip -d $DATE.sql.gz
tar xvf $DATE.sql

Then you have to logon on to your MySQL system and import the data:
mysql -p
(enter your password)
source $DATE.sql

If you can’t logon to your MySQL system this way, you can use a tool like phpMyAdmin to import your weblog data. Most hosting companies offer this.

Believe me; you will sleep better with daily backups of your weblog. I hope this description was detailed enough. Let me know, if you have problems.

Subscribe to our newsletter for sysadmins. We will not spam you! Mails deliverd by Google.

Comments are moderated. Please watch your language! Cancel reply

Name and Email required. Your email address will not be published.

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.