-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlbackup.php
182 lines (160 loc) · 6.56 KB
/
mysqlbackup.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
<?php
/*mysql backup
*help you to generate backup to your database and transfer it to ftp server or save it in local server
*for any help contact me on facebook https://fb.com/escode4
*/
/*
* example to use this class
*
* require_once __DIR__.'mysqlbackup.php'; // mysqlbackup.php file path
*
* $backup = new MySqlBackup();
* $backup->mysql_host = "localhost"; // database host
* $backup->mysql_user = "root"; // database user name
* $backup->mysql_pass = "root"; // database password
* $backup->mysql_db = "testdb"; //database name
* $backup->limit_backups = "3"; // maximum number of backups in directory
* $backup->backupFtp = true; // if you want save backup on ftp server set it `true` set it false
* $backup->backupLocal = true; // if you want save backup on local server set it `true` else set it false
* $backup->backups_folder = "/home/user-name/DbBackups"; // your local backup directory
* $backup->ftp_host = "127.0.0.1"; // ftp server
* $backup->ftp_user = "ftpuser"; // ftp user name
* $backup->ftp_pass = "ftppassword"; // ftp password
* $backup->ftp_backup_folder = "files/backups"; // ftp backup directory
* $backup->sendMail = false; // send an email report
* $backup->from_mail = "[email protected]"; // from mail
* $backup->to_mail = "[email protected]"; // to mail
* $backup->subject = "new backup"; // mail subject
* echo $backup->Run();
*/
/*
* you can use crontab to schedule backups
* just type in terminal crontab -e and type "0 0 * * * php mysqlbackup,php" to generate backup every day at At 12:00 am
*/
class MySqlBackup
{
public $limit_backups = 3;
public $backups_folder;
public $mysql_db;
public $mysql_host;
public $mysql_user;
public $mysql_pass;
public $ftp_user;
public $ftp_pass;
public $ftp_host;
public $ftp_backup_folder;
public $backupFtp = true;
public $backupLocal = true;
public $sendMail = true;
public $to_mail;
public $from_mail;
public $subject;
protected $new_backup_file_name;
protected $messages;
protected $errors;
protected $ftp_conn;
public function Run()
{
$this->new_backup_file_name = $this->mysql_db . '-backup-' . date("Y-m-d-H:i:s") . '.sql.gz';
if ($this->BackupDBinLocal()) {
if ($this->backupFtp === true) {
$this->BackupDbinFtp();
}
}
return $this->SendReport();
}
protected function BackupDBinLocal()
{
$old_backups = scandir($this->backups_folder);
$sortBackups = $this->ReturnOldBackup($old_backups, "local");
exec("mysqldump -h $this->mysql_host -u $this->mysql_user -p$this->mysql_pass $this->mysql_db 2> /dev/null | gzip >$this->backups_folder/$this->new_backup_file_name ", $output, $result);
if ($result == 0) {
$this->DeleteOldBackups($sortBackups, "local");
if ($this->backupLocal === true) {
$this->messages .= "[+]The backup was successfully created on local \n[+]Backup Name:$this->new_backup_file_name \n";
}
return true;
}
$this->errors .= "[-]can't backup mysql database because mysqldump don't run correct \n";
return false;
}
protected function BackupDbinFtp()
{
$this->ftp_conn = ftp_connect($this->ftp_host);
if (!$this->ftp_conn) {
$this->errors .= "[-]can't connect to ftp server \n";
return false;
}
$login_result = ftp_login($this->ftp_conn, $this->ftp_user, $this->ftp_pass);
if ($login_result) {
$fp = fopen($this->backups_folder . '/' . $this->new_backup_file_name, 'r');
$old_backups = ftp_nlist($this->ftp_conn, $this->ftp_backup_folder);
$sortBackups = $this->ReturnOldBackup($old_backups, "ftp");
if (ftp_fput($this->ftp_conn, $this->ftp_backup_folder . '/' . $this->new_backup_file_name, $fp, FTP_BINARY)) {
$this->DeleteOldBackups($sortBackups, "ftp");
if ($this->backupLocal !== true) {
$this->DeleteOldBackups([], "", true);
}
$this->messages .= "[+]The backup was sent to ftp server successfully \n";
return true;
} else {
return false;
}
ftp_close($this->ftp_conn);
fclose($fp);
}
$this->errors .= "[-]can't login to ftp server \n";
return false;
}
protected function ReturnOldBackup($old_backups, $type = "local")
{
$ignored = ['.', '..'];
$all_backups = [];
foreach ($old_backups as $backup) {
if (in_array($backup, $ignored)) continue;
if ($type == "ftp") {
$all_backups[basename($backup)] = ftp_mdtm($this->ftp_conn, $backup);
} else {
$all_backups[basename($backup)] = filemtime($this->backups_folder . '/' . $backup);
}
}
arsort($all_backups);
return array_keys($all_backups);
}
protected function DeleteOldBackups($backups = [], $type = "local", $current = false)
{
if ($current !== false) {
unlink($this->backups_folder . '/' . $this->new_backup_file_name);
return true;
}
if (count($backups) >= $this->limit_backups) {
$first_backup = $backups[$this->limit_backups - 1];
if ($type == "ftp") {
if (!ftp_chdir($this->ftp_conn, $this->ftp_backup_folder)) {
$this->errors .= "[-]can't delete old backups because ftp_chdir not working \n";
}
if (!ftp_delete($this->ftp_conn, $first_backup)) {
$this->errors .= "[-]can't delete old backups because ftp_delete not working \n";
}
} else {
if (!unlink($this->backups_folder . '/' . $first_backup)) {
$this->errors .= "[-]can't delete old backups because unlink not working \n";
}
}
}
}
protected function SendReport()
{
if (empty($this->errors)) {
$this->messages .= "[+]My code tells me there are no problems and a backup has been created `$this->new_backup_file_name`. Please check this \n";
}
$report_msg = $this->messages . $this->errors;
if ($this->sendMail === true) {
$headers = "From: $this->from_mail \r\n" .
"Reply-To: $this->from_mail \r\n" .
"X-Mailer: PHP/" . phpversion();
mail($this->to_mail, $this->subject, $report_msg, $headers);
}
return $report_msg;
}
}