Create custom backups from your website using cURL

These days I needed a script to backup only a part of a customers website using a CRON. Most of the control panels I know allow only a complete website backup and this is not what I needed. While planning the script, I thought about a solution for webmaster without full SSH access to their hosting account. A typical situation could be:

  • A shared hosting account that allows only backups for the whole site incl. database, emails and other settings
  • No administration rights via SSH
  • A FTP host for the storage of the the backup files
  • Support for cURL and a default PHP5 configuration (sorry no more code for PHP4).

First of all we need to create a variable to define the local source directory on your website where you like to start the backup from:

The next code is to create a new class including the constructor function and some variables used in that class:

Our class will store file and directories on the remote FTP server, the next function will create a directory if it not already exists:

The following function will open a file and uploads the data to specified directory on the target server. For each file that is not stored the error number is returned, this information is used later to build up an error string.

Now we need a function that will loop through the specified base directory and will switch between directories and files; the previous functions will create the directory or upload the file.

At last there is a tiny function that will send you some short email message about the backup result. You can call the class in your PHP script:

Thats all, now you need to create a CRON job for the file that will execute the backup. If your hosting provider doesn’t provide CRON jobs, just google for “free cron jobs”.

Disclaimer:
We tested the script “successful” on two modern Linux web servers with ~100 files between 10kb and 300MB. If you have a backup with a lot of files you should think about a different solution. With every file or directory a new CURL function is called, this could result in a high server load. We are not responsible for any damage and/or data loss as the result off using this script.

If you like the tutorial, download the PHP class script on finalwebsites.com.

10 thoughts on “Create custom backups from your website using cURL

  1. Great article.
    In my opinion, cURL is one of the most under-appreciated features of PHP. It is so powerful.

  2. @corbyboy,

    you’re absolutely right, bad enough that the php curl extension is so limited. The curl lib you can use via the command line has much more features. For example you can upload multiple files with one connection ;)

  3. …oeps, I forgot to upload the file. It’s fixed right now!

  4. The link to the zip file id not working anymore. I need to download the code. Can you please tell me where can I find the code?

  5. Well if you are worried about a high server load due to multiple cURL connections, why not write a script that compresses the files into a zip first, then it could move the zip over to the backup server. You could even name the zip with a time stamp so u have various backup versions on your backup server.

  6. Hi Matt,

    creating (bigger) zips takes much more server resources…

    if my directadmin server is processing the backups the server becomes very slow…

  7. Gurpreet Singh,

    I don’t know, is there some information on the cURL website? and do you checked the php manual?

  8. Hello,

    Thanks for creating and sharing such a brilliant and helpful article.

    My situation is that I am creating an application for my client, through which he can upload hi website directory to a remote server. I have used your code in building the application.

    While testing I came to know that the code is working on only a specific version of CURL. If the CURL version is 7.10, then it doesn’t work. But if the version is 7.17, then the files are uploaded.

    My client is having different web servers to host his websites. And the version of CURL is not the same on each one of them.

    Can you throw some light on it, as how to make it version independent?

  9. you think this would be possible with scp/sftp instead of ftp?
    tia,
    greg

Comments are closed.