Backing Up Router Configurations


You need to download all of the active router configurations to see what has changed recently.


The Perl script in Example 1-4 automatically retrieves and stores router configuration files on a nightly basis. By default, it retains these configuration files for 30 days. The script should be run through the Unix cron utility to get the automatic nightly updates, but you can also run it manually if required. No arguments are required or expected.

Example 1-4. backup.pl

# backup.pl -- a script to automatically backup a list of 
# router configuraton files on a nightly basis.
# Set behavior
if (! -d $storage) {mkdir ($storage, 0755)};
if (! -d $prev) {mkdir ($prev, 0755)};
if (! -d $latest) {mkdir ($latest, 0755)};
($sec, $min, $hr, $mday, $mon, $year, @etc) = localtime(time);
$mon++; $year=$year+1900;
$today1=sprintf("%.4d_%.2d_%.2d", $year, $mon, $mday);
system("cp -p $latest/* $prev/");
unlink <$latest/*>;
mkdir ($today, 0755);

open (RTR, "$rtrlist") || die "Can't open $rtrlist file";
open (LOG, ">$workingdir/RESULT") || die "Can't open $workingdir/RESULT file";
print LOG "Router Configuration Backup Report for $year/$mon/$mday
print LOG "======================================================
print LOG "Device Name Status
print LOG "======================================================
while () {
 $snmpset ="/usr/local/bin/snmpset -v1 -c $snmprw -t60 -r2 $rtr";
 unlink $rtrfile;
 open (CFG, ">$rtrfile"); print CFG " ";close CFG;
 chmod 0666, $rtrfile;
 chop ($status=Q$snmpset $oid s $rtr.cfgQ);
 $status=~/.+ = "(.+)".*$/;
 if($1 eq "$rtr.cfg") {
 if( -z "$rtrfile" ) {
 $result="not ok (File empty)";
 unlink $rtrfile; 
 else {
 chmod 0444, $rtrfile;
 system("mv $rtrfile $latest");
 else {
 $result="not ok";
 unlink $rtrfile;

printf LOG ("%-28s %-28s
", $rtr,$result); 

system ("cp -p $latest/*cfg $today");
print "$time
($sec, $min, $hr, $mday, $mon, $year, @etc) = localtime(time-$time);
$mon++; $year=$year+1900;
$rmdir=sprintf("%s/%.4d_%.2d_%.2d",$configs, $year, $mon, $mday);
system ("rm -r -f $storage/$rmdir");


As we mentioned earlier in the chapter, it is extremely important to make regular backup copies of your router configuration files. However, as the size of your network grows, it becomes quite tedious to maintain a useful archive of these backups. This script automates the task of collecting and storing router configuration files on a Unix-based TFTP server.

This script will maintain 30 days worth of configuration files. We have found that this is a reasonable length of time, allowing engineers to recover router configuration files that are up to one month old. However, if you prefer, you can change the $days variable to increase or decrease how long the script will store these files before deleting them. If you increase the length of time that the server must store these files, it will obviously increase the amount of disk space you need to hold the extra configuration files. But router configuration files are generally quite small, so this is usually not a serious problem unless you support thousands of routers.

Before executing this script, you will need to modify a few variables. First, the $workingdir variable should contain the name of the directory that the server will run the script in. Then the $snmprw variable must contain your SNMP read-write community string. Please note that the read-only community string will not allow you to copy a configuration fileit must be the read-write string. The other variable you need to change is $ipaddress, which should contain the IP address of your TFTP server.

The script is written in Perl, and it makes a few system calls out to Bourne Shell commands. The script expects to find the Perl executable in the /usr/local/bin directory. The script is also dependent on NET-SNMP, and it expects to find the executable snmpset in the /usr/local/bin directory as well. If these files are in different locations on your local system, you will need to modify these paths. For more information on Perl or NET-SNMP, please see Appendix A.

Finally, you will need a file called RTR_LIST that contains the list of router names. This file must be in the working directory.

As we mentioned earlier, you should run this backup script should from the Unix cron utility on a nightly basis. This will ensure that you have an up-to-date backup of your configuration files. We recommend launching this script during off hours, since it does generate traffic across your network, as well as a small amount of CPU loading on the routers, although neither of these should be large. Here is an example crontab entry to start the script every night at 1:30AM.

30 1 * * * /home/cisco/bkup/backup.pl

When the script runs, it will create a new directory, called storage, under the working directory. Under this directory, the script will create several subdirectories, including LATEST, PREV, and dated directory names, such as 2003_01_28. The directory LATEST will always contain the most up-to-date router configuration files. And you can find the previous stored version of each router's configuration in the directory called PREV. The dated directories will contain all of the router configuration files that were captured on the date indicated in the directory name.

You can use the Unix diff command to see what changes have occurred on a given router.

Finally, the script will create a nightly status report that it stores in a file called RESULT in the working directory:

Freebsd% cat RESULT
Router Configuration Backup Report for 2003/1/28
Device Name Status
toronto ok 
boston not ok 
test ok 
frame ok 

With slight modification, you can configure the script to email this report to the responsible engineer. However, since each different Unix flavor uses a different mail program, we chose not to include it here in the interest of compatibility. On a Solaris server, for example, you could add the following line to the bottom of the script to mail this report:

system ("/usr/ucb/mail -s "Config Report for $today1" Q/bin/cat $mailQ < $workingdir/RESULT"); 

In this case, you would need to define the variable $mail to be email distribution list for the report. For other Unix or Linux variants, please consult you man pages for more information on your local mail program.

See Also

Recipe 1.2; Appendix A

Router Configuration and File Management

Router Management

User Access and Privilege Levels


IP Routing





Frame Relay

Handling Queuing and Congestion

Tunnels and VPNs

Dial Backup

NTP and Time


Router Interfaces and Media

Simple Network Management Protocol





First Hop Redundancy Protocols

IP Multicast

IP Mobility




Appendix 1. External Software Packages

Appendix 2. IP Precedence, TOS, and DSCP Classifications


show all menu

Cisco IOS Cookbook
Cisco IOS Cookbook (Cookbooks (OReilly))
ISBN: 0596527225
EAN: 2147483647
Year: 2004
Pages: 505
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net