Project

General

Profile

Download (5.7 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
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions are met:
12
 *
13
 * 1. Redistributions of source code must retain the above copyright notice,
14
 *    this list of conditions and the following disclaimer.
15
 *
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in
18
 *    the documentation and/or other materials provided with the
19
 *    distribution.
20
 *
21
 * 3. All advertising materials mentioning features or use of this software
22
 *    must display the following acknowledgment:
23
 *    "This product includes software developed by the pfSense Project
24
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
25
 *
26
 * 4. The names "pfSense" and "pfSense Project" must not be used to
27
 *    endorse or promote products derived from this software without
28
 *    prior written permission. For written permission, please contact
29
 *    coreteam@pfsense.org.
30
 *
31
 * 5. Products derived from this software may not be called "pfSense"
32
 *    nor may "pfSense" appear in their names without prior written
33
 *    permission of the Electric Sheep Fencing, LLC.
34
 *
35
 * 6. Redistributions of any form whatsoever must retain the following
36
 *    acknowledgment:
37
 *
38
 * "This product includes software developed by the pfSense Project
39
 * for use in the pfSense software distribution (http://www.pfsense.org/).
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
42
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
45
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52
 * OF THE POSSIBILITY OF SUCH DAMAGE.
53
 */
54

    
55
require_once('config.inc');
56

    
57
cleanup_backupcache();
58
$confvers = get_backups();
59
unset($confvers['versions']);
60

    
61
$fp = fopen('php://stdin', 'r');
62

    
63
function print_backup_info($backup_info, $number) {
64
	if ($backup_info['time'] != 0) {
65
		$date = date(gettext("n/j/y H:i:s"), $backup_info['time']);
66
	} else {
67
		$date = gettext("Unknown");
68
	}
69

    
70
	list($page, $reason) = explode(": ", $backup_info['description'], 2);
71
	if (empty($reason)) {
72
		$reason = $page;
73
		$page = gettext("Unknown Page");
74
	}
75

    
76
	echo sprintf("%02d", $number) . ". {$date}\tv{$backup_info['version']}\t{$page}\n";
77
	if ($reason) {
78
		echo "    {$reason}\n";
79
	}
80
}
81

    
82
function list_backups($which="all", $return=false) {
83
	global $confvers;
84

    
85
	if (count($confvers) == 0) {
86
		echo gettext("No backups found in the configuration history.");
87
		return;
88
	}
89

    
90
	for ($c = count($confvers)-1; $c >= 0; $c--) {
91
		if (is_numeric($which) && ($c != $which)) {
92
			continue;
93
		}
94
		print_backup_info($confvers[$c], $c+1);
95
		echo "\n";
96
	}
97
}
98

    
99
function choose_backup() {
100
	global $fp, $confvers;
101
	if (count($confvers) == 0) {
102
		echo gettext("No backups found in the configuration history.");
103
		return -1;
104
	}
105
	echo gettext("Which configuration would you like to restore?") . "\n";
106
	echo " 1-" . count($confvers) . " : ";
107
	$number = strtoupper(chop(fgets($fp)));
108
	if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
109
		return $number;
110
	} else {
111
		echo gettext("That is not a valid backup number.\n");
112
		return -1;
113
	}
114
}
115

    
116
function restore_history_backup($number) {
117
	global $g, $fp, $confvers;
118
	if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
119
		$realnumber = $number - 1;
120
		echo "\n" . gettext("Is this the backup you wish to restore?") . "\n";
121
		list_backups($realnumber);
122
		$thisbackup = $confvers[$realnumber];
123
		echo gettext("Y/N?") . " : ";
124
		$confirm = strtoupper(chop(fgets($fp)));
125
		if ($confirm == gettext("Y")) {
126
			conf_mount_rw();
127
			if (config_restore($g['conf_path'] . '/backup/config-' . $thisbackup['time'] . '.xml') == 0) {
128
				echo "\n";
129
				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']);
130
				echo "\n" . gettext("You may need to reboot the firewall or restart services before the restored configuration is fully active.") . "\n\n";
131
			} else {
132
				echo gettext("Unable to revert to the selected configuration.") . "\n";
133
			}
134
			conf_mount_ro();
135
		} else {
136
			echo gettext("Restore cancelled.") . "\n";
137
		}
138
	} else {
139
		echo gettext("Restore cancelled due to invalid input.") . "\n";
140
	}
141
}
142

    
143
while (true) {
144

    
145
	echo "\n";
146
	echo gettext("Restore Backup from Configuration History") . "\n\n";
147
	echo "1) " . gettext("List Backups") . "\n";
148
	echo "2) " . gettext("Restore Backup") . "\n";
149
	echo "Q) " . gettext("Quit") . "\n";
150
	echo "\n\n";
151
	echo gettext("Please select an option to continue") . ": ";
152

    
153
	$command = strtolower(chop(fgets($fp)));
154

    
155
	// Make sure we can detect a foreign language "quit" command.
156
	if (strtolower($command) == gettext("quit")) {
157
		$command = "quit";
158
	}
159

    
160
	switch ($command) {
161
		case "q":
162
		case "quit":
163
			echo "\n";
164
			fclose($fp);
165
			die;
166
			break;
167
		case "1":
168
			list_backups();
169
			break;
170
		case "2":
171
			$number = choose_backup();
172
			restore_history_backup($number);
173
			fclose($fp);
174
			die;
175
			break;
176
	}
177
}
178

    
179
fclose($fp);
180
die;
181
?>
(81-81/94)