forked from Alanaktion/MCHostPanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-run.php
124 lines (97 loc) · 3.4 KB
/
backup-run.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
<?php
require_once dirname(__FILE__) . '/data/config.php';
//CLI overwrites
if (PHP_SAPI !== 'cli') {
error_log("MCHostPanel Backup: Attempted to run backup-run.php over HTTP!");
exit("Invalid access type\r\n");
}
if(!isset($argv[2])) {
error_log("MCHostPanel Backup: No secret supplied!");
exit("No user supplied!\r\n");
}
if(!isset($argv[1])) {
error_log("MCHostPanel Backup: No user supplied!");
exit("No user supplied!\r\n");
}
if(!isset($argv[3])) {
error_log("MCHostPanel Backup: No backup auto-delete supplied!");
exit("No auto-delete supplied!\r\n");
}
$name = $argv[1];
$secret = $argv[2];
$delete = $argv[3];
if(is_file(dirname(__FILE__) . '/data/users/' . strtolower(preg_replace('/([^A-Za-z0-9\- ])/','',$name)) . '.json')) {
$user = json_decode(file_get_contents(dirname(__FILE__) . '/data/users/' . strtolower(preg_replace('/([^A-Za-z0-9\- ])/','',$name) . '.json')), true);
} else {
$user = false;
}
if (!$user) {
// Clean out the supplied user just incase
$user = preg_replace('/[^A-Za-z0-9\- ]/', '', $name);
// User does not exist, redirect to login page
error_log("MCHostPanel Backup: '" . $user . "' user does not exist!");
exit('Not Authorized\r\n');
}
//Make sure this page is run via cron and not from URL guessing
if($secret != hash("sha256", $user['pass'])) {
error_log("MCHostPanel Backup: Invalid secret!");
exit('Not Authorized\r\n');
}
$running = !!strpos(`screen -ls`, KT_SCREEN_NAME_PREFIX . $name);
if(!$running) {
exit('Server not running\r\n');
}
server_cmd($user['user'], "/save-all");
//Give the server a chance to save
sleep(30);
//Prevent auto-saves while we run the backup
server_cmd($user['user'], "/save-off");
//Notify players the world is backing up
server_cmd($user['user'], "/say [MCBackup] Starting backup...");
if(!is_dir($user['home'] . "/" . "backups")){
mkdir($user['home'] . "/" . "backups");
}
$timeout = intval($delete) * 60 * 60; //Convert to seconds
//Delete old backups based on their delete interval
if($timeout !== 0) {
$backups = array_diff(scandir($user['home'] . "/" . "backups/"), array('.', '..'));
foreach($backups as $backup) {
$timeCreated = filectime($user['home'] . "/" . "backups/" . $backup);
//Times up!
if($timeout + $timeCreated <= time()) {
unlink($user['home'] . "/" . "backups/" . $backup);
}
}
}
try {
$archiveFile = date('Y-m-d') . " - " . time() . " - Minecraft Backup.tar";
$phar = new PharData($user['home'] . "/" . "backups/" . $archiveFile);
$phar->buildFromDirectory($user['home'], '/^((?!backups).)*$/');
$phar->compress(Phar::GZ);
//Delete the .tar file since now we have a .tar.gz
unlink($user['home'] . "/" . "backups/" . $archiveFile);
} catch (Exception $e) {
error_log("MCHostPanel Backup: '" . $user . "' Backup Failure!\r\nException : " . $e);
exit("Exception : " . $e . "\r\n");
}
//Notify players the backup is done
server_cmd($user['user'], "/say [MCBackup] Backup complete");
//Turn auto-saves back on
server_cmd($user['user'], "/save-on");
echo "MCHostPanel Backup Success\r\n";
/**
* Pass a command to a running server
* This is a local copy to prevent importing dependencies
* @param string $name
* @param string $cmd
*/
function server_cmd($name,$cmd) {
shell_exec(
sprintf(
KT_SCREEN_CMD_EXEC, // Base command
KT_SCREEN_NAME_PREFIX.$name, // Screen Name
str_replace(array('\\','"'),array('\\\\','\\"'),(get_magic_quotes_gpc() ? stripslashes($cmd) : $cmd)) // Server command
)
);
}
?>