Project

General

Profile

Download (3.88 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -q
2
<?php
3
/*
4
 * rc.restore_config_backup
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2004-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2022 Rubicon Communications, LLC (Netgate)
10
 * All rights reserved.
11
 *
12
 * 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
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * 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
 */
24

    
25
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
		if (is_numeric($which) && ($c != $which)) {
43
			continue;
44
		}
45
		echo backup_info($confvers[$c], $c+1);
46
		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
	if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
60
		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
	if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
70
		$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
			if (config_restore($g['conf_path'] . '/backup/config-' . $thisbackup['time'] . '.xml') == 0) {
78
				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
				echo "\n" . gettext("You may need to reboot the firewall or restart services before the restored configuration is fully active.") . "\n\n";
81
				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
			} else {
87
				echo gettext("Unable to revert to the selected configuration.") . "\n";
88
			}
89
		} else {
90
			echo gettext("Restore cancelled.") . "\n";
91
		}
92
	} else {
93
		echo gettext("Restore cancelled due to invalid input.") . "\n";
94
	}
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
	if (strtolower($command) == gettext("quit")) {
111
		$command = "quit";
112
	}
113

    
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
?>
(74-74/85)