/
function.php
117 lines (104 loc) · 3.4 KB
/
function.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
<?php
use Ifsnop\Mysqldump\Mysqldump;
use League\Flysystem\Adapter\Ftp;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
function info($message)
{
echo $message;
flush();
ob_flush();
ob_implicit_flush();
}
function get_source($finder, $prefix = '')
{
$results = [];
foreach ($finder as $file) {
$results[$prefix.$file->getRelativePathname()] = $file->getRealpath();
}
return $results;
}
function database_backup($config)
{
info(__FUNCTION__.' start...<br />');
$file = $config['database']['target'].'/'.$config['database']['filename'];
if (is_dir(dirname($file)) === false) {
mkdir(dirname($file), 0777, true);
}
try {
$compress = Mysqldump::NONE;
switch ($config['database']['compress']) {
case 'gzip':
case Mysqldump::GZIP:
$file .= '.gz';
$compress = Mysqldump::GZIP;
break;
}
$dump = new Mysqldump($config['database']['dsn'], $config['database']['username'], $config['database']['password'], [
'single-transaction' => false,
'compress' => $compress,
]);
$dump->start($file);
} catch (\Exception $e) {
info('mysqldump-php error: '.$e->getMessage());
}
info(__FUNCTION__.' finished...<br />');
$remote = $config['database']['remoteFolder'].'/'.basename($file);
return [
$remote => $file,
];
}
function local_backup($config, $files)
{
info(__FUNCTION__.' start...<br />');
$fs = new Filesystem(new Local($config['local']['target']));
info('local backup...<br />');
foreach ($files as $remote => $file) {
// info(sprintf('put: %s to %s<br />', $file, $remote));
try {
$fp = fopen($file, 'r+');
$fs->putStream($remote, $fp);
fclose($fp);
} catch (Exception $e) {
info('<span class="color:red">error: '.$e->getMessage().'</span>');
info('<span class="color:red">file: '.$file.'</span>');
}
}
info(__FUNCTION__.' finished...<br />');
}
function ftp_backup($config, $files)
{
info(__FUNCTION__.' start...<br />');
$fs = new Filesystem(new Ftp($config['ftp']));
foreach ($files as $remote => $file) {
// info(sprintf('put: %s to %s<br />', $file, $remote));
try {
$fp = fopen($file, 'r+');
$fs->putStream($remote, $fp);
fclose($fp);
} catch (Exception $e) {
info('<span class="color:red">error: '.$e->getMessage().'</span>');
info('<span class="color:red">file: '.$file.'</span>');
}
}
info(__FUNCTION__.' finished...<br />');
}
function archive_backup($config, $files)
{
info(__FUNCTION__.' start...<br />');
$fs = new Filesystem(new ZipArchiveAdapter($config['archive']['target']));
foreach ($files as $remote => $file) {
// info(sprintf('put: %s to %s<br />', $file, $remote));
try {
$fp = fopen($file, 'r+');
$fs->putStream($remote, $fp);
fclose($fp);
} catch (Exception $e) {
info('<span class="color:red">error: '.$e->getMessage().'</span>');
info('<span class="color:red">file: '.$file.'</span>');
}
}
$fs->getAdapter()->getArchive()->close();
info(__FUNCTION__.' finished...<br />');
}