-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemWrapper.php
57 lines (48 loc) · 1.47 KB
/
SystemWrapper.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
<?php
/**
* the base class for a all backup classes. handles interaction with the console. linux only
*
* @author Jonas Brekle <[email protected]>
*/
class SystemWrapper {
public $folder = "";
public $file = "";
public $logStr = "";
function __construct($folder) {
$this->folder = $folder;
$this->file = $folder . date('Y-m-d_H:i:s') . '.log';
$retArr = array();
$retCode = 0;
exec('whoami', $retArr, $retCode);
if ($retArr[0] != 'root') {
throw new Exception('need to be root');
}
}
function log($str) {
$str .= "\n";
echo $str;
$this->logStr .= $str;
file_put_contents($this->file, $str, FILE_APPEND);
}
function notify($str){
$this->log($str);
exec('notify-send "Backup: " "'.$str.'" -i /usr/share/pixmaps/gnome-schedule/gnome-schedule.svg -t 1000');
}
# my exec wrapper with logging
function exec($str) {
$retCode = 1;
$str = "ionice -c3 nice -n 19 $str >> " . $this->file ." 2>&1" ;
$this->log("executing cmd: \"".$str."\"");
system($str, $retCode);
$this->log("exit status: ".$retCode);
$this->log("");
return $retCode;
}
function cleanUp() {
# write through
$this->exec("sync");
$this->log("clean up old log files");
$this->exec("find " . $this->folder . " -name \"*.log\" -mtime +14 -exec rm {} \;");
}
}
?>