-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsync.php
269 lines (222 loc) · 6.97 KB
/
rsync.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
<?php
/*
For sample usage and documentation, view readme at http://github.com/davidsalazar/rsync
TODO:
Add environment test method to test if directories are writable, ssh and mysql users are valid.
Add intelligent logging.
*/
class Rsync
{
/**
* Will check for existence of this file for a list of files and dirs to exclude.
* Directories and files are relative to the source path.
* If not prefixed with /. it will match every directory.
* For more info, read http://articles.slicehost.com/2007/10/10/rsync-exclude-files-and-folders
*
*
* @var string
*/
public $exclude_file = 'exclude.txt';
/**
* Mysql backup directory relative to the source path.
*
* @var string
*/
public $mysql_dir = '_sql';
public $mysql_backup_interval = 'daily';
public $mysql_gzip = FALSE;
// The following intervals must have a minimum of 1 backup.
public $hourly = 2;
public $daily = 2;
public $weekly = 2;
public $monthly = 2;
public $rsync_bin = 'rsync';
public $rsync_switches = '-avz --delete';
private $source;
private $dest;
private $cp_switches;
private $cmd;
private $output;
private $backups = array('hourly' => '-1 hour', 'daily' => '-1 day', 'weekly' => '-7 day', 'monthly' => '-30 day');
private $mysql_backups = array();
private $time_limit = 30;
/**
* Instantiate an object by setting the source and destination path.
*
* @param string $source Ssh url or local absolute path
* @param string $dest Local absolute path
*/
public function __construct($source, $dest)
{
// OSX is gay and doesn't support hard links via copy using the cp command
$this->cp_switches = PHP_OS == 'Darwin' ? '-pPR' : '-al';
$this->source = rtrim($source, '/');
$this->dest = rtrim($dest, '/');
is_writable($this->dest) or die("Destination directory doesn't exist or isn't writable: $this->dest");
}
/**
* Any public properties can be configured after object is instantiated.
*
* @param array $options Associate array with index being property name and value being property value.
* @return void
*/
public function set_options($options = array())
{
$public_properties = $this->get_public_properties();
foreach ($options as $k => $v)
{
if (in_array($k, $public_properties))
{
$this->$k = $v;
}
}
}
/**
* Begin backup procedure.
*
* @return string $results Results from backup script are returned as html.
*/
public function init()
{
set_time_limit(60 * $this->time_limit);
$this->prep();
$this->cmd .= "$this->source/ $this->dest/current";
$rsync_output = $this->exec($this->cmd);
if (strpos($rsync_output, 'total size is') === FALSE || strpos($rsync_output, 'rsync error') !== FALSE)
die("<h3>Backup Failed</h3><p>" . nl2br($this->output) . "</p>");
if ($this->is_empty_dir("$this->dest/current"))
die("<h3Backup failed</h3><p><i>$this->dest/current</i> is an empty dir.</p><h3>Output</h3><p>" . nl2br($this->output) . "</p>");
$this->process();
return nl2br($this->output);
}
/**
* Add a mysql backup.
*
* @param string $dbhost
* @param string $dbuser
* @param string $dbpass
* @param array $dbnames (optional, if omitted all databases will be backed up)
* @return void
*/
public function add_mysql($dbhost, $dbuser, $dbpass, $dbnames = array())
{
$this->mysql_backups[] = array('dbhost' => $dbhost, 'dbuser' => $dbuser, 'dbpass' => $dbpass, 'dbnames' => $dbnames);
}
private function prep()
{
$this->cmd = "$this->rsync_bin $this->rsync_switches ";
$this->mysql_dir = rtrim($this->mysql_dir, '/');
$base_path = dirname(__FILE__) . '/';
if (file_exists($exclude_file = $base_path . $this->exclude_file))
{
$this->cmd .= "--exclude-from '$exclude_file' ";
}
// If source does not start with a /, we assume it's an ssh url
if (strpos($this->source, '/') !== 0)
{
$this->cmd .= '-e ssh ';
}
}
private function process()
{
$prev_interval = NULL;
foreach ($this->backups as $interval => $expiration)
{
$prev_interval_count = $prev_interval ? ($this->$prev_interval - 1) : NULL;
// If the last increment isn't complete, we will wait until it is.
if ($interval != 'hourly' && !is_dir("$this->dest/$prev_interval.$prev_interval_count"))
{
continue;
}
$interval_count = $this->$interval - 1;
if ($interval == 'hourly' || !is_dir("$this->dest/$interval.0") || ($this->filemtime("$this->dest/$interval.0") < strtotime($expiration)))
{
$this->exec("rm -rf $this->dest/$interval.$interval_count");
for ($i = $interval_count - 1; $i >= 0; $i--)
{
$increment = $i + 1;
if (is_dir("$this->dest/$interval.$i"))
{
$this->exec("mv $this->dest/$interval.$i $this->dest/$interval.$increment");
}
}
if ($interval == 'hourly')
{
$this->exec("cp $this->cp_switches $this->dest/current $this->dest/$interval.0");
}
else
{
$this->exec("cp $this->cp_switches $this->dest/$prev_interval.$prev_interval_count $this->dest/$interval.0");
touch("$this->dest/$interval.0");
}
if (count($this->mysql_backups) && $this->mysql_backup_interval == $interval)
{
$this->process_mysql($interval);
}
}
$prev_interval = $interval;
}
}
private function process_mysql($interval)
{
@mkdir("$this->dest/$interval.0/$this->mysql_dir");
if (is_writable("$this->dest/$interval.0/$this->mysql_dir") && count($this->mysql_backups))
{
file_put_contents("$this->dest/$interval.0/$this->mysql_dir/.htaccess", 'deny from all');
foreach ($this->mysql_backups as $mysql_backup)
{
if (count($mysql_backup['dbnames']))
{
$dbnames = $mysql_backup['dbnames'];
}
else
{
$dbnames = $this->exec("mysql -h{$mysql_backup['dbhost']} -u{$mysql_backup['dbuser']} -p{$mysql_backup['dbpass']} -e 'show databases' ");
$dbnames = @explode("\n", trim($dbnames));
array_shift($dbnames);
}
foreach ($dbnames as $dbname)
{
$mysqldump = "mysqldump --opt -h{$mysql_backup['dbhost']} -u{$mysql_backup['dbuser']} -p{$mysql_backup['dbpass']} $dbname ";
if ($this->mysql_gzip)
{
$mysqldump .= "| gzip > $this->dest/$interval.0/$this->mysql_dir/$dbname.sql.gz";
}
else
{
$mysqldump .= "> $this->dest/$interval.0/$this->mysql_dir/$dbname.sql";
}
$this->exec($mysqldump);
}
}
}
}
private function is_empty_dir($dir)
{
return count(@scandir($dir)) <= 2;
}
private function exec($cmd)
{
$this->output .= '<h4>' . $cmd . '</h4>';
$this->output .= '<p>' . ($output = shell_exec("$cmd 2>&1")) . '</p>';
return $output;
}
private function filemtime($file)
{
return trim(shell_exec(PHP_OS == 'Darwin' ? "stat -f %m $file" : "stat -c %Y $file"));
}
private function get_public_properties()
{
$this_object = new ReflectionObject($this);
$properties = $this_object->getProperties();
$public_properties = array();
foreach ($properties as $property)
{
if ($property->isPublic())
{
$public_properties[] = $property->getName();
}
}
return $public_properties;
}
}