forked from ScottMansfield/S3-Website-Backup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3backup.php
More file actions
259 lines (201 loc) · 7.35 KB
/
Copy paths3backup.php
File metadata and controls
259 lines (201 loc) · 7.35 KB
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
#!/usr/bin/php5
<?php
// the above should be changed to the path of the php 5.3
// executable on your system
error_reporting(E_ALL);
################################################################################
### Config Section
################################################################################
$config = array(
'user' => 'user',
'path_to_sites' => '/path/to/sites',
'local_backup_days' => 5,
'home_dir' => '/path/to/home/directory',
's3_key' => 'OMGTHISISMYKEY',
's3_secret' => 'PLEASEDONTSHARETHISSECRETKEYWITHANYONE',
'bucket' => 'backups',
'chunk_size_in_MB' => 10,
'remote_backup_days' => 30
);
$sites = array(
'example.com' => array(
'has_db' => false),
'blog.example.com' => array(
'has_db' => true,
'db_host' => 'mysql.example.com',
'db_name' => 'my_blog_db',
'db_user' => 'bloguser',
'db_pass' => 'correct horse battery staple')
);
################################################################################
### Backup Section
################################################################################
$now = time();
$date = date("Y-m-d-H:i:s", $now);
// Takes script location and creates temp directory for backups
$backup_dir = __DIR__ . '/backup_' . $date . '/';
ensure_dir_exists($backup_dir);
// Backup all mysql databases
$mysql_backup_dir = $backup_dir . '/mysql/';
ensure_dir_exists($mysql_backup_dir);
foreach($sites as $site_name => $details)
{
if ($details['has_db'])
{
echo 'Dumping ' . $details['db_name'] . PHP_EOL;
// backup_dir/mysql/db_backup_dbname_0000_00_00_00:00:00.bak.gz
$path = $mysql_backup_dir . 'db_backup_' .
escapeshellarg($details['db_name']) . '_' . $date . '.bak.gz';
// mysqldump -h host.example.com -u user -ppassword dbname | gzip > path
$command = 'mysqldump -h ' . escapeshellarg($details['db_host']) .
' -u ' . escapeshellarg($details['db_user']) . ' -p' .
escapeshellarg($details['db_pass']) . ' ' .
escapeshellarg($details['db_name']). ' | gzip > ' . $path;
exec($command);
}
}
echo 'MySQL dumps complete.' . PHP_EOL . PHP_EOL;
// Backup all website data
$site_backup_dir = $backup_dir . '/sites/';
ensure_dir_exists($site_backup_dir);
foreach($sites as $site_name => $details)
{
echo 'Archiving ' . $site_name . PHP_EOL;
// this script is meant to reside in a backup dir beside website dirs
$site_dir = $config['path_to_sites'] . '/' . $site_name;
$path = $site_backup_dir . 'site_backup_' . $site_name . '_' . $date .
'.bak.tar.gz';
$command = 'tar czf ' . escapeshellarg($path) . ' -C ' .
escapeshellarg($site_dir) . ' ' . escapeshellarg($site_dir);
exec($command);
}
echo 'Site archiving complete.' . PHP_EOL . PHP_EOL;
// Bundle into one big archive
echo 'Bundling into one big file' . PHP_EOL;
$file_name = __DIR__ . '/backup_' . $config['user'] . '_' . $date . '.bak.tar.gz';
// --force-local is needed because of the colons in the file name
$command = 'tar czf ' . escapeshellarg($file_name) . ' ' .
escapeshellarg($backup_dir) . ' --force-local';
exec($command);
echo 'Bundling complete.' . PHP_EOL . PHP_EOL;
// Delete all temp files
// rmdir() function requires the file to be empty, it's easier to rely on
// the rm -R shell command to recursively delete the files
echo 'Cleaning up temporary files.' . PHP_EOL;
exec('rm -R ' . $backup_dir);
echo 'Temp files deleted.' . PHP_EOL . PHP_EOL;
// Delete local archive files older than configured number of days
$dir_handle = opendir(__DIR__);
$days = intval($config['local_backup_days']);
echo "Removing backups older than $days days from local system." . PHP_EOL;
$file_cutoff_time = $now - ($days*24*60*60);
// if it isnt a implied directory and is an old backup, delete the file
while (($file = readdir($dir_handle)) !== false)
{
if ($file != '.' &&
$file != '..' &&
strpos($file, 'backup_' . $config['user']) === 0 &&
$file_cutoff_time > filemtime(__DIR__ . '/' . $file))
{
echo 'Deleting ' . $file . PHP_EOL;
unlink(__DIR__ . '/' . $file);
}
}
echo "Backups older than $days days removed from local system." .
PHP_EOL . PHP_EOL;
echo 'Cleanup complete.' . PHP_EOL . PHP_EOL;
################################################################################
### S3 section
################################################################################
putenv('HOME=' . $config['home_dir']);
require_once 'sdk-1.5.3/sdk.class.php';
define('MB', 1024 * 1024);
$s3 = new AmazonS3(array(
'key' => $config['s3_key'],
'secret' => $config['s3_secret']
));
$bucket = $config['bucket'];
$file = fopen($file_name, 'r');
if (!$file) die("Could not open $file_name for upload." . PHP_EOL);
$upload_file_name = basename($file_name);
echo 'Creating object.' . PHP_EOL . PHP_EOL;
$response = $s3->initiate_multipart_upload($bucket, $upload_file_name);
$upload_id = (string)$response->body->UploadId;
$chunk_size = intval($config['chunk_size_in_MB']);
echo "Uploading parts in $chunk_size MB chunks." . PHP_EOL . PHP_EOL;
$part_counts = $s3->get_multipart_counts(filesize($file_name), $chunk_size*MB);
$total_parts = count($part_counts);
$current_part = 1;
foreach($part_counts as $i => $part)
{
$response = $s3->upload_part($bucket, $upload_file_name, $upload_id,
array(
'expect' => '100-continue',
'fileUpload' => $file,
'partNumber' => ($i + 1),
'seekTo' => (integer)$part['seekTo'],
'length' => (integer)$part['length']
));
if ($response->isOK())
{
echo "Part number $current_part of $total_parts uploaded." . PHP_EOL;
$current_part++;
}
else
{
echo "Upload of file $upload_file_name failed, aborting upload." .
PHP_EOL;
$s3->abort_multipart_upload($bucket, $upload_file_name, $upload_id);
die();
}
}
echo PHP_EOL;
$parts = $s3->list_parts($bucket, $upload_file_name, $upload_id);
$response = $s3->complete_multipart_upload($bucket, $upload_file_name, $upload_id,
$parts);
// release resources
fclose($file);
if ($response->isOK())
{
echo "File $upload_file_name successfully uploaded to $bucket ." . PHP_EOL . PHP_EOL;
}
else
{
echo "Upload of file $upload_file_name to $bucket failed." . PHP_EOL . PHP_EOL;
die();
}
echo "Finished uploading." . PHP_EOL . PHP_EOL;
// delete remote backups older than the specified number of days
$days = intval($config['remote_backup_days']);
echo "Removing backups older than $days days from S3." . PHP_EOL;
$file_cutoff_time = $now - ($days*24*60*60);
$interval = new DateInterval('P' . $days . 'D');
// grab only the backup files from the bucket
// will return 1000 results (if there are that many)
$response = $s3->list_objects($bucket, array('prefix' => 'backup_' . $config['user']));
// essentially I am using the DateTime class to parse the string I get back
foreach($response->body->Contents as $file)
{
$time = new DateTime($file->LastModified->to_string());
$stamp = $time->getTimestamp();
if ($stamp < $file_cutoff_time)
{
echo 'Deleting ' . $file->Key . PHP_EOL;
$s3->delete_object($bucket, $file->Key);
}
}
echo "Backups older than $days days removed from S3." .
PHP_EOL . PHP_EOL;
echo 'Process Complete.' . PHP_EOL . PHP_EOL;
################################################################################
### Helper functions
################################################################################
function ensure_dir_exists($dir)
{
if (!file_exists($dir))
{
$old_umask = umask(0);
mkdir($dir, 0777, true);
umask($old_umask);
}
}