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 |
38809d47
|
Renato Botelho do Couto
|
* Copyright (c) 2004-2013 BSD Perimeter
|
8 |
|
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
9 |
8f2f85c3
|
Luiz Otavio O Souza
|
* Copyright (c) 2014-2022 Rubicon Communications, LLC (Netgate)
|
10 |
ac24dc24
|
Renato Botelho
|
* All rights reserved.
|
11 |
|
|
*
|
12 |
b12ea3fb
|
Renato Botelho
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
13 |
|
|
* you may not use this file except in compliance with the License.
|
14 |
|
|
* You may obtain a copy of the License at
|
15 |
ac24dc24
|
Renato Botelho
|
*
|
16 |
b12ea3fb
|
Renato Botelho
|
* http://www.apache.org/licenses/LICENSE-2.0
|
17 |
ac24dc24
|
Renato Botelho
|
*
|
18 |
b12ea3fb
|
Renato Botelho
|
* Unless required by applicable law or agreed to in writing, software
|
19 |
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
20 |
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
21 |
|
|
* See the License for the specific language governing permissions and
|
22 |
|
|
* limitations under the License.
|
23 |
ac24dc24
|
Renato Botelho
|
*/
|
24 |
|
|
|
25 |
7d7ce752
|
jim-p
|
require_once('config.inc');
|
26 |
|
|
|
27 |
|
|
cleanup_backupcache();
|
28 |
|
|
$confvers = get_backups();
|
29 |
|
|
unset($confvers['versions']);
|
30 |
|
|
|
31 |
|
|
$fp = fopen('php://stdin', 'r');
|
32 |
|
|
|
33 |
|
|
function list_backups($which="all", $return=false) {
|
34 |
|
|
global $confvers;
|
35 |
|
|
|
36 |
|
|
if (count($confvers) == 0) {
|
37 |
|
|
echo gettext("No backups found in the configuration history.");
|
38 |
|
|
return;
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
for ($c = count($confvers)-1; $c >= 0; $c--) {
|
42 |
e173dd74
|
Phil Davis
|
if (is_numeric($which) && ($c != $which)) {
|
43 |
7d7ce752
|
jim-p
|
continue;
|
44 |
e173dd74
|
Phil Davis
|
}
|
45 |
db95baf1
|
Viktor G
|
echo backup_info($confvers[$c], $c+1);
|
46 |
7d7ce752
|
jim-p
|
echo "\n";
|
47 |
|
|
}
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
function choose_backup() {
|
51 |
|
|
global $fp, $confvers;
|
52 |
|
|
if (count($confvers) == 0) {
|
53 |
|
|
echo gettext("No backups found in the configuration history.");
|
54 |
|
|
return -1;
|
55 |
|
|
}
|
56 |
|
|
echo gettext("Which configuration would you like to restore?") . "\n";
|
57 |
|
|
echo " 1-" . count($confvers) . " : ";
|
58 |
|
|
$number = strtoupper(chop(fgets($fp)));
|
59 |
d292bd8d
|
jim-p
|
if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
|
60 |
7d7ce752
|
jim-p
|
return $number;
|
61 |
|
|
} else {
|
62 |
|
|
echo gettext("That is not a valid backup number.\n");
|
63 |
|
|
return -1;
|
64 |
|
|
}
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
function restore_history_backup($number) {
|
68 |
|
|
global $g, $fp, $confvers;
|
69 |
d292bd8d
|
jim-p
|
if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
|
70 |
7d7ce752
|
jim-p
|
$realnumber = $number - 1;
|
71 |
|
|
echo "\n" . gettext("Is this the backup you wish to restore?") . "\n";
|
72 |
|
|
list_backups($realnumber);
|
73 |
|
|
$thisbackup = $confvers[$realnumber];
|
74 |
|
|
echo gettext("Y/N?") . " : ";
|
75 |
|
|
$confirm = strtoupper(chop(fgets($fp)));
|
76 |
|
|
if ($confirm == gettext("Y")) {
|
77 |
e173dd74
|
Phil Davis
|
if (config_restore($g['conf_path'] . '/backup/config-' . $thisbackup['time'] . '.xml') == 0) {
|
78 |
7d7ce752
|
jim-p
|
echo "\n";
|
79 |
|
|
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']);
|
80 |
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";
|
81 |
772e14a2
|
Viktor G
|
if (file_exists("{$g['conf_path']}/trigger_initial_wizard")) {
|
82 |
|
|
/* restoring from a backup history means that the assignment is OK,
|
83 |
|
|
* interfaces reassignment may only be need on restore from the WebGUI */
|
84 |
|
|
touch("{$g['conf_path']}/assign_complete");
|
85 |
|
|
}
|
86 |
7d7ce752
|
jim-p
|
} else {
|
87 |
|
|
echo gettext("Unable to revert to the selected configuration.") . "\n";
|
88 |
|
|
}
|
89 |
|
|
} else {
|
90 |
e173dd74
|
Phil Davis
|
echo gettext("Restore cancelled.") . "\n";
|
91 |
7d7ce752
|
jim-p
|
}
|
92 |
|
|
} else {
|
93 |
e173dd74
|
Phil Davis
|
echo gettext("Restore cancelled due to invalid input.") . "\n";
|
94 |
7d7ce752
|
jim-p
|
}
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
while (true) {
|
98 |
|
|
|
99 |
|
|
echo "\n";
|
100 |
|
|
echo gettext("Restore Backup from Configuration History") . "\n\n";
|
101 |
|
|
echo "1) " . gettext("List Backups") . "\n";
|
102 |
|
|
echo "2) " . gettext("Restore Backup") . "\n";
|
103 |
|
|
echo "Q) " . gettext("Quit") . "\n";
|
104 |
|
|
echo "\n\n";
|
105 |
|
|
echo gettext("Please select an option to continue") . ": ";
|
106 |
|
|
|
107 |
|
|
$command = strtolower(chop(fgets($fp)));
|
108 |
|
|
|
109 |
|
|
// Make sure we can detect a foreign language "quit" command.
|
110 |
086cf944
|
Phil Davis
|
if (strtolower($command) == gettext("quit")) {
|
111 |
7d7ce752
|
jim-p
|
$command = "quit";
|
112 |
086cf944
|
Phil Davis
|
}
|
113 |
7d7ce752
|
jim-p
|
|
114 |
|
|
switch ($command) {
|
115 |
|
|
case "q":
|
116 |
|
|
case "quit":
|
117 |
|
|
echo "\n";
|
118 |
|
|
fclose($fp);
|
119 |
|
|
die;
|
120 |
|
|
break;
|
121 |
|
|
case "1":
|
122 |
|
|
list_backups();
|
123 |
|
|
break;
|
124 |
|
|
case "2":
|
125 |
|
|
$number = choose_backup();
|
126 |
|
|
restore_history_backup($number);
|
127 |
|
|
fclose($fp);
|
128 |
|
|
die;
|
129 |
|
|
break;
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
fclose($fp);
|
134 |
|
|
die;
|
135 |
cb7d18d5
|
Renato Botelho
|
?>
|