Skip to main content
MarioZ
New Member
March 18, 2022
Question

Automation -> CLI Script: Use date as Part of filename

  • March 18, 2022
  • 5 replies
  • 19622 views

Hello 
we use automation/cli script to backup the configuration.
How can i use the date in the filename?
Is there a parameter like %%date%%?
So that a backup would look like:
execute backup full-config ftp /Folder/backup_%%date%%.cfg user pwd

thanks for any input.

Regards
Mario

5 replies

bpozdena_FTNT
Staff
Staff
March 18, 2022

Hi Mario,

 

if you followed my advise from your yesterday's question and used Automation Stitch to run the backup script, you can use variables %%log.date%% and %%log.time%% .  The most appropriate triggers would likely be configuration change or schedule

 

Action example:

config system automation-action     edit "sftp_backup"         set action-type cli-script         set script "config global exec backup config sftp /<path>/backup_%%log.date%%_%%log.time%%.cfg <server_IP> <username> <password>"         set accprofile "super_admin"     next end

 

I would highly suggest you to use the GUI wizard for automation stitches if you are not yet familiar with the CLI. 

 

HTH,

Boris

tobannon
New Member
April 28, 2022

Ok, I'm not sure if this is a bug but I've created two automation tasks.  One for capturing the FGT config every night at 12:15a and one that captures changes made during the day and saving them to an FTP server.  Nothing fancy.

 

Run this script on config changes (works as expected):

execute backup config ftp /HOSTNAME/"changes_backup_%%date%%.conf" <IP ADDRESS> <user> <password>

 

Run this script on a schedule (names the file with the %%date%%):

execute backup config ftp /HOSTNAME/"sched_backup_%%date%%.conf" <IP ADDRESS> <user> <password>

 

I've copied and pasted these commands numerous times and even tried on two different FGTs.  Both are running 6.4.8 b1914.  I've looked and cannot find a bug for this issue.  

 

I did even take the %%log.date%% hoping that would work and I get the exact same results in both commands.

 

TIA

bpozdena_FTNT
Staff
Staff
April 29, 2022

Please upgrade to FortiOS 7.0.1 or newer to fix bug 719029 .

 

Alternatively raise support ticket for FortiOS 6.4. 

kamal_g
New Member
May 25, 2023

anyhow this date variable not working .... i tried on 7.0.2, 6.4.8 & 6.2.2, on output file it is giving the file name i.e. changes_backup_%%date%%.conf

if we will execute below scrpit 

execute backup config ftp /HOSTNAME/"changes_backup_%%date%%.conf" <IP ADDRESS> <user> <password>

srajeswaran
Staff
Staff
May 26, 2023

How are you testing the backup? If you are testing the command by directly running the below command on CLI, it won't work. This command will create a filename "backup_%%date%%.conf" on the FTP server.

 

execute backup config ftp backup_%%date%%.conf <server IP> <username> <password>

Please create a automation stitch and include this command as part of the action/cli-script and then you will see the file name "backup_YYYY-MM-DD.conf" format.
If your FTP server is on linux, you can also use %%time%%, time won't work on windows based FTP servers as the windows system won't allow to create filenames with ":".

Creating Automation stitch for backup: https://community.fortinet.com/t5/FortiGate/Technical-Tip-Adding-date-and-time-in-file-name-for-scheduled/ta-p/203847


Once you create the stitch, you may use "test automation stitch" to test the same immediately.

kamal_g
New Member
May 25, 2023

does anyone have the solution guys? or this bug will remain, since 6.4.8 firmware is not very very old but if this is the bug then i don't know when this will be fixed 

srajeswaran
Staff
Staff
May 26, 2023

6.4 reached End of Engineering Support Date (EOES) on 3/31/2023, you may plan for an upgrade to latest versions in 7.0.x or 7.2.x .

Explorer
May 26, 2023

#!/bin/bash

# Get the current date in the desired format date=$(date +"%Y-%m-%d")

# Generate the filename with the date filename="/Folder/backup_${date}.cfg"

# Execute the backup command with the generated filename execute backup full-config ftp "${filename}" user pwd

Explorer
May 26, 2023

The script retrieves the current date using the date command and formats it as "YYYY-MM-DD". It then generates the filename by concatenating the static part ("/Folder/backup_") with the formatted date and the ".cfg" extension. Finally, it executes the backup command with the dynamically generated filename.

rtichkule
Staff
Staff
May 27, 2023

Hello,

 

Please refer below document to configure the automation stitch for configuration backup.

 

https://community.fortinet.com/t5/FortiGate/Technical-Tip-Adding-date-and-time-in-file-name-for-scheduled/ta-p/203847

 

It won't work if you test the command by typing the command directly into the CLI.

This will generate a file name which is in the command line, however in order to achieve your requirement please configure the parameters in the cli of automation stitch then only the file will be having the date and time.