-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-mysql.php
103 lines (86 loc) · 2.54 KB
/
backup-mysql.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
<?php
$cfgBackupPath = "/root/backup";
$cfgBackupDate = date("Y-m-d_H-i-s");
$cfgServerHost = "127.0.0.1";
$cfgServerUser = "root";
$cfgServerPass = "";
$cfgBackupPurge = 7; // Max history time in days
clearstatcache();
$dir = opendir($cfgBackupPath);
while (false !== ($file = readdir($dir))) {
if (substr($file, -7) == '.tar.gz') {
if (!is_dir($cfgBackupPath . "/" . $file)) {
$cache_time = filemtime($cfgBackupPath . "/" . $file);
if (round(abs(time() - $cache_time)/60/60/24) > $cfgBackupPurge)
unlink($cfgBackupPath . "/" . $file);
}
}
}
$msg[0] = "---------------------------------------------------------------\n";
$msg[1] = "Script init\n";
$msg[2] = "Conecting DB... ";
$msg[3] = "\n*** ERROR *** Unable to connect to server!";
$msg[4] = "Backup init...\n";
$msg[5] = "Backup OK!\n";
$msg[6] = "*** ERROR *** Unable to backup!\n";
$msg[7] = "Gziping file...\n";
$msg[8] = "Purge temp files (.sql)... ";
$msg[9] = "\n\tGzip OK!\n";
$msg[10] = "\n*** ERROR *** Unable to gzip files!";
$msg[11] = "\n*** ERROR *** Unable to purge files!";
shell_exec("clear");
echo $msg[0];
echo $msg[1];
echo $msg[0];
echo $msg[2];
$db = mysql_connect($cfgServerHost, $cfgServerUser, $cfgServerPass);
if ($db) {
echo "\tOK!\n";
} else {
echo $msg[3];
$error = "echo $cfgBackupDate - $msg[3] > $cfgBackupPath/erro-$cfgBackupDate.log";
shell_exec($error);
exit();
}
$sql = "SHOW DATABASES";
$sts = mysql_query($sql, $db) or die (mysql_error());
echo $msg[0];
echo $msg[4];
while ($row = mysql_fetch_array($sts)) {
if ($row[0] != 'mysql' && $row[0] != 'information_schema') {
$tableName = $row[0];
echo "$tableName = ";
$cmd = "mysqldump --host=$cfgServerHost --user=$cfgServerUser --password=$cfgServerPass --databases $tableName > $cfgBackupPath/" . $tableName . "-" . $cfgBackupDate . ".sql";
if (!shell_exec($cmd)) {
echo $msg[5];
} else {
echo $msg[6];
$error = "echo $cfgBackupDate - $msg[6] > erro-$cfgBackupDate.log";
shell_exec($error);
}
}
}
$file = "mysql-" . $cfgBackupDate;
echo $msg[0];
echo $msg[7];
$cmd = "tar -cvzf $cfgBackupPath/$file.tar.gz $cfgBackupPath/*.sql";
if (shell_exec($cmd)) {
echo $msg[9];
} else {
echo $msg[10];
$error = "echo $cfgBackupDate - $msg[10] > $cfgBackupPath/erro-$cfgBackupDate.log";
shell_exec($error);
}
// apagar arquivos .sql
echo $msg[0];
echo $msg[8];
$cmd = "rm -f $cfgBackupPath/*.sql";
if (!shell_exec($cmd)) {
echo "OK!\n";
} else {
echo $msg[11];
$error = "echo $cfgBackupDate - $msg[11] > $cfgBackupPath/erro-$cfgBackupDate.log";
shell_exec($error);
exit();
}
echo $msg[0];