/
mysql_backup_manager.php
executable file
·287 lines (257 loc) · 9.08 KB
/
mysql_backup_manager.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
* MySQL Backup Manager
* PHP based MySQL Backup Manager script used to backup the databases specified in the database controlling the script
* @author JA Clarke
* @since 2010/03/23
*
* @version 0.1
* @description
* - Initial Framework configuration
* - Command line option getting and filtering
* @todo
* - Implement DB getting
*
* @version 0.2
* @description
* - Added Project Spook implementation
*
* @version 0.3
* @description
* - Added MD5 file CRC's for compressed DB backup files
* - Added proper commenting to the functions and classes as well as core internal code segments
* - Added the time limit line so that it will not timeout due to a DB backup operation taking to long
* - Fixed a couple of simulated/non-simulated run options
*
*/
/**
* Set the time limit to zero (0) so that the script cannot time out due to a long backup process
*/
set_time_limit(0);
/**
* Setup the core defines
*/
define( "LIVE", true );
define( "DEBUG", false );
define( "APPID", "309401bf" );
$system_version = "0.3";
// Load the config file
if( LIVE === true ) :
require_once "/ts_systems/config/config.php";
include "/ts_systems/systemreporter/agent/system.reporter.agent.php";
else :
require_once "/www/bill/systems/config/config.php";
include "/www/bill/systems/systemreporter/agent/system.reporter.agent.php";
endif;
/**
* Class MySQLHandler
*/
class MySQLHandler
{
/**
* Public Function runBackup
* @param db string The name of the database that the backup is going to be executed for
* @param simulation boolean Whether or not the run is a simulated run or actual run
* @param color boolean Whether or not output should be generated in color code or not
*/
public function runBackup( $db, $simulation = false, $color = false )
{
// Check if the system is live or not
if( LIVE === true ) :
$backupDir = "/ts_backups/mysql_backups/";
else :
$backupDir = "/srv_admin/srv_backups/mysql/";
endif;
// Full backups are run on Friday's, setup the commands accordingly
if( date("D") == "Fri" ) :
$backupDir .= "full_backups/";
$mysql_options = "";
else :
$backupDir .= "incremental_backups/";
$mysql_options = "--flush-logs --delete-master-logs";
endif;
$backupDir .= date("d_m_Y") . "/";
// Check if the backup directory exists, else create it
if( !is_dir( $backupDir ) ) :
mkdir($backupDir);
endif;
// Setup the backup command
$command = "mysqldump -u " . MySQLBackupDB::USER . " -p" . MySQLBackupDB::PASS . " -h " . MySQLBackupDB::DBHOST . " --port=" . MySQLBackupDB::DBPORT . " " . (DEBUG?"--verbose":"") . " {$db} {$mysql_options} | gzip -m9 > {$backupDir}{$db}.sql.gz";
if( DEBUG === true ) : OutputHandler::displayOutput( "[%lightgreen%DEBUG%lightgray%] MySQL Command : %lightblue%{$command}%lightgray%", $color ); endif;
// Check if it is a simulated run
if( !$simulation ) :
exec($command, $command_output);
else :
$command_output = array( "Backup complete" );
endif;
// Output the results from the command that was executed
foreach( $command_output as $cmd_output ) :
OutputHandler::displayOutput( "[%lightblue%{$db}%lightgray%]\t" . $cmd_output, $color );
endforeach;
}
}
/**
* Class MySQL_Backup_System
*/
class MySQL_Backup_System
{
/**
* Private class members
*/
private $debug_data = null;
private $current_db = null;
private $simulation = null;
private $color = null;
/**
* Overrridden constructor
* @param sim boolean Enable/disable simulation mode
* @param clr boolean Enable/disable color output of content
*/
public function __construct( $sim = false, $clr = false )
{
$this -> simulation = $sim;
$this -> color = $clr;
OutputHandler::displayOutput("%lightblue%Starting system...%lightgray%", $this -> color);
SR_Agent::Log( APPID, SystemReporter::MSG_MESSAGE, "Starting System" );
}
/**
* Public function runBackups
*/
public function runBackups()
{
$mysqlDBHandler = new MySQLHandler();
//$dbList = DB::getDBList($this -> color);
$dbList = explode( "|", MySQLBackupDB::DATABASES );
foreach( $dbList as $db ) :
SR_Agent::Log( APPID, SystemReporter::MSG_MESSAGE, "Starting Backup for {$db}" );
OutputHandler::displayOutput( "[%lightblue%{$db}%lightgray%]\tStarting backup\n%white%============================================%lightgray%", $this -> color );
$mysqlDBHandler -> runBackup( $db, $this -> simulation, $this -> color );
OutputHandler::displayOutput( "%white%============================================%lightgray%\n", $this -> color );
endforeach;
$this -> completeRun();
}
/**
* Private Fucntion completeRun
* @description
* Function is called after all the databases have been backed up, in order to create a final MD5 checksum of the databases and then compress it all as a single file
*/
private function completeRun()
{
// Check if it is the LIVE environment
if( LIVE === true ) :
$backupDir = "/ts_backups/mysql_backups/";
else :
$backupDir = "/srv_admin/srv_backups/mysql/";
endif;
// Check if it was a full or incremental backup run
if( date("D") == "Fri" ) :
$backupDir .= "full_backups/";
else :
$backupDir .= "incremental_backups/";
endif;
// Setup paths and filenames
$folder = $backupDir;
$backupDir .= date("d_m_Y") . "/";
$compressed_file = $folder . date("d_m_Y") . ".tar.gz";
// Check if it is a simulated run
if( !$this -> simulation ) :
exec("md5sum {$backupDir}/*.tar.gz > {$backupDir}/databases.md5");
if( DEBUG === true ) : OutputHandler::displayOutput( "[%lightgreen%DEBUG%lightgray%] Generated MD5 Hashes of DB Backups%lightgray%", $this -> color ); endif;
endif;
$command = "tar -zcvf {$compressed_file} {$backupDir}";
if( DEBUG === true ) : OutputHandler::displayOutput( "[%lightgreen%DEBUG%lightgray%] Command : %lightblue%{$command}%lightgray%", $this -> color ); endif;
// Check if it is a simulated run
if( !$this -> simulation ) :
exec($command, $command_output);
// Generate MD5 checksum of the compressed file (usefull for integrity checks)
exec("md5sum {$compressed_file} > {$compressed_file}.md5");
else :
$command_output = array( "Folder compressed" );
endif;
// Check if debugging is enabled
if( DEBUG === true ) :
// Display debugging information
foreach( $command_output as $cmd_output ) :
OutputHandler::displayOutput( "[%lightgreen%DEBUG%lightgray%] [%lightblue%{$backupDir}%lightgray%]\t" . $cmd_output, $this -> color );
endforeach;
endif;
// Check if it is a simulated run
if( !$this -> simulation ) :
exec("rm -rf {$backupDir}");
endif;
SR_Agent::Log( APPID, SystemReporter::MSG_SUCCESS, "Backup complete for " . date("d/m/Y") );
}
}
## Failsafe
if( !class_exists( "OutputHandler" ) ) :
class OutputHandler
{
static public function displayOutput($output, $color = false)
{
$o = $output;
$colors = array(
"%lightgray%" => "\033[0;30m",
"%darkgrey%" => "\033[1;30m",
"%blue%" => "\033[0;34m",
"%lightblue%" => "\033[1;34m",
"%green%" => "\033[0;32m",
"%lightgreen%" => "\033[1;32m",
"%cyan%" => "\033[0;36m",
"%lightcyan%" => "\033[1;36m",
"%red%" => "\033[0;31m",
"%lightred%" => "\033[1;31m",
"%purple%" => "\033[0;35m",
"%lightpurple%" => "\033[1;35m",
"%brown%" => "\033[0;33m",
"%yellow%" => "\033[1;33m",
"%lightgray%" => "\033[0;37m",
"%white%" => "\033[1;37m"
);
foreach( $colors as $key => $value ) :
if( $color === true ) :
$o = str_replace( $key, $value, $o );
else :
$o = str_replace( $key, "", $o );
endif;
endforeach;
echo $o . "\n";
}
}
endif;
// Setup default values
$simulation = false;
$color = false;
$version = false;
// Get argument list from command line and setup variable accordingly
foreach( $argv as $arg ) :
switch( $arg ) :
case "--simulation" : $simulation = true;
break;
case "--color" : $color = true;
break;
case "--version" : $version = true;
break;
endswitch;
endforeach;
// Check if version was not called
if( $version === false ) :
$backup_system = new MySQL_Backup_System( $simulation, $color );
echo $backup_system -> runBackups();
OutputHandler::displayOutput("%lightblue%System run complete...%lightgray%", $color);
else :
$date = "2010/05/07";
$version_info = <<<VERSION
MySQL Backup Manager Script, {$date}
Author\t: %white%JA Clarke%lightgray%
Version\t: %white%{$system_version}%lightgray%
Usage\t:
%lightblue%php %white%mysql_backup_manager.php %lightgreen%[--simulation] [--color] [--version]%lightgray%
%white%--simulation%lightgray%\t:\tRuns a simulated MySQL backup (useful for debugging)
%white%--color%lightgray%\t\t:\tDisplays the output as color formatted
%white%--version%lightgray%\t:\tDisplays this information
\n
VERSION;
OutputHandler::displayOutput( $version_info, true );
endif;
exit(0);
?>