-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-php
executable file
·87 lines (75 loc) · 2.88 KB
/
backup-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
#!/usr/bin/php
<?php
require_once 'SystemWrapper.php';
require_once 'LocalRsyncBackup.php';
require_once 'RemoteRsyncBackup.php';
$dir = __DIR__.DIRECTORY_SEPARATOR;
$configFile = $dir.'config.json';
if(!file_exists($configFile)){
echo "no config file found";
exit(1);
}
$config = json_decode(file_get_contents($configFile));
$logFolder = $config->logFolder;
if(!is_dir($logFolder) || !is_writeable($logFolder)){
echo "log folder not existing or not writeable";
exit(1);
}
$backupBase = new SystemWrapper($logFolder);
$backupBase->notify('starting backup');
foreach($config->preCommands as $cmd){
$backupBase->exec($cmd);
}
#putenv('SHELL=/bin/bash');
$localBackuper = new LocalRsyncBackup($backupBase);
foreach($config->local as $local){
$t1 = time();
$localBackuper->notify('starting backup of '.$local->from.' to local secondary disc.');
$ret2 = $localBackuper->backup($local->from, $local->to, $local->toDevice, $dir.$local->excludeList);
if ($ret2) {
$tdiff = time() - $t1;
$localBackuper->log("backup complete (took $tdiff sec)");
$localBackuper->notify('backup of '.$local->from.' to local secondary disc successfull');
} else {
$localBackuper->notify('backup of '.$local->from.' failed');
}
}
#putenv('SHELL=/bin/bash');
$remoteBackuper = new RemoteRsyncBackuper($backupBase);
$home = true;
if($config->onlyHome){
$MAC = $config->homeMAC; //mac of your router at home
$curMAC = exec($dir.'gateway-mac.sh');
$home = $curMAC == $MAC;
}
if($home){
$remoteBackuper->log('at home');
$lastBackupDateFile = $dir.'last-remote-sync';
if(file_exists($lastBackupDateFile)){
$lastBackup = file_get_contents($lastBackupDateFile);
} else {
$lastBackup = '01-01-2000'; //more than one day ago
}
$remoteBackuper->log("last Backup was $lastBackup");
if(
$lastBackup != date('d-m-Y') && // the last backup is not from today
!( (intval(date("H")) <= 6 && $lastBackup == date("d-m-Y", strtotime('-1 day',time())))) //its not 0-6 am, and the last backup is from yesterday
){
foreach($config->remote as $remote){
$remoteBackuper->notify('starting backup of '.$remote->from.' to remote rsync');
$t1 = time();
$ret = $remoteBackuper->backup($remote->from, $remote->to, $dir.$remote->excludeList);
if ($ret) {
$tdiff = time() - $t1;
$remoteBackuper->log("backup complete (took $tdiff sec)");
$localBackuper->notify('backup of '.$remote->from.' to remote rsync successfull');
file_put_contents($lastBackupDateFile, date('d-m-Y'));
} else $remoteBackuper->log('backup to remote rsync server failed');
}
} else {
$remoteBackuper->log('already backupped today');
}
} else {
$remoteBackuper->log('not at home');
}
$backupBase->cleanUp();