-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.php
85 lines (72 loc) · 2.4 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
<?php
namespace OpenCloud;
define('RAXSDK_TIMEOUT', '3600');
require_once('lib/rackspace.php');
date_default_timezone_set('UTC');
// Database connection settings
try {
$db_host = $_GET["db_host"];
$db_user = $_GET["db_user"];
$db_password = $_GET["db_password"];
$db_name = $_GET["db_name"];
} catch (Exception $e) {
echo "Database connection settings are missing.\n";
die();
}
// Rackspace Cloud Files API settings
try {
$username = $_GET["cf_username"];
$key = $_GET["cf_apikey"];
$datacenter = $_GET["cf_datacenter"];
} catch (Exception $e) {
echo "Cloud Files API settings are missing.\n";
die();
}
$date = date("Y-m-d");
$remotefile = "$db_name-backup-$date.zip";
$localfile = tempnam(sys_get_temp_dir(), 'Backup');
// Dump database into local folder
$db_connection = mysql_connect($db_host, $db_user, $db_password);
if (!$db_connection) {
echo "Database connection failed.\n";
die();
} else {
echo "Starting database dump ... ";
shell_exec("mysqldump -h $db_host -u $db_user --password='$db_password' $db_name | gzip -9 > $localfile");
echo "Database dump complete.\n";
}
// Try to connect to Cloud Files
echo "Connecting to Cloud Files ... ";
try {
define('AUTHURL', 'https://identity.api.rackspacecloud.com/v2.0/');
$connection = new Rackspace(AUTHURL, ['username' => $username, 'apiKey' => $key]);
$ostore = $connection->ObjectStore('cloudFiles', "$datacenter");
echo "Connected to Cloud Files.\n";
} catch (HttpUnauthorizedError $e) {
echo "Cloud Files API connection could not be established.\n";
shell_exec("rm $localfile;");
die();
}
// Create Container
echo "Creating Cloud Files Container ... ";
$cont = $ostore->Container();
$cont->Create(array('name'=>"$db_name-cron-backups"));
echo "Cloud Files container created or already exists.\n";
// Move backup to Cloud Files
echo "Moving backup to Cloud Files ... ";
$obj = $cont->DataObject();
$obj->Create(array('name' => "$remotefile", 'content_type' => 'application/x-gzip'), $filename="$localfile");
$etag = $obj->hash;
// Test file integrity
if (md5_file($localfile) != $etag) {
$obj->Delete(array('name'=>"$remotefile"));
echo "Backup failed integrity check.\n";
} else {
echo "Backup moved to Cloud Files Successful.\n";
}
// Local cleanup
echo "Cleaning up local backups ... ";
shell_exec("rm $localfile;");
echo "Local backups cleaned up.\n";
echo "Backup complete.\n";
?>