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:

class Curl_ftp_backup {

	public $basePath, $errors, $ftp_server, $ftp_user_pw , $msg, $email;

	public function __construct($ftp_server, $ftp_userpw, $email_adr) {
		$this->basePath = BASEPATH;
		$this->errors = '';
		$this->ftp_server = $ftp_server;
		$this->ftp_user_pw = $ftp_userpw;
		$this->email = $email_adr;
		$this->msg = '';

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

	private function curl_create_ftp_directory($name, $curr_dir = '') {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->ftp_server);
		curl_setopt($ch, CURLOPT_USERPWD, $this->ftp_user_pw);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$cmd = array();
		if ($curr_dir != '') $cmd[] = 'CWD '.$curr_dir;
		$cmd[] = 'MKD '.$name;
		curl_setopt($ch, CURLOPT_POSTQUOTE, $cmd);
		curl_exec ($ch);

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.

	private function curl_put_file($path) {
		$ch = curl_init();
		$fp = fopen($this->basePath.$path, "r");
		curl_setopt($ch, CURLOPT_URL, $this->ftp_server.$path);
		curl_setopt($ch, CURLOPT_USERPWD, $this->ftp_user_pw);
		curl_setopt($ch, CURLOPT_UPLOAD, 1);
		curl_setopt($ch, CURLOPT_INFILE, $fp);
		curl_setopt($ch, CURLOPT_INFILESIZE, filesize($this->basePath.$path));
		curl_exec ($ch);
		$error = curl_errno($ch);
		curl_close ($ch);
		if ($error != 0) $this->errors .= $path.PHP_EOL;

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.

	public function backupfiles($directory) {
		if ($handle = opendir($this->basePath.$directory)) {
			while (false !== ($file = readdir($handle))) {
				if (is_file($this->basePath.$directory.$file) && $file != '.htaccess') {
				} elseif ($file != '.' && $file != '..' && is_dir($this->basePath.$directory.$file)) {
					$this->backupfiles($directory.$file.'/', $directory);
			if ($this->errors != '') {
				$this->msg = 'Missing files: '.PHP_EOL.$this->errors;
			} else {
				$this->msg = 'FTP upload successful.';
		} else {
			$this->msg = 'Can\'t open local directory.';

	public function send_emailmsg() {
		if ($this->msg != '') {
			$body = PHP_EOL.'Backup result:'.PHP_EOL.PHP_EOL.$this->msg;
			mail($this->email, 'Your Backup on '.date('Y-m-d H:i:s'), $body);

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:

$cb = new Curl_ftp_backup('', 'user:password', '');

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”.

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.

Published in: PHP Scripts


  1. @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 ;)

  2. 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?

  3. 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.

  4. Hi Matt,

    creating (bigger) zips takes much more server resources…

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

  5. Gurpreet Singh,

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

  6. 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?

Comments are closed.