Project

General

Profile

Download (3.99 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-2016 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * 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
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * 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
 */
22

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

    
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
		if (is_numeric($which) && ($c != $which)) {
60
			continue;
61
		}
62
		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
	if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
77
		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
	if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
87
		$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
			if (config_restore($g['conf_path'] . '/backup/config-' . $thisbackup['time'] . '.xml') == 0) {
95
				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
				echo "\n" . gettext("You may need to reboot the firewall or restart services before the restored configuration is fully active.") . "\n\n";
98
			} else {
99
				echo gettext("Unable to revert to the selected configuration.") . "\n";
100
			}
101
		} else {
102
			echo gettext("Restore cancelled.") . "\n";
103
		}
104
	} else {
105
		echo gettext("Restore cancelled due to invalid input.") . "\n";
106
	}
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
	if (strtolower($command) == gettext("quit")) {
123
		$command = "quit";
124
	}
125

    
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
?>
(68-68/79)