-
Notifications
You must be signed in to change notification settings - Fork 7
/
Backuper.php
298 lines (267 loc) · 9.35 KB
/
Backuper.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
<?php
require_once("BackuperIndex.php");
interface IBackuper{
/*!
initializes backuper plugin with prefs for it
@param mixed $prefs can be anything your plugin will understand
*/
function __construct($prefs);
/*!
used to backup all information which needs backup
@param PharData &$arch where backups will be packed to
usually a backuper creates some subfolders in the archive and strores backup data in them, for example /base/MySQL for MySQLBackuper or /files and /patches for FileTreeBackuper
@return ['comment'=>[ something which can be serialized to JSON, it will be included to archive comment...]
*/
function makeBackup(&$arch);
/*!
makes opperations needed for backup for example builds graph or looking for modified files
takes database object to store index data
usually you will want to use BackuperIndex subclass to mantain base
@param PDO &$base opened db connection, don't close it (you can close it but must reopen .... may be in another base file, but it is strongly unrecommended to do so)
*/
function prepareForBackup(PDO &$base);
/*!
determines wheither backup is needed for this plugin
@returns boolean|integer does this plugin have work to do
*/
function needBackup();
};
interface IUploader{
function __construct($prefs);
/*!
@param string $fileName pathname of the zip archive
@param string &$as desired name of the zip archive on the target backup location, usually the same
*/
function upload(string $fileName,string $as);
};
/*!
Main backuper class.
Manages plugins and resources and use them to make backups and uploads.
It doesn't (and mustn't) implement IBackuper because it is not a plugin.
*/
class Backuper{
public $index;
static $backupsDir;//!< folder
static $indexFileName="backupIndex.sqlite";//!< filename for database file (of course you can change it, don't forget to rename index file)
public $arch=null;//!< PharData where everything will be packed
public $archiveFormat;//!< archive format
public $archiveCompression;//!< archive compression
public $archiveFileExtension;//!< archive file extension
const defaultArchiveFormat='zip.gz';
public $plugins;//!< here plugins will be stored, look initPlugins
/*!
@param array $prefs preferences (auth information, some other, etc...) for plugins
array("pluginName"=>array("pref1"=>"ololo","pref2"=>"trololo"),"archive"=>"tar,tar.gz,tar.bz2,zip or phar")
*/
function __construct($prefs){
echo "consructing Backuper\n<br/>";
$filename=static::$backupsDir.'/'.static::$indexFileName;//because by reference
$this->index=new BackuperIndex($filename);
static::initPlugins($prefs);
if(isset($prefs['archive']))
static::determineArchiveFormatByString($prefs['archive']);
else static::determineArchiveFormatByString(static::defaultArchiveFormat);
}
/*!
@param string $compStr in format "archiveType:compression"
tar.gz means tarball compressed with gzip and generates a file with 'tar.gz' extension
zip.gz means zip archive every file in it is compressed with gzip serparately
zip means zip archive without compression
see http://www.php.net/manual/en/phardata.compressfiles.php and http://php.net/manual/en/phar.compress.php for details
*/
function determineArchiveFormatByString($compStr){
$arr=explode(".",strtolower($compStr));
$exts=new StdClass();
if(empty($arr[1]))$arr[1]="";
switch($arr[0]){
case "zip":
$this->archiveFormat=Phar::ZIP;
break;
case "tar":
$this->archiveFormat=Phar::TAR;
break;
case "phar":
$this->archiveFormat=Phar::PHAR;
break;
default:
throw new Exception('Unknown archive format :'.$arr[0]);
break;
}
switch($arr[1]){
case "":
case "store":
$this->archiveCompression=Phar::NONE;
break;
case "bz2":
$this->archiveCompression=Phar::BZ2;
break;
case "gz":
$this->archiveCompression=Phar::GZ;
break;
default:
throw new Exception('Unknown compression :'.$arr[0]);
break;
}
if(!Phar::canCompress($this->archiveCompression)){
throw new Exception("Compression type is not supported");
}
$exts->extension=$arr[0];
$exts->suffix=$this->archiveFormat!=Phar::ZIP?'.'.$arr[1]:'';
$this->archiveFileExtension=$exts;
new dBug($this->archiveFileExtension);
return true;
}
/*!
initializes the plugins (backupers and uploaders)
*/
function initPlugins($prefs){
$this->plugins=new stdClass;
foreach(array("upload","backup") as $pltype){
if(isset($prefs[$pltype])&&is_array($prefs[$pltype])){
$this->plugins->$pltype=array();
foreach($prefs[$pltype] as $pluginName=>&$pluginPrefs){
$plnm=$pluginName.ucfirst($pltype)."er";
try{
if(!include_once ($plnm.".php"))throw new Exception('Plugin '.$plnm.' was not found');
$this->plugins->{$pltype}[$pluginName]=new $plnm($pluginPrefs);
echo 'Plugin '.$plnm.' <font color="green">was successfully initialized</font>'."\n".'<br/>';
}
catch(Exception $err){
echo 'Plugin '.$plnm.' <font color="red">FAILED</font> to initialize. : '.get_class($err).' : '.$err->getCode().' : '.$err->getMessage()."\n<br/>";
new dBug($err);
}
}
}
}
}
/*!
adds index file to archive
*/
function backupIndex(){
//$this->index->save();
$this->arch->addFile($this->index->fileName,static::$indexFileName);
}
function needBackup(){
if(empty($this->plugins->backup))return 0;
foreach($this->plugins->backup as $bname => &$backuper){
if(!$backuper->needBackup()){
unset($this->plugins->backup[$bname]);
echo "$bname : backup is not needed\n<br/>";
}
}
//var_dump($this->plugins->backup);
if(empty($this->plugins->backup))
return 0;
else
return 1;
}
/*!
the method to be called by user to make backups
starts backup process
*/
function makeBackup(){
$time=time();
static::prepareForBackup();
echo "<hr color='magenta'/>";
if(!$this->needBackup()){
echo "backup is not needed <br/>";
return 0;
}
$this->archFileShortName=$time.'.'.$this->archiveFileExtension->extension;
$this->archFileName=static::$backupsDir.'/'.$this->archFileShortName;
new dBug($this->archFileName);
$this->arch=new PharData($this->archFileName,0,$this->archiveFormat);
//$archOpeningRes=$this->arch->open($this->archFileName,ZIPARCHIVE::OVERWRITE|ZIPARCHIVE::CREATE|ZIPARCHIVE::CHECKCONS);
//if($archOpeningRes !== TRUE)throw new Exception('Cannot create archive : '.$this->arch->getStatusString());
//unset($archOpeningRes);
//new dBug($this->arch);
//new dBug($this->arch);
$this->comment=[];
$this->comment['timeStarted']=[$time,date('g:i:s A D d.m.y',$time)];
$time=time();
$this->comment['timePrepared']=[$time,date('g:i:s A D d.m.y',$time)];
//! calling plugins
static::makeBackups();
$this->comment['timeBackuped']=[$time,date('g:i:s A D d.m.y',$time)];
$this->arch->setMetadata(json_encode($this->comment,JSON_PRETTY_PRINT));
//$this->arch->setArchiveComment($this->comment);
unset($this->comment);
static::backupIndex();
if($this->archiveFormat==Phar::ZIP){
$this->arch->compressFiles($this->archiveCompression);
}else{
$this->arch->compress($this->archiveCompression);
}
$this->arch=null;
if($this->archiveFormat!=Phar::ZIP){
unlink($this->archFileName);
$this->archFileName.=$this->archiveFileExtension->suffix;
}
echo 'saving backup archive...<br/>';
static::save();
echo 'backup archive saved<br/>';
echo '<hr color="magenta"/>';
}
/*!
prepares backups with backupers' method prepareForBackup
*/
function prepareForBackup(){
if(empty($this->plugins->backup))return;
foreach($this->plugins->backup as $name=>&$backuper){
try{
$backuper->prepareForBackup($this->index->base);
echo 'preparing backuping plugin '.$name." <font color='green'>succeed</font>\n<br/>";
}catch(Exception $err){
echo 'preparing backuping plugin '.$name.' <font color="red">FAILED</font>: '.$err."\n<br/>";
new dBug($err);
}
}
}
/*!
make backups with backupers
*/
function makeBackups(){
if(empty($this->plugins->backup))return;
foreach($this->plugins->backup as $name=>&$backuper){
$this->comment[$name]=[];
try{
$res=$backuper->makeBackup($this->arch);
if(isset($res)){
if(isset($res['comment']))$this->comment[$name]=$res['comment'];
}
$this->comment[$name]['state']='OK';
echo 'backuping plugin '.$name." <font color='green'>succeed</font>\n<br/>";
}catch(Exception $err){
echo $name.' backuping plugin '.$name.' <font color="red">FAILED</font>: '.$err->getMessage()."\n<br/>";
$this->comment[$name]['state']='FAILED: '.$err->getMessage();
new dBug($err);
}
}
}
/*!
uploads the archive to different servers and services and deletes it from local disk
if no servers were specified or none of the uploads succeeded the zip file will not be deleted
*/
function save(){
if(empty($this->plugins->upload))return;
$uploadsFailed=0;
$uploaders=0;
foreach($this->plugins->upload as $uplnm=>&$uploader){
$uploaders++;
try{
$uploader->upload($this->archFileName,$this->archFileShortName);
}catch(Exception $err){
echo $uplnm.' uploading <font color="red">FAILED</font>: '.get_class($err).':'.$err->getCode().':'.$err->getMessage()."\n<br/>";
new dBug($err);
$uploadsFailed++;
}
echo $uplnm.' uploading <font color="green">succeed</font>'."\n<br/>";
}
if($uploadsFailed!=$uploaders&&$uploaders!=0){
unlink($this->archFileName);
}
//parent::save();
}
}
Backuper::$backupsDir=__DIR__;
?>