1
|
#!/usr/local/bin/php -q
|
2
|
<?php
|
3
|
require_once('config.inc');
|
4
|
|
5
|
cleanup_backupcache();
|
6
|
$confvers = get_backups();
|
7
|
unset($confvers['versions']);
|
8
|
|
9
|
$fp = fopen('php://stdin', 'r');
|
10
|
|
11
|
function print_backup_info($backup_info, $number) {
|
12
|
if($backup_info['time'] != 0)
|
13
|
$date = date(gettext("n/j/y H:i:s"), $backup_info['time']);
|
14
|
else
|
15
|
$date = gettext("Unknown");
|
16
|
|
17
|
list($page, $reason) = explode(": ", $backup_info['description'], 2);
|
18
|
if (empty($reason)) {
|
19
|
$reason = $page;
|
20
|
$page = gettext("Unknown Page");
|
21
|
}
|
22
|
|
23
|
echo sprintf("%02d", $number) . ". {$date}\tv{$backup_info['version']}\t{$page}\n";
|
24
|
if ($reason) {
|
25
|
echo " {$reason}\n";
|
26
|
}
|
27
|
}
|
28
|
|
29
|
function list_backups($which="all", $return=false) {
|
30
|
global $confvers;
|
31
|
|
32
|
if (count($confvers) == 0) {
|
33
|
echo gettext("No backups found in the configuration history.");
|
34
|
return;
|
35
|
}
|
36
|
|
37
|
for ($c = count($confvers)-1; $c >= 0; $c--) {
|
38
|
if (is_numeric($which) && ($c != $which))
|
39
|
continue;
|
40
|
print_backup_info($confvers[$c], $c+1);
|
41
|
echo "\n";
|
42
|
}
|
43
|
}
|
44
|
|
45
|
function choose_backup() {
|
46
|
global $fp, $confvers;
|
47
|
if (count($confvers) == 0) {
|
48
|
echo gettext("No backups found in the configuration history.");
|
49
|
return -1;
|
50
|
}
|
51
|
echo gettext("Which configuration would you like to restore?") . "\n";
|
52
|
echo " 1-" . count($confvers) . " : ";
|
53
|
$number = strtoupper(chop(fgets($fp)));
|
54
|
if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
|
55
|
return $number;
|
56
|
} else {
|
57
|
echo gettext("That is not a valid backup number.\n");
|
58
|
return -1;
|
59
|
}
|
60
|
}
|
61
|
|
62
|
function restore_history_backup($number) {
|
63
|
global $g, $fp, $confvers;
|
64
|
if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
|
65
|
$realnumber = $number - 1;
|
66
|
echo "\n" . gettext("Is this the backup you wish to restore?") . "\n";
|
67
|
list_backups($realnumber);
|
68
|
$thisbackup = $confvers[$realnumber];
|
69
|
echo gettext("Y/N?") . " : ";
|
70
|
$confirm = strtoupper(chop(fgets($fp)));
|
71
|
if ($confirm == gettext("Y")) {
|
72
|
conf_mount_rw();
|
73
|
if(config_restore($g['conf_path'] . '/backup/config-' . $thisbackup['time'] . '.xml') == 0) {
|
74
|
echo "\n";
|
75
|
echo sprintf(gettext('Successfully reverted to timestamp %1$s with description "%2$s".'), date(gettext("n/j/y H:i:s"), $thisbackup['time']), $thisbackup['description']);
|
76
|
echo "\n" . gettext("You may need to reboot the firewall or restart services before the restored configuration is fully active.") . "\n\n";
|
77
|
} else {
|
78
|
echo gettext("Unable to revert to the selected configuration.") . "\n";
|
79
|
}
|
80
|
conf_mount_ro();
|
81
|
} else {
|
82
|
echo gettext("Restore canceled.") . "\n";
|
83
|
}
|
84
|
} else {
|
85
|
echo gettext("Restore canceled due to invalid input.") . "\n";
|
86
|
}
|
87
|
}
|
88
|
|
89
|
while (true) {
|
90
|
|
91
|
echo "\n";
|
92
|
echo gettext("Restore Backup from Configuration History") . "\n\n";
|
93
|
echo "1) " . gettext("List Backups") . "\n";
|
94
|
echo "2) " . gettext("Restore Backup") . "\n";
|
95
|
echo "Q) " . gettext("Quit") . "\n";
|
96
|
echo "\n\n";
|
97
|
echo gettext("Please select an option to continue") . ": ";
|
98
|
|
99
|
$command = strtolower(chop(fgets($fp)));
|
100
|
|
101
|
// Make sure we can detect a foreign language "quit" command.
|
102
|
if (strtolower($command) == gettext("quit"))
|
103
|
$command = "quit";
|
104
|
|
105
|
switch ($command) {
|
106
|
case "q":
|
107
|
case "quit":
|
108
|
echo "\n";
|
109
|
fclose($fp);
|
110
|
die;
|
111
|
break;
|
112
|
case "1":
|
113
|
list_backups();
|
114
|
break;
|
115
|
case "2":
|
116
|
$number = choose_backup();
|
117
|
restore_history_backup($number);
|
118
|
fclose($fp);
|
119
|
die;
|
120
|
break;
|
121
|
}
|
122
|
}
|
123
|
|
124
|
fclose($fp);
|
125
|
die;
|
126
|
?>
|