Automatic full backup on cPanel


logo_cpanelYesterday I had some issues with the WordPress 2.8 update and, supposedly,  qtranslate (a plugin to support several languages). The thing is I could restore WordPress 2.7.1 from an old backup, but I realized it dated from a month ago…bad, bad. I decided to do a research on automatic full-backup with cpanel and I came across the Automatic cPanel backup (domain & MySQL) with cron & PHP post by Justin Cook .There’s a php script to perform the cPanel‘s full backup request with ftp redirection to a remote host. Due to I don’t have a ftp server, I changed some lines to make backup locally, so I could get those backups from cPanel full-backup tool. Here’s what I got:

PHP Script

<?php
// Info required for cPanel access
$cpuser = "username";
$cppass = "password";
$domain = "example.com";
$skin = "x3";

// Notification information
$notifyemail = "[email protected]";

// Secure or non-secure mode
$secure = 1;

// Set to 1 to have web page result appear in your cron log
$debug = 0;

// ** NO EDIT BELOW THIS LINE ***

if ($secure) {
   $url = "ssl://".$domain;
   $port = 2083;
} else {
   $url = $domain;
   $port = 2082;
}

$socket = fsockopen($url,$port);

if (!$socket) {
   echo "Failed to open socket connection… Bailing out!\n"; exit;
}

// Encode authentication string
$authstr = $cpuser.":".$cppass;
$pass = base64_encode($authstr);

$params = \
"dest=homedir&email=$notifyemail&server=&user=&pass=&port=&rdir=";

// Make POST to cPanel
fputs($socket,\
"POST /frontend/".$skin."/backup/dofullbackup.html?".$params." HTTP/1.0\r\n");
fputs($socket,"Host: $domain\r\n");
fputs($socket,"Authorization: Basic $pass\r\n");
fputs($socket,"Connection: Close\r\n");
fputs($socket,"\r\n");

// Grab response even if we don't do anything with it.
while (!feof($socket)) {

$response = fgets($socket,4096);

if ($debug) echo $response;
}
fclose($socket);
?>

You can download the example here

Cron jobs

In order to have the backup made every 15 days just save the above script as fullbackup.php and set cron in cPanel‘s tool as shown next:

cron-cpanel-web

References

,

  1. #1 by Luis Gallardo on 29/11/2010 - 12:47 pm

    @Jair capaz es por copiar y pegar directamente de la página. Para descartar esto subí el ejemplo en este enlace http://lgallardo.com/wp-content/uploads/2009/06/fullbackup.zip

    Cualquier cosa avisas :)

  2. #2 by Jair Gutierrez on 29/11/2010 - 11:08 am

    Hola gracias por el script lo puse en el sitio pero recibo este error

    Warning: Unexpected character in input: ‘\’ (ASCII=92) state=1 in /home/cxayucej/public_html/fullbackup.php on line 37

    Warning: Unexpected character in input: ‘\’ (ASCII=92) state=1 in /home/cxayucej/public_html/fullbackup.php on line 41

  3. #3 by Luis Gallardo on 01/10/2010 - 6:17 am

    @James Yes, it is ;)

  4. #4 by James Garcia on 01/10/2010 - 5:50 am

    Cpanel is reall great for managing websites and server reources:`.

  5. #5 by Luis Gallardo on 16/08/2010 - 2:04 pm

    @patricio el error está claro: no existe esa ruta de directorio. Revisa cuál es la ruta donde colocas el script php. Si no sabes (o estás limitado por cPanel) quizás puedas intentar capturar el directorio de tus scripts con la función getcwd http://php.net/manual/en/function.getcwd.php

  6. #6 by patricio on 16/08/2010 - 11:49 am

    Luis, hice una prueba con este script, pero me envia un error por mail y no realiza :

    /bin/sh: /usr/local/bin/php/home/canalud/fullbackup.php: Not a directory

    me podrías ayudar?

  7. #7 by Luis Gallardo on 12/08/2010 - 11:49 pm

    @William Nice advice! cPanel helps out a lot, specially if you don’t know how to admin a server (VPS)

  8. #8 by William Roberts on 12/08/2010 - 12:59 pm

    if you are going to get a VPS server make sure that it has cPanel coz it makes server maintennance easier.~-`

  9. #9 by Luis Gallardo on 15/07/2010 - 9:28 am

    @Patricio Debes colocar los datos de tu cuenta en el script:

    // Info required for cPanel access
    $cpuser = “username”;
    $cppass = “password”;
    $domain = “example.com”;
    $skin = “x3″;

    De resto no debes editar nada. Luego guardas el script en tu directorio de trabajo, no en el public_html porque todo el mundo podría ver tu usuario y clave. Por último configuras el cron en cPanel como se muestra en la imágen del artículo, pero colocando la ruta de tu script.

  10. #10 by Patricio on 15/07/2010 - 9:19 am

    Muy interesante tu artículo y es lo que andaba buscando, pero como soy ignorante de estos temas, que partes del script debo reemplazar para poder ocuparlo en mi sitio? el archivo php debo guardarlo en public_html?
    Agradecido de antemano

  11. #11 by Luis Gallardo on 04/05/2010 - 11:59 pm

    @Colton Diaz You’re right. In my case I don’t have a public ftp server or another CPANEL installed, meaning, I back it up automatically, and download it manually :P

  12. #12 by Colton Diaz on 04/05/2010 - 11:42 pm

    It is also easy to backup and transfer all your websites from one server to another server if you have cPanel installed*”*

(will not be published)