1 |
cb7d18d5
|
Renato Botelho
|
#!/usr/local/bin/php-cgi -q
|
2 |
7d7ce752
|
jim-p
|
<?php
|
3 |
ac24dc24
|
Renato Botelho
|
/*
|
4 |
|
|
* rc.restore_config_backup
|
5 |
|
|
*
|
6 |
|
|
* part of pfSense (https://www.pfsense.org)
|
7 |
b8f91b7c
|
Luiz Souza
|
* Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
|
8 |
ac24dc24
|
Renato Botelho
|
* All rights reserved.
|
9 |
|
|
*
|
10 |
b12ea3fb
|
Renato Botelho
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
11 |
|
|
* you may not use this file except in compliance with the License.
|
12 |
|
|
* You may obtain a copy of the License at
|
13 |
ac24dc24
|
Renato Botelho
|
*
|
14 |
b12ea3fb
|
Renato Botelho
|
* http://www.apache.org/licenses/LICENSE-2.0
|
15 |
ac24dc24
|
Renato Botelho
|
*
|
16 |
b12ea3fb
|
Renato Botelho
|
* Unless required by applicable law or agreed to in writing, software
|
17 |
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
18 |
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
19 |
|
|
* See the License for the specific language governing permissions and
|
20 |
|
|
* limitations under the License.
|
21 |
ac24dc24
|
Renato Botelho
|
*/
|
22 |
|
|
|
23 |
7d7ce752
|
jim-p
|
require_once('config.inc');
|
24 |
|
|
|
25 |
|
|
cleanup_backupcache();
|
26 |
|
|
$confvers = get_backups();
|
27 |
|
|
unset($confvers['versions']);
|
28 |
|
|
|
29 |
|
|
$fp = fopen('php://stdin', 'r');
|
30 |
|
|
|
31 |
|
|
function print_backup_info($backup_info, $number) {
|
32 |
086cf944
|
Phil Davis
|
if ($backup_info['time'] != 0) {
|
33 |
7d7ce752
|
jim-p
|
$date = date(gettext("n/j/y H:i:s"), $backup_info['time']);
|
34 |
e173dd74
|
Phil Davis
|
} else {
|
35 |
7d7ce752
|
jim-p
|
$date = gettext("Unknown");
|
36 |
e173dd74
|
Phil Davis
|
}
|
37 |
7d7ce752
|
jim-p
|
|
38 |
|
|
list($page, $reason) = explode(": ", $backup_info['description'], 2);
|
39 |
|
|
if (empty($reason)) {
|
40 |
|
|
$reason = $page;
|
41 |
|
|
$page = gettext("Unknown Page");
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
echo sprintf("%02d", $number) . ". {$date}\tv{$backup_info['version']}\t{$page}\n";
|
45 |
|
|
if ($reason) {
|
46 |
|
|
echo " {$reason}\n";
|
47 |
|
|
}
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
function list_backups($which="all", $return=false) {
|
51 |
|
|
global $confvers;
|
52 |
|
|
|
53 |
|
|
if (count($confvers) == 0) {
|
54 |
|
|
echo gettext("No backups found in the configuration history.");
|
55 |
|
|
return;
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
for ($c = count($confvers)-1; $c >= 0; $c--) {
|
59 |
e173dd74
|
Phil Davis
|
if (is_numeric($which) && ($c != $which)) {
|
60 |
7d7ce752
|
jim-p
|
continue;
|
61 |
e173dd74
|
Phil Davis
|
}
|
62 |
7d7ce752
|
jim-p
|
print_backup_info($confvers[$c], $c+1);
|
63 |
|
|
echo "\n";
|
64 |
|
|
}
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
function choose_backup() {
|
68 |
|
|
global $fp, $confvers;
|
69 |
|
|
if (count($confvers) == 0) {
|
70 |
|
|
echo gettext("No backups found in the configuration history.");
|
71 |
|
|
return -1;
|
72 |
|
|
}
|
73 |
|
|
echo gettext("Which configuration would you like to restore?") . "\n";
|
74 |
|
|
echo " 1-" . count($confvers) . " : ";
|
75 |
|
|
$number = strtoupper(chop(fgets($fp)));
|
76 |
d292bd8d
|
jim-p
|
if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
|
77 |
7d7ce752
|
jim-p
|
return $number;
|
78 |
|
|
} else {
|
79 |
|
|
echo gettext("That is not a valid backup number.\n");
|
80 |
|
|
return -1;
|
81 |
|
|
}
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
function restore_history_backup($number) {
|
85 |
|
|
global $g, $fp, $confvers;
|
86 |
d292bd8d
|
jim-p
|
if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
|
87 |
7d7ce752
|
jim-p
|
$realnumber = $number - 1;
|
88 |
|
|
echo "\n" . gettext("Is this the backup you wish to restore?") . "\n";
|
89 |
|
|
list_backups($realnumber);
|
90 |
|
|
$thisbackup = $confvers[$realnumber];
|
91 |
|
|
echo gettext("Y/N?") . " : ";
|
92 |
|
|
$confirm = strtoupper(chop(fgets($fp)));
|
93 |
|
|
if ($confirm == gettext("Y")) {
|
94 |
e173dd74
|
Phil Davis
|
if (config_restore($g['conf_path'] . '/backup/config-' . $thisbackup['time'] . '.xml') == 0) {
|
95 |
7d7ce752
|
jim-p
|
echo "\n";
|
96 |
|
|
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']);
|
97 |
45e4510b
|
jim-p
|
echo "\n" . gettext("You may need to reboot the firewall or restart services before the restored configuration is fully active.") . "\n\n";
|
98 |
7d7ce752
|
jim-p
|
} else {
|
99 |
|
|
echo gettext("Unable to revert to the selected configuration.") . "\n";
|
100 |
|
|
}
|
101 |
|
|
} else {
|
102 |
e173dd74
|
Phil Davis
|
echo gettext("Restore cancelled.") . "\n";
|
103 |
7d7ce752
|
jim-p
|
}
|
104 |
|
|
} else {
|
105 |
e173dd74
|
Phil Davis
|
echo gettext("Restore cancelled due to invalid input.") . "\n";
|
106 |
7d7ce752
|
jim-p
|
}
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
while (true) {
|
110 |
|
|
|
111 |
|
|
echo "\n";
|
112 |
|
|
echo gettext("Restore Backup from Configuration History") . "\n\n";
|
113 |
|
|
echo "1) " . gettext("List Backups") . "\n";
|
114 |
|
|
echo "2) " . gettext("Restore Backup") . "\n";
|
115 |
|
|
echo "Q) " . gettext("Quit") . "\n";
|
116 |
|
|
echo "\n\n";
|
117 |
|
|
echo gettext("Please select an option to continue") . ": ";
|
118 |
|
|
|
119 |
|
|
$command = strtolower(chop(fgets($fp)));
|
120 |
|
|
|
121 |
|
|
// Make sure we can detect a foreign language "quit" command.
|
122 |
086cf944
|
Phil Davis
|
if (strtolower($command) == gettext("quit")) {
|
123 |
7d7ce752
|
jim-p
|
$command = "quit";
|
124 |
086cf944
|
Phil Davis
|
}
|
125 |
7d7ce752
|
jim-p
|
|
126 |
|
|
switch ($command) {
|
127 |
|
|
case "q":
|
128 |
|
|
case "quit":
|
129 |
|
|
echo "\n";
|
130 |
|
|
fclose($fp);
|
131 |
|
|
die;
|
132 |
|
|
break;
|
133 |
|
|
case "1":
|
134 |
|
|
list_backups();
|
135 |
|
|
break;
|
136 |
|
|
case "2":
|
137 |
|
|
$number = choose_backup();
|
138 |
|
|
restore_history_backup($number);
|
139 |
|
|
fclose($fp);
|
140 |
|
|
die;
|
141 |
|
|
break;
|
142 |
|
|
}
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
fclose($fp);
|
146 |
|
|
die;
|
147 |
cb7d18d5
|
Renato Botelho
|
?>
|