-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVjBackup.php
195 lines (172 loc) · 7.55 KB
/
VjBackup.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
<?php
/**
* http://www.cyberzo.com/
* Developed By: Vijay Naidu
* Email: vijay@cyberzo.com
* Date: 28-8-2015 IST
*/
class VjBackup{
private $absolutePathOfWpFiles = "";
private $backupFilePath = "";
private $backupSqlFilePath = "";
private $backupSqlZipPath = "";
private $excludeFilePathsForBackup = array();
private $isDoDatabaseBackup = false;
private $databaseConfig = array(
'host'=>'',
'port'=>'',
'username'=>'',
'password'=>'',
'database'=>'',
);
public function __construct($arrConfig = array()){
if(!empty($arrConfig['files_to_make_backup'])) {
$this->absolutePathOfWpFiles = $arrConfig['files_to_make_backup'];
}
else{
echo "Undefined path of files to be backup. \n"; exit;
}
if(!empty($arrConfig['backup_file'])) {
$this->backupFilePath = $arrConfig['backup_file'];
}
else{
echo "Undefined path of backup file to be stored temp. \n"; exit;
}
if(!empty($arrConfig['exclude_file_paths_for_backup'])) {
$this->excludeFilePathsForBackup = $arrConfig['exclude_file_paths_for_backup'];
}
if(isset($arrConfig['is_database_backup'])){
if($arrConfig['is_database_backup'] == true){
$this->isDoDatabaseBackup = true;
if(!empty($arrConfig['database_host']) && !empty($arrConfig['database_port']) && !empty($arrConfig['database_user']) && !empty($arrConfig['database_password']) && !empty($arrConfig['database_name']) && !empty($arrConfig['backup_sql_path']) && !empty($arrConfig['backup_sql_zip_path'])){
$this->databaseConfig = array(
'host'=>$arrConfig['database_host'],
'port'=>$arrConfig['database_port'],
'username'=>$arrConfig['database_user'],
'password'=>$arrConfig['database_password'],
'database'=>$arrConfig['database_name'],
);
$this->backupSqlFilePath = $arrConfig['backup_sql_path'];
$this->backupSqlZipPath = $arrConfig['backup_sql_zip_path'];
}
else{
$this->echoConsole("Database backup requested. But empty or invalid parameters!. Turn off backup database if you not required.", true);
}
}
}
}
public function backup(){
$arr = array(
'status'=>false,
'backup_file_path'=>'',
'backup_file_name'=>'',
'backup_file_size'=>'',
'backup_sql_path'=>'',
'backup_sql_name'=>'',
'backup_sql_size'=>'',
);
$this->echoConsole('Backup process started. ');
$backupDone = $this->_doBackup();
if($backupDone['status']){
$arr = array(
'status'=>true,
'backup_file_path'=>$this->backupFilePath,
'backup_file_name'=>pathinfo($this->backupFilePath, PATHINFO_BASENAME),
'backup_file_size'=>filesize ($this->backupFilePath),
);
if(!empty($backupDone['db_backup'])){
if(file_exists($backupDone['db_backup'])) {
$arr['backup_sql_path'] = $backupDone['db_backup'];
$arr['backup_sql_name'] = pathinfo($backupDone['db_backup'], PATHINFO_BASENAME);
$arr['backup_sql_size'] = filesize($backupDone['db_backup']);
}
}
}
return $arr;
}
public function echoConsole($mess = '', $isExit = false){
echoConsole($mess, $isExit);
}
private function _doBackup(){
$details = array(
'status'=> false,
'db_backup'=> '',
'file_backup'=> '',
);
if(file_exists($this->backupFilePath)) {
unlink($this->backupFilePath);
}
$this->echoConsole('Doing backup started.');
if($this->isDoDatabaseBackup){
//Database backup need to be done.
$databaseBackupCmd = "mysqldump -P ".$this->databaseConfig['port']." -h ".$this->databaseConfig['host']." -u ".$this->databaseConfig['username']." -p".$this->databaseConfig['password']." --routines ".$this->databaseConfig['database']." > ".$this->backupSqlFilePath;
$this->echoConsole('Executing command '.$databaseBackupCmd);
if($this->executeCommand($databaseBackupCmd)){
if(file_exists($this->backupSqlFilePath)){
//$sqlZipCommand = "zip ".$this->backupSqlZipPath." ".$this->backupSqlFilePath;
$sqlZipCommand = "cd ".pathinfo($this->backupSqlFilePath, PATHINFO_DIRNAME)." && ";
$sqlZipCommand .= "zip ".$this->backupSqlZipPath." ".pathinfo($this->backupSqlFilePath, PATHINFO_BASENAME);
if($this->executeCommand($sqlZipCommand)){
if(file_exists($this->backupSqlZipPath)){
unlink($this->backupSqlFilePath);
$details['status'] = true;
$details['db_backup'] = $this->backupSqlZipPath;
$this->echoConsole('Generated Database backup zip file. ');
}
else {
$this->echoConsole('Sql zip backup file not exist. '.$this->backupSqlZipPath, true);
}
}
else{
$this->echoConsole('Unable to zip sql backup file. '.$this->backupSqlZipPath, true);
}
}
else{
$this->echoConsole('Unable to dump sql backup file. '.$this->backupSqlFilePath, true);
}
}
else{
$this->echoConsole('Unable to generate sql backup file. Please make sure to enable shell_exec or exec.', true);
}
}
$excludeFilesCommand = '';
if(!empty($this->excludeFilePathsForBackup)){
if(is_array($this->excludeFilePathsForBackup)){
$excludeFilesCommand = " -x ".implode(" ", $this->excludeFilePathsForBackup);
}
}
//$cmd = "zip -r ".$this->backupFilePath." ".$this->absolutePathOfWpFiles.$excludeFilesCommand;//TODO: Full path zip testing..
$cmd = "cd ".$this->absolutePathOfWpFiles." && zip -r ".$this->backupFilePath." * ".$excludeFilesCommand;
$this->echoConsole('Executing command '.$cmd);
if($this->executeCommand($cmd)){
if(file_exists($this->backupFilePath)) {
$this->echoConsole('Backup generated.');
$details['status'] = true;
$details['file_backup'] = $this->backupFilePath;
}
else{
$this->echoConsole('Unable to generate backup file. Command executed \n');
$this->echoConsole($cmd, true);
}
}
else{
$this->echoConsole('Unable to generate backup file. Please make sure to enable shell_exec or exec.', true);
}
return $details;
}
public function executeCommand($command = ""){
$isExecuted = false;
if(!empty($command)){
if(function_exists('shell_exec')) {
shell_exec($command);
$isExecuted = true;
}
else if(function_exists('exec')){
exec($command);
$isExecuted = true;
}
}
return $isExecuted;
}
}
?>