Automation

last person joined: 3 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Creating a monthly backup of the configuration

    Posted 10-11-2011 03:08

    Hey all, I want to write an event script to create a montly backup of the configuration, but in set format as in the normal format. To test the script, I wrote it as an op script. I will convert it later to an event script.

     

    version 1.0;
    ns junos = "http://xml.juniper.net/junos/*/junos";
    ns xnm   = "http://xml.juniper.net/xnm/1.1/xnm";
    ns jcs   = "http://xml.juniper.net/junos/commit-scripts/1.0";
    import "../import/junos.xsl";
    
    match / {
      <op-script-results> {
        /*
         * Define the filenames for both backups based on the current time.
         */
        var $modified-time = translate($localtime," ","-");
        var $filename-xml = "backup-" _ $modified-time _ "-xml.txt";
        var $filename-set = "backup-" _ $modified-time _ "-set.txt";
    
        /*
         * Create two copies of the current configuration, one in XML format and one
         * in set format.
         */
        var $show-rpc-xml = {
          <command> "show configuration | save /var/tmp/" _ $filename-xml;
        }
        var $results-show-xml = jcs:invoke($show-rpc-xml);
        var $show-rpc-set = {
          <command> "show configuration | display set | save /var/tmp/" _ $filename-set;
        }
        var $results-show-set = jcs:invoke($show-rpc-set);
    
        /*
         * Copy both files using the <file-copy> API Element. This is the XML API
         * form of the "file copy" CLI command.
         */
        var $file-copy-xml-rpc = <file-copy> {
          <source> "/var/tmp/" _ $filename-xml;
          <destination> "ftp://users:pass@server/Backups/FW1/" _ $filename-xml;
        }
        var $results-xml = jcs:invoke($file-copy-xml-rpc);
    
        var $file-copy-set-rpc = <file-copy> {
          <source> "/var/tmp/" _ $filename-set;
          <destination> "ftp://users:pass@server/Backups/FW1/" _ $filename-set;
        }
        var $results-set = jcs:invoke($file-copy-set-rpc);
    
        /*
         * Report any errors or success.
         */
        if ($results-set/..//xnm:error || $results-xml/..//xnm:error) {
          for-each($results-set/..//xnm:error) {
            expr jcs:syslog("external.error","File Copy Error: ",message);
          }
          for-each($results-xml/..//xnm:error){
            expr jcs:syslog("external.error","File Copy Error: ",message);
          }
        } else {
          expr jcs:syslog("external.info","Monthly backup made.");
        }
      }
    }

     The error/succes lines of code don't work so I should remove them (or make them work). I copied those lines off an example I found on this website.

     

    The code works if I don't add the "save /var/tmp/filename" at the end of the show configuration command. I know there exists an get-configuration RPC but I can't how to make it display the configuration in set format.

     

    Any ideas or help?

     

    Many thanks in advance.



  • 2.  RE: Creating a monthly backup of the configuration
    Best Answer

     
    Posted 10-11-2011 03:32

    Hi Tommie,

    as you wrote, you can use the get-configuration rpc, adding the format=text attribute:

    <get-configuration format="text">

     

     

    Another (perhaps easier) way to get a backup of the configuration database (and also of the output of any useful operational command) is through an event-policy and a time-based event.

     

    You can configure your device in order to generate an event every month:

    [edit event-options]
    event-options { generate-event { every-month time-interval 2592000; } }

     Then you can write an event-policy which will match the event, will launch the command "show configuration | no-more", and will save the output into a file, appending a timestamp to the saved file:

    [edit event-options]
    policy save-configuration-every-month {
    events every-month;
    then {
    execute-commands {
    commands {
    "show configuration | no-more";
    }
    output-filename config_backup;
    output-format text;
    }}}

     This is just an hint, I hope it helps!

    Mattia

     



  • 3.  RE: Creating a monthly backup of the configuration

    Posted 10-11-2011 05:42

    Hey Mattia

     

    Many thanks for your help in trying to find the best solution for my problem! The policy seems a lot more elegant and simple than writing a script, so I'm now testing that solution. It would certainly be a lot easyer to document and explain these policies to my colleagues than have them writing similar scripts.

     

    I however encounter the following issue when I try to enter your code:

     

    {primary:node0}[edit event-options policy save-config-every-month then execute-commands]
    user@FIREWALL# show
    ##
    ## Warning: 'destination' statement must also be included
    ##
    output-filename config-backup;
    output-format text;
    ## Warning: missing mandatory statement(s): 'commands'

    It warns me that I need to include a destination also, but when I add this, it complains about the destination I entered:

     

    {primary:node0}[edit event-options]
    user@FIREWALL# show
    generate-event {
        every-month time-interval 2592000;
    }
    policy save-config-every-month {
        events every-month;
        then {
            execute-commands {
                commands {
                    "show configuration | display set | no-more";
                }
                output-filename config-backup;
                ##
                ## Warning: Destination is not defined
                ##
                destination /var/tmp/;
                output-format text;
            }
        }
    }

     

    Any ideas?



  • 4.  RE: Creating a monthly backup of the configuration

     
    Posted 10-11-2011 05:56

    Sorry Tommie, I forgot to include the destination statement into the event-policy.

    First of all, you must define a destination, under the [event-options] hierarchy. The destination can be at a local or remote site, the following code sample defines a local destination (var/tmp):

    destinations {
      LOCAL {
        archive-sites {
         /var/tmp;
         }
       }

     Then you must reference the above defined destination within the event-policy, adding the line "destination LOCAL" under the then statement of the policy.

     

    Now it should work 🙂



  • 5.  RE: Creating a monthly backup of the configuration

    Posted 10-11-2011 08:05

    Thanks Mattia, that did the trick. I used an ftp location as destination and looks like my monthly backups are doing well now.



  • 6.  RE: Creating a monthly backup of the configuration

    Posted 07-07-2014 09:33

    Hi Tommie

     

    How did you set it to send the backups to the ftp server?

     

    Thanks,

     

    Jorge