Designer & Developer
Recently I launched Snospace which runs on a Media Temple dedicated server. At first it didn't really cross my mind to make backups. I'm used to managed hosting where they usually take care of this sort of stuff for you. I know it's highly important to backup your important files, but sometimes I forget... like eating vegetables.
I wanted a clear and simple solution that would backup my entire server, or at least the most vital information such as website source files and databases. For me it was really important to get these backups, without any human intervention to either an FTP account or my Amazon S3 account. Obviously in the event that something does go wrong, it should be easy to restore from a backup.
The dedicated server has Plesk installed, a fairly simple control panel to manage your server. Plesk does have scheduled backups but unfortunately it's not as stable as I would like. As Eric Nagel pointed out on his blog: "Plesk restorations SUCK. Plesk removes the current (live) site, then attempts the restore. Oh, and if the restore fails? Sorry - you have nothing now. Talk about back-asswards." Besides this major flaw, Plesk backups cannot be transferred automatically from Plesk to an FTP or Amazon S3 server. I'm sure this can be scripted but I'd rather not dive into this messy world of Plesk.
In the same blog post by Eric Nagel, he finally saved my day. He presented a script that would automatically backup everything, including databases to my Amazon S3 account. Perfect!
Indeed, the script ran perfectly. I set it up as a daily cronjob and saw that my Amazon S3 bucket was filling up with daily backup files.
However as the site grew, so did the backup files. At the same time my site had a major outage and I couldn't find out what happened. I restarted my server, checked all the logs and found out that this script timed out while trying to upload a backup file of 2.1 GB to my Amazon S3 account at 100% CPU usage. Oops!
Luckily or unfortunately (depending on how you look at it) I wasn't the only one with this problem. It seems like files larger than 2GB are not supported on 32 bit servers due to PHP's signed integer bug. And looking at the comments of the PHP class, timeouts with the Amazon S3 PHP class can occur, even with smaller files.
So this wasn't the way to go, for me at least. However I did think that Eric Nagel's script was a perfect start to getting automated backups to S3. I decided to build on his script and implement a couple features that would fix my problems, and hopefully yours too.
The improvements are:
The new version is fairly easy to setup. You can download it here. Just open it and change all the configuration variables. You can run it manually in your console by entering php /path/to/script/server_backup.php or set it up as a cronjob. I recommend running it every 2 minutes. Let it run as a user that has access to all directories you want to backup.
Restoring
Restoring is easy too. Just download the backup files from your S3 server. Upload them to your server and untar them. If you downloaded split files (.p01, .p02, etc.) then you can merge them again as one big file by running the command cat prefixoffile.* > largefile.tar.gz, then you can untar that file as well and restore your backup.
The script works for my server and is provided as-is. Please don't ask for support, always ask your system administrator for help first if you can't get it to work or you can figure it out by adjusting the script with trial & error. There are a lot of comments and debug messages provided in the script that can help you further. If not, you can also try to leave a comment below.
Good luck with your server backups!
I am currently co-owner (and designer) at Superhero Cheesecake and used to work as Senior Designer Digital Media at MTV Networks. I live in Amsterdam, The Netherlands. I was born in 1983 and in 2007 I received my Bachelor of Engineering in Mediatechnology at the Hogeschool Utrecht, University of Applied Sciences. This website is part personal blog, part portfolio and part nonsense. If you would like to know more about me, drop me a line or add me on Facebook or LinkedIn.
Andy
January 8th, 2010 at 10:39 am
There is another option to backup data to cloud storage powered by Amazon S3. Check out CloudBerry Backup http://cloudberrydrive.com/ . It is one time fee and the rest what you pay for Amazon S3
Stas
January 10th, 2010 at 2:38 pm
Hi.
Great script, going to try it out.
Just would appreciate an easier download - as currently it’s required to select the whole text then paste it in a file.
Regards.
Stas
January 10th, 2010 at 4:26 pm
Hi again.
I found a critical issue which I wanted you to notice - the current script clears lock on every exit, which means even if didn’t run because it’s already locked, it WILL run the next time.
Moving the register_shutdown() after lock check has solved the issue.
Overall the script is great, and I really think you should host it on github or google code to share it with more people.
Regards.
Lisa
February 15th, 2010 at 9:14 pm
Hi,
Great script - but it keeps timing out on me. I have one website with a lot of files (100megs), and it just stops compressing after a while. Grrr..what to do? Seemed like a perfect solution.
allaboutduncan
March 17th, 2010 at 5:28 pm
I got the script up and running. It runs fine when I manually call it from SSH as root user. It backs up and then states how many minutes until next run.
However, when I add job to my CRON using crontab and SSH - it runs continuously.
For example, after running manually. I set the process to run every 10 minutes (I’m sending 300MB files). It correctly starts 24 hours later. After it starts from CRON, it continues to run - ignoring the 24 time limit.
Any ideas?
Jordi
March 18th, 2010 at 9:47 am
It looks like a permission issue. Maybe the user your cronjob is running doesn’t have the correct permissions to backup all directories on your server. A lot of the directories are owned by the root user and another user can’t back it up. I run this cronjob specifically as root just to make sure I have all directories and files available.
Backup Your WordPress Installation Easily | HighEdWebTech
March 31st, 2010 at 6:32 pm
[...] set this to 5.) If you need to backup files larger than 2GB (a PHP limitation), check out this blog post.I like this method because once set up and set to run via Cron, you can very easily do nightly [...]
Garrett St. John
July 5th, 2010 at 9:50 pm
Genius! Thanks so much!