Project

General

Profile

Download (3.32 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -q
2
<?php
3
require_once('config.inc');
4

    
5
cleanup_backupcache();
6
$confvers = get_backups();
7
unset($confvers['versions']);
8

    
9
$fp = fopen('php://stdin', 'r');
10

    
11
function print_backup_info($backup_info, $number) {
12
	if ($backup_info['time'] != 0) {
13
		$date = date(gettext("n/j/y H:i:s"), $backup_info['time']);
14
	} else {
15
		$date = gettext("Unknown");
16
	}
17

    
18
	list($page, $reason) = explode(": ", $backup_info['description'], 2);
19
	if (empty($reason)) {
20
		$reason = $page;
21
		$page = gettext("Unknown Page");
22
	}
23

    
24
	echo sprintf("%02d", $number) . ". {$date}\tv{$backup_info['version']}\t{$page}\n";
25
	if ($reason) {
26
		echo "    {$reason}\n";
27
	}
28
}
29

    
30
function list_backups($which="all", $return=false) {
31
	global $confvers;
32

    
33
	if (count($confvers) == 0) {
34
		echo gettext("No backups found in the configuration history.");
35
		return;
36
	}
37

    
38
	for ($c = count($confvers)-1; $c >= 0; $c--) {
39
		if (is_numeric($which) && ($c != $which)) {
40
			continue;
41
		}
42
		print_backup_info($confvers[$c], $c+1);
43
		echo "\n";
44
	}
45
}
46

    
47
function choose_backup() {
48
	global $fp, $confvers;
49
	if (count($confvers) == 0) {
50
		echo gettext("No backups found in the configuration history.");
51
		return -1;
52
	}
53
	echo gettext("Which configuration would you like to restore?") . "\n";
54
	echo " 1-" . count($confvers) . " : ";
55
	$number = strtoupper(chop(fgets($fp)));
56
	if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
57
		return $number;
58
	} else {
59
		echo gettext("That is not a valid backup number.\n");
60
		return -1;
61
	}
62
}
63

    
64
function restore_history_backup($number) {
65
	global $g, $fp, $confvers;
66
	if (is_numeric($number) && ($number > 0) && ($number <= count($confvers))) {
67
		$realnumber = $number - 1;
68
		echo "\n" . gettext("Is this the backup you wish to restore?") . "\n";
69
		list_backups($realnumber);
70
		$thisbackup = $confvers[$realnumber];
71
		echo gettext("Y/N?") . " : ";
72
		$confirm = strtoupper(chop(fgets($fp)));
73
		if ($confirm == gettext("Y")) {
74
			conf_mount_rw();
75
			if (config_restore($g['conf_path'] . '/backup/config-' . $thisbackup['time'] . '.xml') == 0) {
76
				echo "\n";
77
				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']);
78
				echo "\n" . gettext("You may need to reboot the firewall or restart services before the restored configuration is fully active.") . "\n\n";
79
			} else {
80
				echo gettext("Unable to revert to the selected configuration.") . "\n";
81
			}
82
			conf_mount_ro();
83
		} else {
84
			echo gettext("Restore cancelled.") . "\n";
85
		}
86
	} else {
87
		echo gettext("Restore cancelled due to invalid input.") . "\n";
88
	}
89
}
90

    
91
while (true) {
92

    
93
	echo "\n";
94
	echo gettext("Restore Backup from Configuration History") . "\n\n";
95
	echo "1) " . gettext("List Backups") . "\n";
96
	echo "2) " . gettext("Restore Backup") . "\n";
97
	echo "Q) " . gettext("Quit") . "\n";
98
	echo "\n\n";
99
	echo gettext("Please select an option to continue") . ": ";
100

    
101
	$command = strtolower(chop(fgets($fp)));
102

    
103
	// Make sure we can detect a foreign language "quit" command.
104
	if (strtolower($command) == gettext("quit")) {
105
		$command = "quit";
106
	}
107

    
108
	switch ($command) {
109
		case "q":
110
		case "quit":
111
			echo "\n";
112
			fclose($fp);
113
			die;
114
			break;
115
		case "1":
116
			list_backups();
117
			break;
118
		case "2":
119
			$number = choose_backup();
120
			restore_history_backup($number);
121
			fclose($fp);
122
			die;
123
			break;
124
	}
125
}
126

    
127
fclose($fp);
128
die;
129
?>
(80-80/93)