Project

General

Profile

Download (4.08 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-2019 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 print_backup_info($backup_info, $number) {
34
	if ($backup_info['time'] != 0) {
35
		$date = date(gettext("n/j/y H:i:s"), $backup_info['time']);
36
	} else {
37
		$date = gettext("Unknown");
38
	}
39

    
40
	list($page, $reason) = explode(": ", $backup_info['description'], 2);
41
	if (empty($reason)) {
42
		$reason = $page;
43
		$page = gettext("Unknown Page");
44
	}
45

    
46
	echo sprintf("%02d", $number) . ". {$date}\tv{$backup_info['version']}\t{$page}\n";
47
	if ($reason) {
48
		echo "    {$reason}\n";
49
	}
50
}
51

    
52
function list_backups($which="all", $return=false) {
53
	global $confvers;
54

    
55
	if (count($confvers) == 0) {
56
		echo gettext("No backups found in the configuration history.");
57
		return;
58
	}
59

    
60
	for ($c = count($confvers)-1; $c >= 0; $c--) {
61
		if (is_numeric($which) && ($c != $which)) {
62
			continue;
63
		}
64
		print_backup_info($confvers[$c], $c+1);
65
		echo "\n";
66
	}
67
}
68

    
69
function choose_backup() {
70
	global $fp, $confvers;
71
	if (count($confvers) == 0) {
72
		echo gettext("No backups found in the configuration history.");
73
		return -1;
74
	}
75
	echo gettext("Which configuration would you like to restore?") . "\n";
76
	echo " 1-" . count($confvers) . " : ";
77
	$number = strtoupper(chop(fgets($fp)));
78
	if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
79
		return $number;
80
	} else {
81
		echo gettext("That is not a valid backup number.\n");
82
		return -1;
83
	}
84
}
85

    
86
function restore_history_backup($number) {
87
	global $g, $fp, $confvers;
88
	if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
89
		$realnumber = $number - 1;
90
		echo "\n" . gettext("Is this the backup you wish to restore?") . "\n";
91
		list_backups($realnumber);
92
		$thisbackup = $confvers[$realnumber];
93
		echo gettext("Y/N?") . " : ";
94
		$confirm = strtoupper(chop(fgets($fp)));
95
		if ($confirm == gettext("Y")) {
96
			if (config_restore($g['conf_path'] . '/backup/config-' . $thisbackup['time'] . '.xml') == 0) {
97
				echo "\n";
98
				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']);
99
				echo "\n" . gettext("You may need to reboot the firewall or restart services before the restored configuration is fully active.") . "\n\n";
100
			} else {
101
				echo gettext("Unable to revert to the selected configuration.") . "\n";
102
			}
103
		} else {
104
			echo gettext("Restore cancelled.") . "\n";
105
		}
106
	} else {
107
		echo gettext("Restore cancelled due to invalid input.") . "\n";
108
	}
109
}
110

    
111
while (true) {
112

    
113
	echo "\n";
114
	echo gettext("Restore Backup from Configuration History") . "\n\n";
115
	echo "1) " . gettext("List Backups") . "\n";
116
	echo "2) " . gettext("Restore Backup") . "\n";
117
	echo "Q) " . gettext("Quit") . "\n";
118
	echo "\n\n";
119
	echo gettext("Please select an option to continue") . ": ";
120

    
121
	$command = strtolower(chop(fgets($fp)));
122

    
123
	// Make sure we can detect a foreign language "quit" command.
124
	if (strtolower($command) == gettext("quit")) {
125
		$command = "quit";
126
	}
127

    
128
	switch ($command) {
129
		case "q":
130
		case "quit":
131
			echo "\n";
132
			fclose($fp);
133
			die;
134
			break;
135
		case "1":
136
			list_backups();
137
			break;
138
		case "2":
139
			$number = choose_backup();
140
			restore_history_backup($number);
141
			fclose($fp);
142
			die;
143
			break;
144
	}
145
}
146

    
147
fclose($fp);
148
die;
149
?>
(70-70/81)