FTP or Amazon S3 Server Backup PHP Script

May 8th, 2009 → 2:21 pm @ Eric Nagel // 17 Comments



I’ve got a dedicated server with GoDaddy, and have been pretty happy with things. I know how important backups are, but don’t like how GoDaddy handles it.

First, you have to set it up yourself, which is OK if you know what you’re doing in Plesk, but Plesk backups SUCK. You can’t extract a single file from them, or get the SQL commands to rebuild part the database.

Furthermore, 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.

The final nail in the coffin is the fact that you can ONLY access the GoDaddy backup FTP server from your main server. So if your main server fails, don’t worry: you have a backup – you just can’t get to it.

After all that, I decided to write my own backup script. If you want to do something right, you have to do it yourself.

I started with a basic script that saved the files an FTP server, which was a good first step. But I couldn’t find a cheap FTP server for backups, so I looked at Amazon S3. While the pricing is a bit confusing, I know it’s cheap. It looks like I can backup my entire server, daily, for about $0.02 / day. You’re not going to find a better deal than that!

Enough already! Here’s the script. It’s completely free, and probably has more comments than actual code in it. Just PLEASE don’t ask for support on this one – get a sys. admin if you need help configuring it. Or figure it out with trial & error.

Storing your data in “the cloud” can be dangerous if you don’t have permissions set right. A webinar by @WilsonMattos cleared that up for me (previous webinar available for download).

So, get your server backed up, and once you do, make sure you can successfully restore the data!


Tags:

17 Comments → “FTP or Amazon S3 Server Backup PHP Script”


  1. Rodney

    1 year ago

    Thanks for sharing your script. Is this setup to only work on Plesk servers, or do you think it might work on a WHM/cpanel server as well?


  2. Eric

    1 year ago

    If you change the paths (mainly $cVhostsDir), this should work on WHM/cpanel as well.


  3. Dash

    1 year ago

    Hi Eric, how will this script handle the number of backups in the S3 bucket? Do I manually have to delete old backups? Or is that included in the script?


  4. Eric

    1 year ago

    @Dash: the very first variable in the script is $nDays, which is “How many days of backups to keep?” It’s set to 5, but you can change that to whatever you’d like. After backing up, the script takes care of clean up.


  5. Z

    1 year ago

    Just wanted to stop by and say thank you very much for sharing this script. It’s very well commented, and worked perfectly. I set it up as a daily cron job. I now have piece of mind. Thanks a lot!

    Z


  6. RPM

    9 months ago

    Great Script!

    I have a question if you don’t mind; if i would like to run this script in the daily cron folder should i change $workingDir ?

    cheers!


  7. Eric Nagel

    9 months ago

    @RPM – everything above “Done editing script” should be looked over and probably changed, including $workingDir


  8. Niro

    8 months ago

    Eric, thanks for the excellent script.
    For some reason it gets stuck when trying to send to amazon a database file which is 400M after the compression. Top command shows cpu is at 100% on httpd process. smaller files did work.

    I tried removing the https in the s3 initialization but it did not help.

    Anyone know how to solve this?


  9. Jordi

    7 months ago

    I have the same problem as Niro. Is there some sort of file limit? The file that it’s uploading is around 2GB.


  10. Eric Nagel

    7 months ago

    Hi Niro, Jordi – I’m not sure why it’s getting stuck. According to the author of the S3 component

    Known Issues:
    Files larger than 2GB are not supported on 32 bit systems due to PHP’s signed integer problem
    SSL is enabled by default and can cause problems with large files. If you don’t need SSL, disable it with S3::$useSSL = false;

    For the database, you can change the dump to dump tables, not the whole database at once, and maybe then you’d be under 2GB. Or, take the .sql file that’s the export from the database and split it up.


  11. Jordi

    7 months ago

    Thanks for the quick response. For me unfortunately it didn’t work though. I tried to turn of SSL but that didn’t do the trick. It’s probably the 2GB file limit then because the tar.gz file is just a little over 2GB.

    For me it’s not the database dump that’s causing problems. That’s actually uploading correctly. The problem lies with the complete file dump, probably have to many images :D

    I’ll have to try something else then. Maybe I’ll have to split it into different files. Too bad automated backups with an option to upload to S3 isn’t baked into Plesk.

    Thanks again!


  12. Jordi

    7 months ago

    I wrote a new version which fixed my large file problems and the timeout issue. Hopefully this will fix your problem too Niro, or anyone else that stumbles on the same issues :)

    http://www.jor-on.com/blog/2010/01/07/php-amazons3-ftp-server-backup-script

    My server is backing up again, I feel much saver now hahaha :D


  13. Eric Nagel

    7 months ago

    Nice job, Jordi! Thanks for not only improving on the backup script, but giving it away, too!

    @Niro – take a look at what Jordi did, and you should be able to backup your server now

  14. [...] the faint of heart, or those not comfortable with working in a Linux/Unix command line environment. This script by Eric Nagel grabs your databases (not just WordPress, any of them you want), your web directories [...]


  15. Adam J

    4 months ago

    Does this back up the plesk settings?
    i.e. dns/ ssl certs/ email accounts etc?


  16. Eric Nagel

    4 months ago

    Hi Adam – no, it does not. Also, Plesk is not required… this script will backup just about any configuration (I just happen to have & use Plesk)


  17. Adam J

    4 months ago

    Hi Eric,

    I use plesk as well and recreating and having to re-enter all those settings would take me a long time as I have many domains and email accounts.


Leave a Reply