Community discussions

MikroTik App
 
User avatar
TealFrog
just joined
Topic Author
Posts: 23
Joined: Sun Oct 02, 2011 11:56 am

Script to Create and Maintain Multiple Exported/Backup Files

Sat Oct 29, 2011 3:22 am

The following script was written to maintain and keep a specific number of exported and backup files stored on a router. The idea behind the script was to execute the script on a periodic basis creating an exported configuration and backup while maintaining a limited number of these files because of space constraints. The script was created with the idea of maintaining the files in terms of generations. Several versions or generations are kept based on the configured number of generations in conjunction with scheduling the execution. In some situations it is probably best to store backups such as these elsewhere not physically on the router, but this script is for those situations where space is a concern and so is having an available back up associated with the router.

The script is intended to be scheduled to run on a periodic basis. The script will maintain the specified number of backup generations based on the count specified by the "fileCount" variable (by default it is set for three file generations). The script uses the "/system backup save" command and the "/export file" command (root level) to create a backup and an exported configuration. The script maintains two files that store information related to the last "generation" created and saved.

For example the script will create an export and a backup file. The files by default will be one of three export and backup generations, G1-G3 (three export and three backup files). By default when generation three (G3) is reached it will remove generation one (G1). The script will then recreate a new generation starting at generation one (G1) recreating both an export and backup file.

The script has the option of storing the backup and export files in separate directories. Please note the specified directories must be manually created before executing the script as the script does not have a directory creation mechanism. By default the file generations are named in the format similar to "SystemIdentity[Bak|Exp]G#-YYYYMMDD".

For example a backup file might appear as "RB493GTik1BakG1-20111028". The naming format convention can be configured to an extent by modifying the configurable variables as noted in the script. Once executed, to start the generations from scratch, i.e. from generation one (G1), remove the two files (by default) named “SavedCfgExpGen” and “SavedcfgBakGen”.

To implement the script, save the script under ROS. Schedule the script to run periodically using the scheduler. If a different script name is used other than the default given, “SaveConfiguration”, modify the “progName” variable to match the script’s name. Other options can be modified by changing the script’s variables. The various options have been noted as comments in the script. It is recommended that space constraints be considered and that the “fileCount” variable be adjusted as needed.

