forked from Cranke/Plesk-Backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.php
executable file
·147 lines (117 loc) · 3.45 KB
/
backup.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
#!/usr/bin/php
<?php
//
// FTP Backup Script for Plesk
// v1.1 - 04/22/2015
// ======================================
// Extended by Michael Papesch - https://github.com/Cranke/Plesk-Backup
// Original by Jay Versluis - http://wpguru.co.uk
// find the latest version here: https://github.com/versluis/Plesk-Backup
// ======================================
// License: GNU General Public License v2 or later
// License URI: http://www.gnu.org/licenses/gpl-2.0.html
echo "\nFTP Backup for Plesk v1.1\n---------------------------------\n\n";
// exit "Terminating\n\n";
// add your FTP credentials here
$ftp_server = "";
$ftp_user = "";
$ftp_password = "";
// exclude Domains Domain1,Domain2
# $excludedomains = "somedomain.com,anotherdomain.com";
$excludedomains = "";
// Remote directory
# $remote_dir = "/path/to/userdir";
$remote_dir = "";
// maximum number of backup files to keep on FTP
$maxbackups = 7;
// add a prefix for your backup
$prefix = 'backup_';
// PLESK utilities directory
$pleskbin = "/usr/local/psa/bin/";
//------------------------------------------
// NO NEED TO EDIT ANYTHING BEYOND THIS LINE
// -----------------------------------------
// do a backup
createbackup();
// create FTP connection
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
ftp_chdir($conn_id, $remote_dir);
$contents = ftp_nlist($conn_id, ".");
// sort the array
rsort($contents);
// show all entries
directory($contents);
// delete first item
echo "\nCleaning up directory, leaving only $maxbackups...\n";
cleanupftp();
// close connection - we're done here!
ftp_close($conn_id);
// --------------------------------------
//
// FUNCTIONS
//
// create backup to FTP repo
function createbackup() {
global $ftp_server, $ftp_user, $ftp_password, $pleskbin, $excludedomains, $remote_dir;
$filename = createfilename() . ".tar";
$command = $pleskbin . "pleskbackup server --exclude-domain=$excludedomains --output-file=";
$command = $command . "ftp://$ftp_user:$ftp_password@$ftp_server";
$command = $command . "$remote_dir/$filename";
/*
if(exec($command)) {
echo "Backup was created successfully.\n";
} else {
echo "Houston, we had a problem with the backup command:\n $command \n";
}
*/
echo "Creating Backup with filename: $filename \n\n";
exec($command);
}
// delete a single backup on FTP
function deletebackup ($file) {
global $conn_id;
echo "Deleting ".$file."...\n";
if (ftp_delete($conn_id, $file)) {
echo "success!\n";
} else {
echo "That didn't work... dang!\n";
}
return;
}
// delete all but the latest $maxbackups
function cleanupftp() {
global $maxbackups, $conn_id;
$contents = ftp_nlist($conn_id, ".");
rsort($contents);
$currentbackups = count($contents);
if ($currentbackups > $maxbackups) {
// delete overspill items
for ($i = $maxbackups; $i < $currentbackups; $i++) {
deletebackup($contents[$i]);
}
}
// list directory
$contents = ftp_nlist($conn_id, ".");
directory($contents);
}
// show current dirtectory
function directory($contents) {
echo "Here comes the directory listing: \n";
foreach($contents as $entry) {
echo $entry . "\n";
}
return;
}
// create a filename for the backup
function createfilename() {
global $prefix;
date_default_timezone_set('UTC');
return $prefix . date('ymdHi');
}
// clear out PMM sessions (currently not in use)
function cleanpmm () {
echo "Cleaning out old PMM sessions...\n\n";
exec("rm -rf /usr/local/psa/PMM/sessions/*");
}
?>