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