For systems that have a large system identity name, use a static name by configuring the “bakFilePrefix” and “expFilePrefix” variables. Using external storage may be appropriate where applicable and may save on system NAND wear on systems that use that type of storage.
#
# v1.0 SaveConfiguration  
# Saves exported router configuration and creates a backup creating multiple
# generation files.
# Yet Another Backup & Export Script (YABES)!  
# Developed and tested on ROS 5.7
#
# $progName - Name of this script, should match ROS script name
:local progName "SaveConfiguration";
# $fileCount - The number of export and backup generations to maintain
:local fileCount 3;
# $expFilePrefix - The file name prefix for the export file ( "" = sys identity)
:local expFilePrefix "";
# $bakFilePrefix - The file name prefix for the backup file ( "" = sys identity)
:local bakFilePrefix "";
# $expFileType - Part of the file name to designate exported configuration
:local expFileType "Exp";
# $bakFileType - Part of the file name to designate file is a backup file
:local bakFileType "Bak";
# $expDirectory - Store exports in specific directory 
# (use "" = no directory, "/dirname/" or "dirname/" to specify directory)
:local expDirectory "";
# $bakDirectory - Store backups in specific directory
# (use "" = no directory, "/dirname/" or "dirname/" to specify directory)
:local bakDirectory ""; 
# $expGenFileName - Name of file that keeps track of export generation number
:local expGenFileName "SavedCfgExpGen";
# $bakGenFileName - Name of file that keeps track of backup generation number
:local bakGenFileName "SavedCfgBakGen";
# $blnRemoveOldExports - Boolean "true" or "false" to search and remove
# generation being created
:local blnRemoveOldExports true;
# $blnRemoveOldBackups - Boolean "true" or "false" to search and remove 
# generation being created
:local blnRemoveOldBackups true;
# General program variables (no further modification needed from here down)
:local generation;
:local expFileGen;
:local bakFileGen;
:local expFileName;
:local bakFileName;
:local content;
:local outGenFile;
:local yyyymmdd;
:local mm;
:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct", \
"nov","dec");
:local curDate [ /system clock get date ];
:local curMonth [ :pick $curDate 0 3 ];
:local curDay [ :pick $curDate 4 6 ];
:local curYear [ :pick $curDate 7 11 ];
:put "$progName: Running...";
:log info "$progName: Running...";
:set mm ([ :find $months $curMonth -1 ] + 1);
:if ($mm < 10) do={
    :set curMonth ("0" . [ :tostr $mm ]);
} else={
    :set curMonth [ :tostr $mm ];
}
:set yyyymmdd ($curYear . $curMonth . $curDay);
# Start attempting to figure out file generation
# Use system identity for file name if ""
:if ([ :len [ :tostr $expFilePrefix ] ] < 1) do={
    :set expFilePrefix [ /system identity get name ];
}
:if ([ :len [ :tostr $bakFilePrefix ] ] < 1) do={
    :set bakFilePrefix [ /system identity get name ];
}
:set expFileName ($expFilePrefix . $expFileType);
:set bakFileName ($bakFilePrefix . $bakFileType); 
:local arrGenFiles ($expGenFileName, $bakGenFileName);
:foreach file in $arrGenFiles do={
    :set generation 1;
    :if ([ [ :len [ :tostr [ /file find name=($file . ".txt") ] ] ] ] > 0) do={
        :set content [ /file get [ /file find name=($file . ".txt") ] contents ];
        :if ([ :len $content ] > 0) do={
            :set generation [ :tonum $content ];
        }
        :if (($generation < 1) or ($generation > $fileCount)) do={
            :set generation 1;
        }
    }
    :put ("$progName: Processing generation: $generation file: " . $file . ".txt");
    :log info ("$progName: Processing generation: $generation file: " . $file . ".txt");
    :if ([ :find $expGenFileName $file -1] = 0) do={
        :set outGenFile $expGenFileName;        
        :set expFileGen $generation;
        :if ($blnRemoveOldExports) do={
            :local expFileSearch ($expFileName . "G" . $expFileGen . "-[0-9]{8}\\.rsc");
            :foreach idFile in [ /file find name~"$expFileSearch" ] do={        
                :put "$progName: Removing previous export file $[ /file get $idFile name ].";
                :log info "$progName: Removing previous export file $[ /file get $idFile name ].";
                /file remove $idFile
            }
        }
        :set expFileName ($expFileName . "G" . $expFileGen . "-" . $yyyymmdd);
        :if ( [ :len [ :tostr $expDirectory ] ] > 0 ) do={
            :set expFileName ($expDirectory . $expFileName);
        }
        :put "$progName: Exporting router configuration to $expFileName.rsc";
        :log info "$progName: Exporting router configuration to $expFileName.rsc";
        /export file $expFileName
    } else={
        :set outGenFile $bakGenFileName;        
        :set bakFileGen $generation;
        :if ($blnRemoveOldBackups) do={
            :local bakFileSearch ($bakFileName . "G" . $bakFileGen . "-[0-9]{8}\\.backup");
            :foreach idFile in [ /file find name~"$bakFileSearch" ] do={        
                :put "$progName: Removing previous backup file $[ /file get $idFile name ].";
                :log info "$progName: Removing previous backup file $[ /file get $idFile name ].";
                /file remove $idFile
            }
        }    
        :set bakFileName ($bakFileName. "G" . $bakFileGen . "-" . $yyyymmdd);
        :if ( [ :len [ :tostr $bakDirectory ] ] > 0 ) do={
            :set bakFileName ($bakDirectory . $bakFileName);
        }
        :put "$progName: Creating backup file $bakFileName.backup";
        :log info "$progName: Creating backup file $bakFileName.backup";
        /system backup save name=$bakFileName
    }
    :set generation ($generation + 1);
    :if ($generation > $fileCount) do={
        :set generation 1; 
    }  
    /file print file="$outGenFile";
    :set outGenFile ($outGenFile . ".txt");
    :put "$progName: Storing next file generation: $generation in file: $outGenFile";
    :log info "$progName: Storing next file generation: $generation in file: $outGenFile";
    /delay 2;
    /file set $outGenFile contents="$generation"
}
:put "$progName: Done.";
:log info "$progName: Done.";
Configure the scheduler to run the script periodically. The provided scheduler configuration does so on a daily basis.
/system scheduler
add disabled=no interval=1d name=SaveConfiguration on-event=\
    SaveConfiguration policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
    start-date=oct/16/2011 start-time=05:00:00

Who is online

Users browsing this forum: Majestic-12 [Bot] and 15 guests