CPanel full backup (all files+databases+emails) PHP script

 

I was looking for a working script to take full backup (all files+databases+emails) manually or using cron services on my hosting server, each CPanel user by one. But most of the scripts are either old, totally unusable or commercial.
So I wrote one for my own use and sharing here so others don’t need to re-invent the wheel

password_auth($cpanel_account,$cpanel_password);
$xmlapi->set_port('2083');
 
// Delete any other backup with filetime greater than expire time, before create new backup
$conn_id = ftp_connect($ftphost);
$login_result = ftp_login($conn_id, $ftpacct, $ftppass);

ftp_chdir($conn_id, $logs_dir);
$files = ftp_nlist($conn_id, ".");
foreach ($files as $filename) {
        $fileCreationTime = ftp_mdtm($conn_id, $filename);
        //$date = date("F j, Y, g:i a", ftp_mdtm($conn_id, $filename));
        //print "
Timestamp of '$filename': $date"; $fileAge=time(); $fileAge=$fileAge-$fileCreationTime; if ($fileAge > $backupexpireindays) { // Is the file older than the given time span? //echo "
The file $filename is older than Expire time :$expiretime ...Deleting\n"; ftp_delete($conn_id, $filename); //echo "
Deleted

"; } } ftp_close($conn_id); $api_args = array( 'passiveftp', $ftphost, $ftpacct, $ftppass, $email_notify, 21, '/' ); $xmlapi->set_output('json'); print $xmlapi->api1_query($cpanel_account,'Fileman','fullbackup',$api_args); ?>

You need to save it with .php extension (upload it to your server) and download include file from xmlapi.zip(right click->save as) and extract it to the same folder (on your web server). Create cron job from your CPanel or trigger it manually to get full backup in your FTP server, That’s it.
OR
You can fork from my git hub Repositories at cpanel-Fullbackup
Enjoy


  1. Josen says:

    This is a great script and I already have it working in production. Thank you!

    One question, however, can you show me how to preserve x number of days before deleting the existing backups?

    By example, I am wanting to keep 21 days of backups on my backup server. Would you be willing to modify this script for me to accomplish that?

    • Babar Shafiq says:

      My pleasure, glad if it is useful for others, you can write little logic and name your backups 1,2,3 like backup1, backup2, backup3 etc, and delete the last one and rename 2nd to 3rd, 1st to 2nd and store new, it will be too simple to add that code !

  2. Josen says:

    I took a stab at this myself. Not sure if it works yet…

    // Delete older backups
    $conn_id = ftp_connect($ftphost);
    $login_result = ftp_login($conn_id, $ftpacct, $ftppass);
    $logs_dir = “/”;
    $expiretime=30240; //expire time in minutes, 21 days = 7*24*60

    ftp_chdir($conn_id, $logs_dir);
    $files = ftp_nlist($conn_id, “.”);
    foreach ($files as $filename) {
    $fileCreationTime = filectime($filename); // Read file creation time
    $fileAge = time() – $fileCreationTime; // Calculate file age in seconds
    if ($fileAge > ($expiretime * 60)) { // Is the file older than the given time span?
    // Now do something with the olders files…
    //echo “The file $filename is older than $expiretime minutes\n”;
    //unlink($filename); //delete files
    ftp_delete($conn_id, $filename);
    }
    }

    ftp_close($conn_id);

    • Babar Shafiq says:

      Your code looks good, good share, I will check tomorrow and give you my feedback.

      Also I think I need to add an option to keep x number of last backups in my script !

  3. Josen says:

    Sounds great. Let me know when you have it worked out.

    All the best…

    Josen

  4. Josen says:

    Hi Babar…

    Have you had a change to look at this yet?

    Josen

  5. Babar Shafiq says:

    If you are copying from this post, than edit line 19 (remove –), syntax plugin is not allowing me to use single – and adding two more —> on line 19, better get it from github

  6. Josen says:

    Thank you so much for your help. This is great!

  7. Babar Shafiq says:

    Most welcome, after little formatting, edit line 25
    From:
    $xmlapi—>password_auth($cpanel_account,$cpanel_password);
    To:
    $xmlapi->password_auth($cpanel_account,$cpanel_password);

  8. Josen says:

    Yeah… I figured that part out. Thanks again. This is very helpful! =D

  9. CACA-COBRA says:

    Is it possible, create a folder for Back-ups?
    I adapted this script to connect and back-up an array list of servers, doing a foreach e creating the copies.
    But, i am doing a daily backup, and the files get messy in the folder.
    So, i would like to separate, create a folder by day (date(d-m-y)), to keep the files into separate folders.

  10. Babar Shafiq says:

    Yes you can manage folders and array of servers no issues, but share your current script and I will update or suggest you new code for doing the same.

    • CACA-COBRA says:

      Hy, i kinda manage to to wha i wanted, but i am sending my script, to you analize. Thanks πŸ˜‰

      • Babar Shafiq says:

        Your modifications looks good, but it will break default behavior of the script (keeping last 3 backups).
        I will update this script in accordance with the current one and will share, shortly.

        (also excuse me, your link removed for security purpose)

  11. Babar Shafiq says:

    OK script updated to reflect multi Cpanel server support.
    Please have a look at : https://github.com/babarnazmi/CPanel-Servers-Fullbackup/archive/master.zip

    It will loop Array of CPanel servers from config.php and use values from config.php for CPanel and FTP. Also it will create separate folders for each server and will check expiry time (default 21 days) for deletion of old backups (automatically).

    ***Not sure it is 100% working, needs some testing, so try it on test system not on production.*****

    If you think it working fine for you and doing beta testing, please let me know, so whoever is using this script can use this script and will be thankful to you too πŸ™‚

    By the way thanks for this.

  12. Chris says:

    Do you have a similar script available that will just do the FTP cleanup portion? Connect to FTP server, list the files in a folder and delete files greater than X days old. I see where the PHP code does the delete but i’m not familiar enough with PHP to figure out if there are any functions in the include api php file that would be required to just preform the delete / ftp clean up.

    Thanks!

  13. mujeres solteras says:

    Great article, exactly what I wanted to find.

  14. Iyaby says:

    An impressive share! I have just forwarded this onto a colleague who has been doing
    a little research on this. And he in fact ordered
    me breakfast because I found it for him…

    lol. So let me reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending the time to discuss this issue here on your web page.

  15. Jorge Luis says:

    Thank you, 2021 and still working. All the other scripts does not use the api

Leave a Reply