-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestoreConsole.php
299 lines (271 loc) · 12.4 KB
/
restoreConsole.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
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* http://www.cyberzo.com/
* Developed By: Vijay Naidu
* Email: vijay@cyberzo.com
* Date: 30-8-2015 IST
*/
/*
* Script should run only in console. Can also be set as cron.
* */
set_time_limit(0);
ini_set('max_execution_time', 0);
function make_array($argv,$argc){
$ini = 1;
$final_args = array();
while($ini<$argc){
$spli = explode(':', $argv[$ini]);
$final_args[$spli['0']] = $spli['1'];
$ini++;
}
return $final_args;
}
if(( php_sapi_name() != 'cli' )){
echo 'Script can be executed only from console.'."\n"; exit;
}
$armts = make_array($argv, $argc);//Making array of passed parameters.
$isStartRestoreProcess = (!empty($armts['start']))?$armts['start']:false;
if($isStartRestoreProcess != true){
//Checking for starting a restoration process.
echo 'Required valid parameter to start restore process. Please check the documentation.'."\n"; exit;
}
if(empty($armts['config'])){
echo 'No parameter received for config. '."\n"; exit;
}
include_once 'base_fns.php';
$arrBaseConfig = readConfig($armts['config']);
include_once 'VjFunctions.php';
include_once 'VjRestore.php';
$isRestoreRunningAlready = isRestoreRunningAlready();
if($isRestoreRunningAlready){
echoConsole("Restoration process is running already. SO, please wait. If it really odd. Inform to technical team.", true);
}
else{
lockRestoreProcess();// Starting Restoration process.
}
register_shutdown_function('removeRestoreProcessLock');
$arrConfig = array(
'aws_key'=>AWS_KEY,
'aws_secret'=>AWS_SECRET,
);
$VjAmazonS3 = new VjAmazonS3($arrConfig);// Setting Amazon AWS config.
$VjAmazonS3->setBucket(BUCKET_NAME);// Validate bucket name.
echoConsole('Amazon Backup path: '.AMAZON_BACKUP_PATH);
$amazonFiles = $VjAmazonS3->getFilesList(AMAZON_BACKUP_PATH);
if(!empty($amazonFiles['all'])){
$fileBackups = $amazonFiles['files'];
$sqlBackups = $amazonFiles['sql'];
$fileBackups = getSorted($fileBackups, 'desc');
$sqlBackups = getSorted($sqlBackups, 'desc');
$noOfRecordsToDisplay = getConsoleInput("No of backup records to display? (all|number)");
if(!empty($noOfRecordsToDisplay) && ($noOfRecordsToDisplay=='all' || (is_int(($noOfRecordsToDisplay-0)) && ($noOfRecordsToDisplay-0)!=0))) {
$nrc = ($noOfRecordsToDisplay=='all')?0:$noOfRecordsToDisplay;
}
else{
echoConsole('Invalid input for total number of records to display: '.$noOfRecordsToDisplay, true);
}
if(!empty($fileBackups)){
$isRestoreFiles = strtolower(getConsoleInput("Did you want to select files restore from backups? (Y|N)"));
if($isRestoreFiles=='y' || $isRestoreFiles=='n'){
if($isRestoreFiles=='y'){
//Show available file backup options.
showArrayOptionsInConsole($fileBackups, $nrc);
$optionFilesToBackup = getConsoleInput("Enter selected option to restore files from archive: ");
if(isset($fileBackups[$optionFilesToBackup])){
$selectedFileForBackup = $fileBackups[$optionFilesToBackup];
}
else{
echoConsole('Invalid option selected: '.$optionFilesToBackup, true);
}
}
}
else{
echoConsole('Invalid input received: '.$isRestoreFiles, true);
}
}
else if(!empty($sqlBackups)){
$resp = strtolower(getConsoleInput("No file backups found. Did you want to proceed to database restoration process? (Y|N)"));
if($resp!='y'){
echoConsole('Aborted restoration process. ', true);
}
}
else{
echoConsole('No previous backup files found for bucket: '.BUCKET_NAME, true);
}
if(!empty($sqlBackups)){
$isRestoreDb = strtolower(getConsoleInput("Did you want to select database restore from backups? (Y|N)"));
if($isRestoreDb=='y' || $isRestoreDb=='n'){
if($isRestoreDb=='y'){
//Show available database backup options.
showArrayOptionsInConsole($sqlBackups, $nrc);
$optionSqlToBackup = getConsoleInput("Enter selected option to restore database from archive: ");
if(isset($sqlBackups[$optionSqlToBackup])){
$selectedSqlForBackup = $sqlBackups[$optionSqlToBackup];
}
else{
echoConsole('Invalid option selected: '.$optionSqlToBackup, true);
}
}
}
else{
echoConsole('Invalid input received: '.$isRestoreDb, true);
}
}
else if(!empty($selectedFileForBackup)){
$response = strtolower(getConsoleInput("No database backups found. Did you want to restore files alone? (Y|N)"));
if($response=='y' || $response=='n'){
if($response=='n'){
echoConsole('Aborted restoration process.', true);
}
}
else{
echoConsole('Invalid input received: '.$response, true);
}
}
else{
echoConsole('No backups found aborted restoration process.', true);
}
//START PROCESS HERE.
if(empty($selectedFileForBackup) && empty($selectedSqlForBackup)){
echoConsole('Aborted restoration process.', true);
}
$arrRestoreConfig = array();
if(!empty($selectedFileForBackup)) {
if (!empty($arrRestorationConfig['path_to_restoration'])) {
$cnf = strtolower(getConsoleInput("Is it correct path you wanted to restore files? \"".$arrRestorationConfig['path_to_restoration']."\" (Y|N)"));
if($cnf=='y'){
//Continue with same path.
$arrRestoreConfig['path_to_restoration'] = $arrRestorationConfig['path_to_restoration'];
}
}
if(empty($arrRestoreConfig['path_to_restoration'])){
$arrRestoreConfig['path_to_restoration'] = getConsoleInput("Enter absolute path of files to be restored: ");
}
if(!file_exists($arrRestoreConfig['path_to_restoration']) || !is_dir($arrRestoreConfig['path_to_restoration'])){
echoConsole('Entered path not exist or is not a directory. ', true);
}
}
if(!empty($selectedSqlForBackup)){
$foundConfig = false;
if(!empty($arrRestorationConfig['host']) && !empty($arrRestorationConfig['port']) && !empty($arrRestorationConfig['username']) && !empty($arrRestorationConfig['password']) && !empty($arrRestorationConfig['database'])){
$arrCf = array();
$arrCf[] = "Host: ".$arrRestorationConfig['host'];
$arrCf[] = "Port: ".$arrRestorationConfig['port'];
$arrCf[] = "Username: ".$arrRestorationConfig['username'];
$arrCf[] = "Password: ".$arrRestorationConfig['password'];
$arrCf[] = "Database: ".$arrRestorationConfig['database'];
$amess = implode("\n", $arrCf);
echoConsole($amess);
$cnf = strtolower(getConsoleInput("Is the above details correct for restoring database? (Y|N): "));
if($cnf=='y'){
//Continue with same config.
$foundConfig = true;
$arrRestoreConfig['host'] = $arrRestorationConfig['host'];
$arrRestoreConfig['port'] = $arrRestorationConfig['port'];
$arrRestoreConfig['username'] = $arrRestorationConfig['username'];
$arrRestoreConfig['password'] = $arrRestorationConfig['password'];
$arrRestoreConfig['database'] = $arrRestorationConfig['database'];
}
}
if(!$foundConfig){
$arrRestoreConfig['host'] = getConsoleInput("Enter database hostname: ");
$arrRestoreConfig['port'] = getConsoleInput("Enter database port: ");
$arrRestoreConfig['username'] = getConsoleInput("Enter database username: ");
$arrRestoreConfig['password'] = getConsoleInput("Enter database password: ");
$arrRestoreConfig['database'] = getConsoleInput("Enter database name: ");
}
$dbconn = new mysqli($arrRestoreConfig['host'], $arrRestoreConfig['username'], $arrRestoreConfig['password'], $arrRestoreConfig['database'], $arrRestoreConfig['port']);
if ($dbconn->connect_error) {
echoConsole('Connect Error (' . $dbconn->connect_errno . ') '. $dbconn->connect_error, true);
}
else{
echoConsole("MYSQL Database credentials successful!.");
}
}
$VjRestore = new VjRestore($arrRestoreConfig);
if(!empty($selectedFileForBackup)){
//Files restoration process.
$downloadToLocalFileArchive = RESTORES_ABS_PATH.DIRECTORY_SEPARATOR.$selectedFileForBackup['file_name'];
if(file_exists($downloadToLocalFileArchive)){
echoConsole("Removing existing backup file: ".$downloadToLocalFileArchive);
unlink($downloadToLocalFileArchive);
}
echoConsole("Downloading from amazon for ".$selectedFileForBackup['key'].".");
$isDownloaded = $VjAmazonS3->downloadFile($downloadToLocalFileArchive, $selectedFileForBackup['key']);
if(file_exists($downloadToLocalFileArchive)){
$unZipCommand = "cd ".$arrRestoreConfig['path_to_restoration']." && unzip -o ".$downloadToLocalFileArchive;
$isExecuted = $VjRestore->executeCommand($unZipCommand);
if($isExecuted){
echoConsole('Executed unzip command. ');
}
else{
echoConsole('Unable to execute unzip command. ', true);
}
if(unlink($downloadToLocalFileArchive)){
echoConsole('Removed download files backup archive. ');
}
else{
echoConsole('Unable to remove download files backup archive. ');
}
echoConsole('Files process completed. ');
}
else{
echoConsole('Unable to download files backup archive. ', true);
}
}
if(!empty($selectedSqlForBackup)){
//Database restoration process.
$downloadToLocalSqlArchive = RESTORES_ABS_PATH.DIRECTORY_SEPARATOR.$selectedSqlForBackup['file_name'];
if(file_exists($downloadToLocalSqlArchive)){
echoConsole("Removing existing backup file: ".$downloadToLocalSqlArchive);
unlink($downloadToLocalSqlArchive);
}
echoConsole("Downloading from amazon for ".$selectedSqlForBackup['key'].".");
$isDownloaded = $VjAmazonS3->downloadFile($downloadToLocalSqlArchive, $selectedSqlForBackup['key']);
if(file_exists($downloadToLocalSqlArchive)){
$unZipCommand = "cd ".RESTORES_ABS_PATH." && unzip -o ".$downloadToLocalSqlArchive;
$isExecuted = $VjRestore->executeCommand($unZipCommand);
if($isExecuted){
echoConsole('Executed unzip command. ');
}
else{
echoConsole('Unable to execute unzip command. ', true);
}
if(unlink($downloadToLocalSqlArchive)){
echoConsole('Removed download sql archive. ');
}
else{
echoConsole('Unable to remove downloaded sql archive. ');
}
$sqlFileForImport = RESTORES_ABS_PATH.DIRECTORY_SEPARATOR.pathinfo($downloadToLocalSqlArchive, PATHINFO_FILENAME);
if(file_exists($sqlFileForImport)){
$sqlImportCommand = "mysql -P ".$arrRestoreConfig['port']." -h ".$arrRestoreConfig['host']." -u ".$arrRestoreConfig['username']." -p".$arrRestoreConfig['password']." ".$arrRestoreConfig['database']." < ".$sqlFileForImport."";
$isExecuted = $VjRestore->executeCommand($sqlImportCommand);
if($isExecuted){
echoConsole('Executed mysql import command. ');
}
else{
echoConsole('Unable to execute mysql import command. ', true);
}
if(unlink($sqlFileForImport)){
echoConsole('Removed imported sql file. ');
}
else{
echoConsole('Unable to remove imported sql file. ');
}
echoConsole('Database process completed. ');
}
else{
echoConsole("Sql file doesn't exist for importing to database. ", true);
}
}
else{
echoConsole('Unable to download sql backup archive. ', true);
}
}
echoConsole('Restoration process completed. ', true);
}
else{
echoConsole('No previous backup files found for bucket: '.BUCKET_NAME, true);
}
?>