-
Notifications
You must be signed in to change notification settings - Fork 1
/
db-backup
executable file
·106 lines (89 loc) · 2.95 KB
/
db-backup
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
#!/usr/bin/env php
<?php
namespace Loadsys\CakePHPShellScripts\DbBackup;
/**
* Prints usage information and exits.
*
* @param string $script Typically set to basename($argv[0]) when the method is called.
* @param int $exitCode The numeric exit code to return. 0 = success. >0 = failure.
* @return void
*/
function usage($script = null, $exitCode = 0) {
$script = $script ?: basename(__FILE__);
$usage = <<<EOD
${script}
Executes a mysqldump of the Cake app's `default` database
into a ZIP file located in a backups/ folder in the project
root (created if necessary). Outputs the ZIP file's size as
a crude verification step. Should be run from the project
root folder. Will exit with an error code of 1 if database
credentials could not be obtained from the Cake app via the
`db-credentials` script.
Usage:
bin/${script}
EOD;
echo $usage;
exit($exitCode);
}
/**
* Convert numeric bytes into human-readable format.
*
* Example: 1535 ==> 1.50K.
* Note that this only works with integer byte values.
*
* @param int $bytes Integer number of bytes to translate.
* @param int $decimals Number of decimal places to preserve in output.
* @return string The converted value with the "closest" unit used.
*/
function human_filesize($bytes, $decimals = 2) {
$sz = 'BKMGTPEZY';
$factor = floor((strlen(intval($bytes)) - 1) / 3);
return (sprintf("%.{$decimals}f", intval($bytes) / pow(1024, $factor)) + 0) . @$sz[$factor];
}
/**
* main() ===================================================================
*/
if (isset($argv[1]) && $argv[1] == '-h') {
usage();
}
// Set up variables.
$backupDir = 'backups';
$date = date('Ymd-His');
try {
$credentialsScript = 'bin/db-credentials';
if (!is_readable($credentialsScript)) {
echo "!! DB credentials are not available. Aborting.";
exit(3);
}
ob_start(); // Capture the shebang line from db-credentials.
$db = include($credentialsScript);
ob_get_clean(); // Flush it.
if ($db instanceof \Exception) {
echo $db->getMessage();
exit(4);
}
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
// Create an appropriate password clause for the command line.
if (empty($db->password)) {
$passClause = '';
} else {
$passClause = ' --password=' . escapeshellcmd($db->password);
}
// Create the backup directory if it doesn't exist.
if(!file_exists($backupDir) || !is_dir($backupDir)) {
mkdir($backupDir, 0777);
}
// Build the command lines to execute.
$sqlFile = $backupDir . "/{$db->database}_{$date}.sql";
$zipFile = "{$sqlFile}.zip";
$dumpCmd = "mysqldump --host={$db->host} --user={$db->username}{$passClause} {$db->database} > {$sqlFile}\n";
$compressCmd = "zip -jrq8 {$zipFile} {$sqlFile}; rm {$sqlFile}";
//echo $db->database . ' ' . $db->username . ' ' . $db->password . "\n";
// Execute the commands.
echo "## Backing up database '{$db->database}' to: {$zipFile}\n";
system($dumpCmd);
system($compressCmd);
echo "## Database backup complete: " . human_filesize(filesize($zipFile)) . PHP_EOL;