
One thing about an unmanaged dedicated server is that you no longer have people making backups of your sites, like you would with shared hosting. So you’ll have to make backups of your sites and files on your server yourself. Today we’ll be talking about an easy way to go about this.
We need to make backups because hard disk drives will eventually fail (it’s like the nature of them), especially when they’re running 24/7/365 in a server environment. If your server’s hard drive crashes and you don’t have a backup of your sites, then what do you do? All the time and energy you put into your sites is now gone. This is why you need to make backups, and I’m going to show you how to make it as simple as possible.
While you could certainly download a backup of all your data to your computer every once in a while, that’s just not very convenient. Not only would that take up quite a lot of time (depending on your Internet connection), you’re also bound to forget to make a backup, which just isn’t good.
So, what’s the simplest, most automated method of making a backup of our server files? I believe that Amazon S3 is our solution. Amazon S3 (Simple Storage Service) provides scalable, reliable (Amazon runs one of the largest networks in the world) and fast storage service for reasonable fees. They charge literally pennies per gigabyte. Not only is inexpensive, but its great APIs make using it for automated backups incredibly easy.
To begin setting up our automated backups, you will first have to sign up for Amazon S3 (you’ll need a credit card for this). After you have done that, SSH into your server. We will need to install some software to allow us to easily interact with Amazon S3. This software requires curl and perl, but they should already be installed on your server. Alright, start off by running these two commands:
curl timkay.com/aws/aws -o aws
perl aws --install
Now we need to create a file that will hold our S3 Access Identifiers (which can by found under Access Identifiers in your S3 account overview). Create the following file:
nano ~/.awssecret
Add your Access Key ID and Secret Access Key to this file in the following format:
ACCESS KEY ID
SECRET ACCESS KEY
Close that file and run the following command to make it more secure:
chmod -go ~/.awssecret
Amazon S3 stores files in buckets, in which you can have an unlimited number of. Without creating a bucket, you will not be able to store files (referred to as objects) in S3. Think of buckets as hard drive letters, you need them to store data. You will need to choose a unique name for your bucket. You should obviously try to make it as relevant as possible, just remember that someone may have already taken the name you want. Once you have chosen a name, run this command (replace BUCKET_NAME with the name you choose):
s3mkdir BUCKET_NAME
Now, we’re going to use a simple bash script to execute some commands to make a backup to S3. The script will first dump of your MySQL databases (and their data) to a .sql.gz file in /home. Then, it will compress your entire /home directory into a .tar.gz file (which will contain the databases). Once that has completed, the script will upload that file to Amazon S3. It will then delete the files it created on your server, and your backup will be complete.
So, create a folder called /s3backups and create a file in there called s3backup.sh:
mkdir /s3backups
cd /s3backups
nano s3backup.sh
Now, paste the following code into that file. Replace MYSQL_PASS with your root MySQL password, and BUCKET_NAME with the name of your bucket you want to upload your backups to.
#!/bin/sh
DATE=`date +%d-%m-%y`
echo "Starting backup process..."
mysqldump -u root --password=MYSQL_PASS --all-databases > /home/$DATE.sql
gzip /home/$DATE.sql
tar -czf /s3backups/$DATE.tar.gz /home
s3put --progress BUCKET_NAME/$DATE.tar.gz /s3backups/$DATE.tar.gz
rm -f /home/$DATE.sql.gz
rm -f /s3backups/$DATE.tar.gz
echo "Backup finished!"
After you have replaced those two things, you can exit out of that file. Now, we need to make this file executable, so run the following command:
chmod +x s3backup.sh
You can now test to see if the backup works correctly. Run the following command to run the backup:
sh s3backup.sh
The backup process may take awhile if you have a large amount of files. If it finishes without any errors, then you have just uploaded a backup to Amazon S3!
I did say this was going to be an automated backup. So, we’re going to set it on a cron. Personally, I run my backup once a week, which I think is a recent enough backup for my sites. You may want to run it more often, or maybe even less often, that’s up to you. I’m just going to show you how to run it on a weekly basis though. Run the following command to copy our bash file to our /etc/cron.weekly folder (which runs any contained scripts once a week):
cp s3backup.sh /etc/cron.weekly
There you have it, simple, automated server backups. Even though the process is entirely automated, you should keep an eye on it and check in on your S3 usage every once in a while (just to make sure you’re not costing yourself too much). That shouldn’t be an issue though, since my Amazon S3 bill for this month will be around $0.20. It’s (literally) a small price to pay for the comfort of knowing my websites are all backed-up and safe in an off-site location.





seo joel
March 27, 2009 8:48 am
I am never too good at managing backups, and I know that one day I am going to pay for it. I should probably put something in place now. lol