-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsnapshot-once.php
executable file
·264 lines (255 loc) · 9.26 KB
/
rsnapshot-once.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
#!/usr/bin/php
<?php
/*
* rsnapshot-once
* Copyright (C) 2013 Philipp C. Heckel <[email protected]>
*
* Original blog post at:
* http://blog.philippheckel.com/2013/06/28/script-run-rsnapshot-backups-only-once-and-rollback-failed-backups-using-rsnapshot-once/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
$opts = getopt("c:");
// General failure
if (!$opts) {
die(
"Usage:\n"
. " {$argv[0]} [-c cfgfile] (daily|weekly|monthly)\n\n"
."Description:\n"
." rsnapshot-once is a wrapper for rsnapshot to ensure that daily, weekly\n"
." and monthly tasks are run only once in the respective time period, i.e.\n"
." it ensures that 'weekly' backups are executed only once a week,\n"
." regardless how often rsnapshot-timer is called.\n\n"
." rsnapshot-once accepts the same parameters as rsnapshot and uses the\n"
." same config file. It does not need any additional configuration.\n\n"
."Example (crontab):\n"
." # Job to run every hour, rsnapshot-once makes sure it only runs once a day.\n"
." 0 * * * * {$argv[0]} -c /home/user/.rsnapshot/rsnapshot.home.conf daily\n"
);
}
//print_r($opts);
// -c (Config file)
if (!isset($opts['c'])) {
$opts['c'] = "/etc/rsnapshot.conf";
}
else if (isset($opts['c']) && !file_exists($opts['c'])) {
die("Config file {$opts['c']} does not exist.\n");
}
$cfgfile = $opts['c'];
// Read logfile
$logfile = trim(`cat '{$opts['c']}' | grep '^logfile'`);
if (!preg_match('!^logfile\t(.+)$!', $logfile, $m)) {
die("Config option 'logfile' not found in config file.\n");
}
$logfile = $m[1];
logft("## STARTING BACKUP ######################\n");
$xargs = $argv; array_shift($xargs);
logft("\$ ".basename($argv[0])." '".join("' '", $xargs)."'\n");
logft("Config read from: $cfgfile\n");
logft("- logfile = $logfile\n");
// Read snapshot_root
$snapshot_root = trim(`cat '{$opts['c']}' | grep '^snapshot_root'`);
if (!preg_match('!^snapshot_root\t(.+)$!', $snapshot_root, $m)) {
logft("Config option 'snapshot_root' not found in config file. EXITING.\n");
exit;
}
$snapshot_root = $m[1];
logft("- snapshot_root = $snapshot_root\n");
if (!preg_match('!/$!', $snapshot_root)) {
logft("Invalid config. 'snapshot_root' has no trailing slash. EXITING.\n");
logft("## BACKUP ABORTED #######################\n\n");
exit;
}
// Other argument (weekly, daily, monthly)
$jobname = $argv[count($argv)-1];
if (!in_array($jobname, array("daily", "weekly", "monthly"))) {
logft("Jobname must be 'daily', 'weekly' or 'monthly'. EXITING.\n");
logft("## BACKUP ABORTED #######################\n\n");
exit;
}
// Check pid file
$pidfile = "{$snapshot_root}.rsnapshot-once.pid";
logft("Checking rsnapshot-once pidfile at $pidfile ... ");
if (file_exists($pidfile)) {
$pidfilepid = trim(file_get_contents($pidfile));
if (file_exists("/proc/$pidfilepid")) {
logf("Exists. PID $pidfilepid still running. ABORTING.\n");
logft("## BACKUP ABORTED #######################\n\n");
exit;
}
else {
logf("Exists. PID $pidfilepid not running. Script crashed before.\n");
$sorted_backups = glob("{$snapshot_root}$jobname.*");
echo ": ".join($sorted_backups);
exit;
natsort($sorted_backups);
if (count($sorted_backups) == 0) {
logft("No previous backups found. No cleanup necessary.\n");
}
else {
logft("Cleaning up unfinished backup ...\n");
$first_backup = array_shift($sorted_backups);
logft("- Deleting $first_backup ... ");
// Double check before deleting (!)
if (!isset($jobname) || empty($jobname) || !file_exists($first_backup) || strlen($first_backup) < 2 || !preg_match('/^(.+)\.(\d+)$/', $first_backup)) {
logf("Script security issue. EXITING.\n");
logft("## BACKUP ABORTED #######################\n\n");
exit;
}
else {
// Delete!
$slashed_first_backup = addslashes($first_backup);
`rm -rf '$slashed_first_backup'`;
logf("DONE\n");
}
while (count($sorted_backups) > 0) {
$nth_backup = array_shift($sorted_backups);
if (preg_match('/^(.+)\.(\d+)$/', $nth_backup, $m)) {
$n_minus_1th_backup = $m[1].".".($m[2]-1);
logft("- Moving $nth_backup to $n_minus_1th_backup ... ");
rename($nth_backup, $n_minus_1th_backup);
logf("DONE\n");
}
}
}
}
}
else {
logf("Does not exist. Last backup was clean.\n");
}
logft("Checking delays (minimum 15 minutes since startup/wakeup) ...\n");
// Backup delay (uptime/resumetime + 15 minutes)
$uptime_minutes = 0;
$uptime_minutes = strtok(exec("cat /proc/uptime"), ".")/60;
if ($uptime_minutes) {
if ($uptime_minutes < 15) {
logft("- Computer uptime is ".sprintf("%.1f", $uptime_minutes)." minutes. NOT ENOUGH. EXITING.\n");
logft("## BACKUP ABORTED #######################\n\n");
exit;
}
else {
logft("- Computer uptime is ".sprintf("%.1f", $uptime_minutes)." minutes. THAT'S OKAY.\n");
}
}
// Get time of resume
// http://unix.stackexchange.com/questions/22140/determine-time-of-last-suspend-to-ram
$wakeup_minutes = 0;
$wakeup_date = trim(`egrep 'Running hooks for (resume|thaw)' /var/log/pm-suspend.log | tail -n 1 | sed 's/^\(.*\):.*$/\\1/'`);
if (isset($wakeup_date) && $wakeup_date) {
$wakeup_minutes = (time() - intval(`date --date="$wakeup_date" +%s`))/60;
if ($wakeup_minutes) {
if ($wakeup_minutes < 15) {
logft("- Computer resume time is ".sprintf("%.1f", $wakeup_minutes)." minutes. NOT ENOUGH. EXITING.\n");
logft("## BACKUP ABORTED #######################\n\n");
exit;
}
else {
logft("- Computer resume time is ".sprintf("%.1f", $wakeup_minutes)." minutes. THAT'S OKAY.\n");
}
}
}
// Get date of newest folder (e.g. weekly.0, daily.0, monthly.0)
// to figure out if the job needs to run
$newest_backup_folder = "{$snapshot_root}{$jobname}.0";
if (!file_exists($newest_backup_folder)) {
logft("No backup exists for job '$jobname' at '$newest_backup_folder'.\n");
$job_needs_to_run = true;
}
else {
$backuptime = filemtime($newest_backup_folder);
logft("Newest backup for '$jobname' at $newest_backup_folder was at ".date("d/M/Y, H:i:s", $backuptime).".\n");
if ($jobname == "daily") {
$job_needs_to_run = time() - $backuptime > 23*60*60;
$text_between_runs = sprintf("%.1f", (time() - $backuptime)/60/60)." hour(s)";
$text_min_time = "23 hours";
}
else if ($jobname == "weekly") {
$job_needs_to_run = time() - $backuptime > 6.5*24*60*60;
$text_between_runs = sprintf("%.1f", (time() - $backuptime)/60/60/24)." day(s)";
$text_min_time = "6.5 days";
}
else if ($jobname == "monthly") {
$job_needs_to_run = time() - $backuptime > 29*24*60*60;
$text_between_runs = sprintf("%.1f", (time() - $backuptime)/60/60/24)." day(s)";
$text_min_time = "29 days";
}
else {
logft("Error: This should not happen. ERROR.\n");
exit;
}
if (!$job_needs_to_run) {
logft("Job does NOT need to run. Last run is only $text_between_runs ago (min. is $text_min_time). EXITING.\n");
logft("## BACKUP ABORTED #######################\n\n");
exit;
}
else {
logft("Last run is $text_between_runs ago (min. is $text_min_time).\n");
}
}
logft("Writing rsnapshot-once pidfile (PID ".getmypid().") to ".$pidfile.".\n");
file_put_contents($pidfile, getmypid());
logft("NOW RUNNING JOB: ");
array_shift($argv);
$escaped_pidfile = addslashes($pidfile);
$cmd = "rsnapshot '".join("' '", $argv)."' ".'2>&1';
logf("$cmd\n");
$exitcode = -1;
$configerror = false;
$output = array();
exec($cmd, $output, $exitcode);
foreach ($output as $outline) {
logft(" rsnapshot says: $outline\n");
if (preg_match("/rsnapshot encountered an error/", $outline)) {
$configerror = true;
}
}
if ($configerror) {
logft("Exiting rsnapshot-once, because error in rsnapshot run detected.\n");
logft("Removing rsnapshot-once pidfile at ".$pidfile." (CLEAN EXIT).\n");
unlink($pidfile);
logft("## BACKUP ABORTED #######################\n");
exit;
}
// pidfile should NOT exist if exit was clean
if ($exitcode == 1) { // 1 means 'fatal error' in rsnapshot terminology
logft("No clean exit. Backup aborted. Cleanup necessary on next run (DIRTY EXIT).\n");
logft("## BACKUP ABORTED #######################\n");
exit;
}
logft("Removing rsnapshot-once pidfile at ".$pidfile." (CLEAN EXIT).\n");
unlink($pidfile);
logft("Rotating log ...\n");
logrotate();
logft("## BACKUP COMPLETE ######################\n\n");
#### FUNCTIONS #############################################################
// log with time
function logft($s) {
logf("[".date("d/M/Y:H:i:s")."/rsnapshot-once] ".$s);
}
// log without time
function logf($s) {
echo $s;
if (isset($GLOBALS['logfile'])) {
file_put_contents(trim($GLOBALS['logfile']), $s, FILE_APPEND | LOCK_EX);
}
}
// rotate log
function logrotate() {
$logfile = $GLOBALS['logfile'];
if (isset($GLOBALS['logfile'])) {
`tail -n 1000 $logfile > $logfile.tmp`;
`mv $logfile.tmp $logfile`;
}
}
?>