[php] WHMbackup.Solutions

Viewer

copydownloadembedprintName: WHMbackup.Solutions
  1. <?php
  2.  
  3. /**
  4.  * WHM Backup Solutions
  5.  * https://whmbackup.solutions
  6.  * 
  7.  * Description:     This script utilises cPanel's official API's to enable reseller
  8.  *                  users to automate backups of accounts within their reseller account,
  9.  *                  a feature currently missing.
  10.  * 
  11.  * Requirements:    cPanel Version 11.68+
  12.  *                  PHP Version 5.6+
  13.  *                  Curl
  14.  * 
  15.  * Instructions:    For instructions on how to configure and run this script see README.txt
  16.  *                  or visit https://whmbackup.solutions/documentation/
  17.  *  
  18.  * LICENSE: This source file is subject to GNU GPL-3.0-or-later
  19.  * that is available through the world-wide-web at the following URI:
  20.  * https://www.gnu.org/licenses/gpl.html.  If you did not receive a copy of
  21.  * the GNU GPL License and are unable to obtain it through the web, please
  22.  * send a note to [email protected] so we can mail you a copy immediately.
  23.  *
  24.  * @author      Peter Kelly <[email protected]>
  25.  * @license     https://www.gnu.org/licenses/gpl.html GNU GPL 3.0 or later
  26.  * @link        https://whmbackup.solutions
  27.  * @filename    whmbackup.php
  28.  */
  29.  
  30. @set_time_limit(180);
  31. @ini_set('max_execution_time', 180);
  32.  
  33. $directory = realpath(__dir__ ) . DIRECTORY_SEPARATOR;
  34.  
  35. // Include Functions file.
  36. include ($directory . "resources" . DIRECTORY_SEPARATOR . "functions.php");
  37. include ($directory . "resources" . DIRECTORY_SEPARATOR . "xmlapi" . DIRECTORY_SEPARATOR . "xmlapi.php");
  38.  
  39. // Variables
  40. $generate = null;
  41. $force = null;
  42. $config_name = null;
  43.  
  44. // Check if script run via command line or web browser.
  45. if ((PHP_SAPI == 'cli'))
  46. {
  47.         foreach ($argv as $arg)
  48.         {
  49.                 list($arg_x, $arg_y) = explode('=', $arg);
  50.                 $_GET[$arg_x] = $arg_y;
  51.         }
  52.  
  53. }
  54.  
  55. // Check if script variables are set in $_GET.
  56. $generate = array_key_exists("generate", $_GET);
  57. $force = array_key_exists("force", $_GET);
  58. if (array_key_exists("config", $_GET))
  59.         $config_name = $_GET["config"];
  60.  
  61. // Include Config File
  62. $include_config = include_config($config_name);
  63. if ($include_config["error"])
  64.         record_log("system", $include_config["response"], true);
  65. $config = $include_config["response"];
  66.  
  67. // Valid Config Variables
  68. $config_variables = array(
  69.         "date_format",
  70.         "timezone",
  71.         "obfuscate_config",
  72.         "check_version",
  73.         "whm_hostname",
  74.         "whm_port",
  75.         "whm_username",
  76.         "whm_auth",
  77.         "whm_auth_key",
  78.         "type_of_backup",
  79.         "backup_criteria",
  80.         "backup_exclusions",
  81.         "backup_destination",
  82.         "backup_hostname",
  83.         "backup_port",
  84.         "backup_user",
  85.         "backup_pass",
  86.         "backup_email",
  87.         "backup_rdir");
  88.  
  89. // Check Config For All Required Variables.
  90. foreach ($config_variables as $var)
  91. {
  92.         if (!isset($config[$var]))
  93.                 record_log("system", "Variable $config["" . $var .
  94.                         ""] Missing From Config. Please Generate A New Configuration File Using config.php.new", true);
  95. }
  96.  
  97. // Retrieve Backup Status
  98. $retrieve_status = retrieve_status($config_name);
  99. if ($retrieve_status["error"] == "1"){
  100.         record_log("system", $retrieve_status["response"]);
  101.     $email_log = email_log("ERROR - Reseller Backup Log (WHM Backup Solutions)", "The backup of \"" . $config['whm_username'] .
  102.                         "\" has an error that required attention. The log of backup initiation is available below.\r\n", TRUE);
  103.         if ($email_log["error"] == "0")
  104.         {
  105.                 record_log("note", "Retrieve Status Log File Successfully Sent To " . $config["backup_email"]);
  106.         } else
  107.         {
  108.                 record_log("note", "Retrieve Status " . $email_log["response"], true);
  109.         }
  110.     exit();
  111. }
  112.  
  113. $log_file = $retrieve_status["log_file"];
  114.  
  115. try
  116. {
  117.         $xmlapi = new xmlapi($config["whm_hostname"]);
  118.     $xmlapi->set_port($config["whm_port"]);
  119.         if ($config["whm_auth"] == "password")
  120.         {
  121.                 $xmlapi->password_auth($config["whm_username"], $config["whm_auth_key"]);
  122.         } else
  123.                 if ($config["whm_auth"] == "hash")
  124.                 {
  125.                         $xmlapi->hash_auth($config["whm_username"], $config["whm_auth_key"]);
  126.                 } else
  127.                 {
  128.                         record_log("system", "Invalid Authentication Type, Set $config[\"whm_auth\"] to either password or hash.");
  129.             $email_log = email_log("ERROR - Reseller Backup Log (WHM Backup Solutions)", "The backup of \"" . $config['whm_username'] .
  130.                         "\" has an error that required attention. The log of backup initiation is available below.\r\n", TRUE);
  131.             if ($email_log["error"] == "0")
  132.             {
  133.                 record_log("note", "Invalid Authentication Log File Successfully Sent To " . $config["backup_email"]);
  134.             } else
  135.             {
  136.                 record_log("note", "Invalid Authentication " . $email_log["response"], true);
  137.             }
  138.             exit();
  139.                 }
  140.  
  141.         $xmlapi->set_output('json');
  142.         $xmlapi->set_debug(0);
  143.  
  144. // Generate Variable Set, If Backup Already Started & Force Variable Set OR If Backup Not Already Started, Generate Account List
  145. if ((($generate == true) && ($retrieve_status["status"] == "1") && ($force == true)) || (($generate == true) &&
  146.         ($retrieve_status["status"] != "1")))
  147. {
  148.     
  149.         $update_status = update_status(array(), "", $config_name);
  150.  
  151.         // Generate Account List
  152.         $generate_account_list = generate_account_list();
  153.         $log_file = $generate_account_list["log_file"];
  154.     
  155.     $cpanel_version = json_decode($xmlapi->version(), true);
  156.     if(!isset($cpanel_version["version"])) $cpanel_version["version"] = "Error";
  157.  
  158.     if((isset($cpanel_version["status"])) && ($cpanel_version["status"] == "0")) $cpanel_version["version"] = $cpanel_version["statusmsg"];
  159.     record_log("note", $config['whm_username'] . "@" . $config['whm_hostname'] . ", cPanel Version: " . $cpanel_version["version"]);
  160.     
  161.         if ($generate_account_list["error"] == "1"){
  162.                 record_log("note", "(Generation) ERROR: " . $generate_account_list["response"]);
  163.         $email_log = email_log("ERROR - Reseller Backup Log (WHM Backup Solutions)", "The backup of \"" . $config['whm_username'] .
  164.                         "\" has an error that required attention. The log of backup initiation is available below.\r\n");
  165.                 if ($email_log["error"] == "0")
  166.                 {
  167.                         record_log("note", "Generation Log File Successfully Sent To " . $config["backup_email"]);
  168.                 } else
  169.                 {
  170.                         record_log("note", "Generation " . $email_log["response"], true);
  171.                 }
  172.         exit();
  173.     }
  174.  
  175.         // Check For New Version
  176.         if ($config["check_version"] != '0')
  177.         {
  178.                 $check_version = check_version();
  179.                 if ($check_version["error"] == "1")
  180.                 {
  181.                         record_log("note", "UPDATE CHECK ERROR: " . $check_version["response"]);
  182.                 } else
  183.                 {
  184.                         if (($config["check_version"] == $check_version["version_status"]) || (($config["check_version"] == "2") &&
  185.                                 ($check_version["version_status"] == "1")))
  186.                         {
  187.                                 record_log("note", "UPDATE CHECK: " . $check_version["response"]);
  188.                                 if ((!isset($config["skip_update"])) || ($config["skip_update"] != "1"))
  189.                                 {
  190.                                         $update_script = update_script($check_version["hash"]);
  191.                     if($update_script["error"] == "1"){
  192.                         record_log("note", "UPDATE SCRIPT ERROR: " . $update_script["response"]);
  193.                     }else{
  194.                         record_log("note", "UPDATE SCRIPT: " . $update_script["response"]);
  195.                     }
  196.                                 }
  197.                         }
  198.                 }
  199.         }
  200.  
  201.         $save_status = update_status($generate_account_list["account_list"], $generate_account_list["log_file"], $config_name);
  202.         if ($save_status["error"] == "1")
  203.                 record_log("note", "(Generation) ERROR: " . $save_status["response"], true);
  204.         record_log("note", "Accounts To Be Backed Up: " . implode(", ", $generate_account_list["account_list"]), false);
  205.         if (count($generate_account_list["account_excluded"]) > 0)
  206.                 record_log("note", "Accounts Excluded From Backup By Config: " . implode(", ", $generate_account_list["account_excluded"]), false);
  207.         if (count($generate_account_list["account_suspended"]) > 0)
  208.                 record_log("note", "Accounts Excluded From Backup Due To Being Suspended: " . implode(", ", $generate_account_list["account_suspended"]), false);
  209.         exit();
  210. }
  211.  
  212. if (($generate == true) && ($retrieve_status["status"] == "1"))
  213. {
  214.         echo "Backup Already Started. To Generate A New Backup Use Force Variable.";
  215. }
  216.  
  217. if (($generate == false) && ($retrieve_status["status"] == "0"))
  218. {
  219.         echo "No Backups Required.";
  220. }
  221.  
  222. // Generate Variable Not Set, Backup Already Started, Accounts Remaining To Backup.
  223. if (($generate == false) && ($retrieve_status["status"] == "1"))
  224. {
  225.         $backup_accounts = backup_accounts($retrieve_status["account_list"]);
  226.         $account = $retrieve_status["account_list"][0];
  227.         //$retrieve_status["account_list"] = array_values($retrieve_status["account_list"]);
  228.         unset($retrieve_status["account_list"][0]);
  229.         $save_status = update_status(array_values($retrieve_status["account_list"]), $retrieve_status["log_file"], $config_name);
  230.  
  231.         if (($backup_accounts["error"] == "0") && (!empty($config["backup_email"]))){
  232.         if(isset($backup_accounts["pid"])){
  233.             record_log("note", "(" . $account .
  234.                         ") Backup Initiated (PID: " . $backup_accounts["pid"] . "). For More Details See The Backup Email For This Account.", true);
  235.         }else{
  236.             record_log("note", "(" . $account .
  237.                         ") Backup Initiated. For More Details See The Backup Email For This Account.", true);
  238.         }
  239.     }
  240.  
  241.         if (($backup_accounts["error"] == "0") && (empty($config["backup_email"]))){
  242.         if(isset($backup_accounts["pid"])){
  243.             record_log("note", "(" . $account . ") Backup Initiated (PID: " . $backup_accounts["pid"] . ").", true);
  244.         }else{
  245.             record_log("note", "(" . $account . ") Backup Initiated.", true);
  246.         }
  247.     }
  248.  
  249.         record_log("note", "(" . $account . ") ERROR: " . $backup_accounts["response"], true);
  250. }
  251.  
  252. // Generate Variable Not Set, Backup Already Started, All Accounts Backed Up, Send Log File in Email.
  253. if (($generate == false) && ($retrieve_status["status"] == "2"))
  254. {
  255.         if (!empty($config['backup_email']))
  256.         {
  257.                 $email_log = email_log("Reseller Backup Log (WHM Backup Solutions)", "The backup of \"" . $config['whm_username'] .
  258.                         "\" has been completed. The log of backup initiation is available below.\r\n");
  259.                 if ($email_log["error"] == "0")
  260.                 {
  261.                         record_log("note", "Backup Completion Log File Successfully Sent To " . $config["backup_email"]);
  262.                 } else
  263.                 {
  264.                         record_log("note", "Backup Completion " . $email_log["response"], true);
  265.                 }
  266.         } else
  267.         {
  268.                 record_log("note", "Log File Completed.");
  269.         }
  270.         $update_status = update_status(array(), "", $config_name);
  271. }
  272.  
  273. }
  274. catch (exception $e)
  275. {
  276.         record_log("system", "cPanel API Error: " . $e->getMessage());
  277.     $email_log = email_log("ERROR - Reseller Backup Log (WHM Backup Solutions)", "The backup of \"" . $config['whm_username'] .
  278.                         "\" has an error that required attention. The log of backup initiation is available below.\r\n", TRUE);
  279.         if ($email_log["error"] == "0")
  280.         {
  281.                 record_log("note", "cPanel API Error Log File Successfully Sent To " . $config["backup_email"]);
  282.         } else
  283.         {
  284.                 record_log("note", "cPanel API Error " . $email_log["response"], true);
  285.         }
  286.     exit();
  287. }
  288.  
  289. ?>

Editor

You can edit this paste and save as new:


File Description
  • WHMbackup.Solutions
  • Paste Code
  • 27 Sep-2021
  • 10.83 Kb
You can Share it: