About the author
Boaz I'm a software developer, working at a small company in the Netherlands. Currently I'm mostly using techniques like .NET, C#, SQL and jQuery, but I have experience with JAVA and PHP as well.

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

I read somewhere that it's very easy to backup a site using wget on linux. That made me realize that, at the moment, I do not have any backups of my sites. The only backups that (hopefully) exist are the ones my shared host creates. So I figured it would be safe to have my own backups. Since I run my own Ubuntu home-server, already for a few years now, I have a backup target too.

The only problem left is: the databases. Luckily dasBlog does not use a database to store it's posts, but a XML datastore. This makes it less painful to make a backup, especially on shared hosting. My MySQL databases can be accessed externally, so they can be backuped using a (separate) script too. Currently I already do this for my e-mail database.

So, I created the following script:

cd "$(dirname "$0")"

backupfile=$(date +%Y%m%d)
savetodir=./backup-$(date +%Y)/

if [ -d $host ]; then
    rm -r $host

wget --recursive --level=inf --quiet --user=$user --password=$pass ftp://$host/

if [ $? -ne 0 ]; then
    echo "wget faild during the backup of $host" >&2
    exit 1

tar -cf $backupfile.tar $host && gzip -c $backupfile.tar > $backupfile.tar.gz && rm $backupfile.tar

if [ $? -ne 0 ]; then
    echo "faild to create a tar.gz during the backup of $host" >&2
    exit 1

if [ ! -d $savetodir ]; then
    mkdir $savetodir

mv $backupfile.tar.gz $savetodir

exit 0
Fist a few variables are set, like the ftp site, username and password. I created a special backup user that only has read privileges, since no writing is required when making backups.

By default wget creates a directory named after the host, so I start with removing that directory and it's contents but only if it exists (from a previous backup).

This is follwed by the wget command that does a fully-recursive download (note the: --level=inf) after finishing the exit code of wget is checked for errors. The backup is then compressed and stored in a directory per-year.

Finally I scheduled a cronjob, to automatically create a backup once a week:
0 3 * * 3 boaz sh /mnt/array1/backup/n3rd.nl-sohosted/backup.sh

November 21, 2011 - Comments [0] - Posted in Backup | Ubuntu
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview