forked from GNUWeeb/mail.gnuweeb.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.php
154 lines (128 loc) · 3.97 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
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
<?php
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2021 Ammar Faizi <[email protected]>
*/
$botToken = getenv("TG_BOT_TOKEN");
if (empty($botToken)) {
printf("Cannot find TG_BOT_TOKEN env var\n");
exit(1);
}
$channelId = getenv("TG_CHANNEL_ID");
if (empty($botToken)) {
printf("Cannot find TG_CHANNEL_ID env var\n");
exit(1);
}
require __DIR__."/config.php";
$e = [];
$startTime = microtime(true);
$now = date("Y_m_d__H_i_s");
/*
* Backup gnuweeb database.
*/
$targetFile = "/tmp/gnuweeb_{$now}.sql.gz";
$dumpCmd = "exec mysqldump --default-character-set=utf8mb4 --hex-blob";
$dumpCmd .= " -h".escapeshellarg(DB_HOST);
$dumpCmd .= " -P".escapeshellarg(DB_PORT);
$dumpCmd .= " -u".escapeshellarg(DB_USER);
$dumpCmd .= " -p".escapeshellarg(DB_PASS);
$dumpCmd .= " ".escapeshellarg(DB_NAME);
$dumpCmd .= " | gzip -9c > ".escapeshellarg($targetFile);
$dumpCmd .= " 2>&1";
$e[] = exec_cmd($dumpCmd, $targetFile);
/*
* Backup postfixadmin database.
*/
if (USE_POSTFIX):
$targetFile = "/tmp/postfixadmin_{$now}.sql.gz";
$dumpCmd = "exec mysqldump --default-character-set=utf8mb4 --hex-blob";
$dumpCmd .= " -h".escapeshellarg(PF_DB_HOST);
$dumpCmd .= " -P".escapeshellarg(PF_DB_PORT);
$dumpCmd .= " -u".escapeshellarg(PF_DB_USER);
$dumpCmd .= " -p".escapeshellarg(PF_DB_PASS);
$dumpCmd .= " ".escapeshellarg(PF_DB_NAME);
$dumpCmd .= " | gzip -9c > ".escapeshellarg($targetFile);
$dumpCmd .= " 2>&1";
$e[] = exec_cmd($dumpCmd, $targetFile);
$targetFile = "/tmp/vmail_gnuweeb_org_{$now}.tar.gz";
$dumpCmd = "cd /var/vmail; tar --dereference -c gnuweeb.org | gzip -9c > ".escapeshellarg($targetFile);
$e[] = exec_cmd($dumpCmd, $targetFile);
$targetFile = "/tmp/varlib_mailman_{$now}.tar.gz";
$dumpCmd = "cd /var/lib; tar --dereference -c mailman | gzip -9c > ".escapeshellarg($targetFile);
$e[] = exec_cmd($dumpCmd, $targetFile);
endif;
$endTime = microtime(true);
$totalTime = $totalSize = 0;
$st1 = "<b>[Time taken for each job]</b>\n";
$st2 = "<b>[File size for each job]</b>\n";
foreach ($e as $k => $v) {
$st1 .= "<b>{$v["fname"]}:</b> <code>{$v["time"]}</code> s\n";
$totalTime += $v["time"];
$st2 .= "<b>{$v["fname"]}:</b> <code>{$v["size"]}</code> bytes\n";
$totalSize += $v["size"];
}
$st1 .= "\n<b>Total time:</b> <code>{$totalTime}</code> s\n";
$st1 .= "<b>Wall time:</b> <code>".($endTime - $startTime)."</code> s\n";
$st2 .= "\n<b>Total size:</b> <code>{$totalSize}</code> bytes";
sendMsg($st1);
sendMsg($st2);
sendMsg("==========================");
function exec_cmd(string $dumpCmd, string $targetFile)
{
global $botToken, $channelId;
echo $dumpCmd."\n";
$startTime = microtime(true);
echo shell_exec($dumpCmd);
$size = filesize($targetFile);
$md5sum = md5_file($targetFile);
$sha1sum = sha1_file($targetFile);
$bsfilename = basename($targetFile);
$datetime = date("c");
$caption = <<<CAPTION
<b>Datetime:</b> <code>{$datetime}</code>
<b>File name:</b> <code>{$bsfilename}</code>
<b>File size:</b> <code>{$size}</code> bytes
<b>MD5 sum:</b> <code>{$md5sum}</code>
<b>SHA1 sum:</b> <code>{$sha1sum}</code>
CAPTION;
$ch = curl_init("https://api.telegram.org/bot{$botToken}/sendDocument");
curl_setopt_array($ch,
[
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
"chat_id" => $channelId,
"document" => new \CurlFile($targetFile),
"caption" => $caption,
"parse_mode" => "HTML"
]
]
);
echo curl_exec($ch)."\n";
curl_close($ch);
$endTime = microtime(true);
@unlink($targetFile);
return [
"fname" => $bsfilename,
"size" => $size,
"time" => $endTime - $startTime
];
}
function sendMsg(string $msg)
{
global $botToken, $channelId;
$ch = curl_init("https://api.telegram.org/bot{$botToken}/sendMessage");
curl_setopt_array($ch,
[
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
"chat_id" => $channelId,
"text" => $msg,
"parse_mode" => "HTML"
]
]
);
echo curl_exec($ch)."\n";
curl_close($ch);
}