@@ -12,6 +12,7 @@ class ABHelper {
12
12
const LOGLEVEL_ERR = 'error ' ;
13
13
14
14
private static $ skipStartContainers = [];
15
+ private static $ dockerCfg = null ;
15
16
16
17
public static $ targetLogLevel = '' ;
17
18
@@ -76,6 +77,13 @@ public static function backupLog(string $msg, string $level = self::LOGLEVEL_INF
76
77
mkdir (ABSettings::$ tempFolder );
77
78
}
78
79
80
+ /**
81
+ * Do not log, if the script is not running
82
+ */
83
+ if (!self ::scriptRunning ()) {
84
+ return ;
85
+ }
86
+
79
87
if ($ level != self ::LOGLEVEL_DEBUG ) {
80
88
file_put_contents (ABSettings::$ tempFolder . '/ ' . ABSettings::$ logfile , ($ skipDate ? '' : "[ " . date ("d.m.Y H:i:s " ) . "][ $ level] " ) . " $ msg " . ($ newLine ? "\n" : '' ), FILE_APPEND );
81
89
}
@@ -227,33 +235,25 @@ public static function sortContainers($containers, $order, $reverse = false, $re
227
235
public static function backupContainer ($ container , $ destination ) {
228
236
global $ abSettings , $ dockerClient ;
229
237
230
- self :: backupLog ( " Backup { $ container [ ' Name ' ]} - Container Volumeinfo: " . print_r ($ container ['Volumes ' ], true ), self :: LOGLEVEL_DEBUG );
238
+ $ containerSettings = $ abSettings -> getContainerSpecificSettings ($ container ['Name ' ] );
231
239
232
- $ stripAppdataPath = '' ;
240
+ self :: backupLog ( " Backup { $ container [ ' Name ' ]} - Container Volumeinfo: " . print_r ( $ container [ ' Volumes ' ], true ), self :: LOGLEVEL_DEBUG ) ;
233
241
234
- // Get default docker storage path
235
- $ dockerCfgFile = ABSettings::$ dockerIniFile ;
236
- if (file_exists ($ dockerCfgFile )) {
237
- self ::backupLog ("Parsing $ dockerCfgFile " , self ::LOGLEVEL_DEBUG );
238
- $ dockerCfg = parse_ini_file ($ dockerCfgFile );
239
- if ($ dockerCfg ) {
240
- if (isset ($ dockerCfg ['DOCKER_APP_CONFIG_PATH ' ])) {
241
- $ stripAppdataPath = $ dockerCfg ['DOCKER_APP_CONFIG_PATH ' ];
242
- self ::backupLog ("Got default appdataPath: $ stripAppdataPath " , self ::LOGLEVEL_DEBUG );
243
- }
244
- } else {
245
- self ::backupLog ("Could not parse $ dockerCfgFile " , self ::LOGLEVEL_DEBUG );
246
- }
242
+ $ volumes = self ::examineContainerVolumes ($ container );
243
+ $ dockerAppdataPath = self ::getDockerAppdataPath ();
244
+ if (empty ($ dockerAppdataPath )) {
245
+ ABHelper::backupLog ("Docker appdata path could not be examined! " , self ::LOGLEVEL_ERR );
246
+ return false ;
247
247
}
248
248
249
-
250
- $ volumes = [];
251
- foreach ($ container ['Volumes ' ] ?? [] as $ volume ) {
252
- $ hostPath = explode (": " , $ volume )[0 ];
253
- if (!empty ($ stripAppdataPath ) && strpos ($ volume , $ stripAppdataPath ) === 0 ) {
254
- $ hostPath = ltrim (str_replace ($ stripAppdataPath , '' , $ hostPath ), '/ ' );
249
+ if (true || $ containerSettings ['backupExtVolumes ' ] == 'no ' ) {
250
+ self ::backupLog ("Should NOT backup ext volumes, sanitizing... " , self ::LOGLEVEL_DEBUG );
251
+ foreach ($ volumes as $ index => $ volume ) {
252
+ if (str_starts_with ($ volume , '/ ' )) {
253
+ self ::backupLog ("Removing volume " . $ volume . " because ext volumes should be skipped " , self ::LOGLEVEL_DEBUG );
254
+ unset($ volumes [$ index ]);
255
+ }
255
256
}
256
- $ volumes [] = $ hostPath ;
257
257
}
258
258
259
259
if (empty ($ volumes )) {
@@ -265,14 +265,10 @@ public static function backupContainer($container, $destination) {
265
265
266
266
$ destination = $ destination . "/ " . $ container ['Name ' ] . '.tar ' ;
267
267
268
- $ containerSettings = $ abSettings ->getContainerSpecificSettings ($ container ['Name ' ]);
269
-
270
268
$ tarVerifyOptions = ['--diff ' ];
271
269
$ tarOptions = ['-c ' ];
272
270
273
- if (!empty ($ stripAppdataPath )) {
274
- $ tarOptions [] = $ tarVerifyOptions [] = '-C ' . escapeshellarg ($ stripAppdataPath );
275
- }
271
+ $ tarOptions [] = $ tarVerifyOptions [] = '-C ' . escapeshellarg ($ dockerAppdataPath );
276
272
277
273
switch ($ abSettings ->compression ) {
278
274
case 'yes ' :
@@ -337,6 +333,7 @@ public static function backupContainer($container, $destination) {
337
333
/**
338
334
* Special debug: The creation was ok but verification failed: Something is accessing docker files! List docker info for this container
339
335
*/
336
+ $ output = null ; // Reset exec lines
340
337
exec ("ps aux | grep docker " , $ output );
341
338
self ::backupLog ("ps aux docker: " . PHP_EOL . print_r ($ output , true ), self ::LOGLEVEL_DEBUG );
342
339
$ nowRunning = $ dockerClient ->getDockerContainers ();
@@ -371,4 +368,49 @@ public static function scriptRunning($externalCmd = false) {
371
368
public static function abortRequested () {
372
369
return file_exists (ABSettings::$ tempFolder . '/ ' . ABSettings::$ stateFileAbort );
373
370
}
371
+
372
+ public static function getDockerAppdataPath () {
373
+ $ dockerAppdataPath = '' ;
374
+
375
+ // Get default docker storage path
376
+ if (empty (self ::$ dockerCfg )) {
377
+ $ dockerCfgFile = ABSettings::$ dockerIniFile ;
378
+ if (file_exists ($ dockerCfgFile )) {
379
+ self ::backupLog ("Parsing $ dockerCfgFile " , self ::LOGLEVEL_DEBUG );
380
+ $ dockerCfg = parse_ini_file ($ dockerCfgFile );
381
+ ABHelper::backupLog ("dockerCfg: " . PHP_EOL . print_r ($ dockerCfg , true ), self ::LOGLEVEL_DEBUG );
382
+ if ($ dockerCfg ) {
383
+ self ::$ dockerCfg = $ dockerCfg ;
384
+ } else {
385
+ self ::backupLog ("Could not parse $ dockerCfgFile " , self ::LOGLEVEL_ERR );
386
+ return false ;
387
+ }
388
+ }
389
+ } else {
390
+ ABHelper::backupLog ("Got dockerCfg from cache. " , ABHelper::LOGLEVEL_DEBUG );
391
+ $ dockerCfg = self ::$ dockerCfg ;
392
+ }
393
+
394
+ if (isset ($ dockerCfg ['DOCKER_APP_CONFIG_PATH ' ])) {
395
+ $ dockerAppdataPath = $ dockerCfg ['DOCKER_APP_CONFIG_PATH ' ];
396
+ } else {
397
+ self ::backupLog ("dockerCfg is there but no Appdata path iset set?? " , self ::LOGLEVEL_ERR );
398
+ }
399
+ return $ dockerAppdataPath ;
400
+ }
401
+
402
+ public static function examineContainerVolumes ($ container ) {
403
+
404
+ $ dockerAppdataPath = self ::getDockerAppdataPath ();
405
+
406
+ $ volumes = [];
407
+ foreach ($ container ['Volumes ' ] ?? [] as $ volume ) {
408
+ $ hostPath = explode (": " , $ volume )[0 ];
409
+ if (!empty ($ dockerAppdataPath ) && str_starts_with ($ volume , $ dockerAppdataPath )) {
410
+ $ hostPath = ltrim (str_replace ($ dockerAppdataPath , '' , $ hostPath ), '/ ' );
411
+ }
412
+ $ volumes [] = rtrim ($ hostPath , '/ ' );
413
+ }
414
+ return $ volumes ;
415
+ }
374
416
}
0 commit comments