Backing Up Your Website and Database
If you’re like me and most anyone else, you’re probably not that great at backing up your PC. Which means you’re probably even worse at backing up your data on your website. For once, I’m attempting to be proactive and get the stuff backed up on my hosting provider before something bad happens.
I’ve been using a Linux host at 1and1.com for my hosting (yes, that’s a plug) and I have not had any problems. I would highly recommend them. Their prices are very reasonable as well. Depending on your provider the information below may require some modification, but these are all general Linux commands.
I’m also using a Linux box at home as the backup machine. It’s always on 24/7, but there are some equivalent Windows tools that get the job done on demand. (I’ll list some further reading for Windows users at the end.) Finally, I also assume SSH access to your hosting account.
Setting up the Host
Many web based applications (such as Wordpress) use a database to store persistent data. It’s important to dump out that data and keep a backup. Since I’m using MySQL, they provide a handy tool called mysqldump for doing just that.
I’ve created the following script called bkupdb.cron on my web host and stored it in my ~/cronjobs folder. The output is placed in the ~/backups folder (you will need to create both of these.)
#!/bin/bash
td=`eval date +%Y%m%d-%H%M%S`
/usr/bin/mysqldump --host=<db-host> --all-databases --add-drop-table -u <username> --password=<password> > ~/backups/$td-database.sql
/bin/gzip ~/backups/$td-database.sql
Replace <db-host> with the name of the machine that hosts your database. In some cases it’s the same machine as the web server. In that case you can omit that parameter.
<username> and <password> correspond to your database account, which may not necessarily be the same as your SSH account.
cleanup-files.cron also stored in the ~/cronjobs folder.
/usr/bin/find ~/backups/*sql.gz -type f -mtime +14 -exec rm -f {} \; 2> /dev/null
Make both scripts executable by running:
chmod +x ~/cronjobs/*.cron
The next step is to get the host’s server to run these scripts every day. This is where crontab comes in. Execute ‘crontab -e’ to edit your contab entry. This will probably bring up the default vi editor. If you’re not familiar with vi, just hit ‘a’ then paste this into your SSH terminal:
20 1 * * * ~/cronjobs/bkupdb.cron
25 1 * * * ~/cronjobs/cleanup-files.cron
Then press ESC, :wq, ENTER.
This will run the bkupdb.cron file at 1:20 AM and cleanup-files.cron at 1:25 AM
Pages: 1 2
August 10th, 2006 at 2:49 pm
Nice. You almost have verbatim how I have my site-backup setup.
Although for the db, an alternative some users may have is if their provider gives a link to a gzipped dump of their databases. Which is then easy to grab using ‘wget’.
August 24th, 2006 at 10:44 am
That is a pretty good idea.
I have been using a gzip dump of the database and storing it onsite and offsite from oscommerce, but an always on solution may be a more comprehensive answer.. especially when I am days away from going online with 3 more websites.
May have to get the York fix! Later bro
- T
August 29th, 2006 at 1:00 pm
For those web hosts that don’t allow you to access their DB servers via mysqldump, I created a PHP file that does the same thing and returns the SQL output. Unfortunately the version that I posted on my blog is slightly out of date…