F5 scheduled backup script
I needed an automatic backup solution to backup LTM's and GTM's devices in my environment. The below scripts would generate UCS, scf files and would then transfer the files to FTP server.
Create 2 files dailybackup.sh and monthlybackup.sh
Copy the below code into those files. Daily backup will be send to the FTP server and I needed monthly backup which runs once every month to be on the device. The only change that needs to be done here is FTP SERVER IP, FTP USER and FTP PASSWORD and the beginning of the script and it should work fine.
Daily – Backup files sent to FTP server
Monthly – Backup files on the F5 devices @ /var/local/usc and /var/local/scf folders
Daily backup script –
#!/bin/bash#/home/root/dailyf5backup.sh
HOST="<
DIR="/" # FTP Directory
#Create current date/time stamp variable
DATETIME=$( date '+%Y-%m-%d_%H-%M' )
#Create hostname variable
HOSTNAME=$(uname -n| cut -f 1 -d".")
UCS_FILENAME="${DATETIME}_$HOSTNAME"
SCF_FILENAME="${DATETIME}_$HOSTNAME"
#Delete files that are older than 10 days
find /var/tmp/*.tar -mtime +10 -type f -delete
find /var/tmp/*.scf -mtime +10 -type f -delete
find /var/tmp/*.ucs -mtime +10 -type f -delete
logger -p local0.info -t BIGIP configuration backup started.
cd /config
tmsh save /sys config
tmsh save /sys ucs /var/tmp/$UCS_FILENAME.ucs
tmsh save sys config file /var/tmp/$SCF_FILENAME.scf
logger -p local0.info -t BIGIP configuration upload to $HOST started.
FILE=/var/tmp/$hostname.ucs
TONAME=$hostname-$DATE.ucs
ftp -inv $HOST<
bin
lcd /var/tmp
cd $DIR
put $UCS_FILENAME.ucs
put $SCF_FILENAME.scf
put *.tar
quit
END_SCRIPT
logger -p local0.info -t BIGIP configuration upload to $HOST completed.
logger -p local0.info -t BIGIP configuration backup completed.
exit 0
Monthly backup script -
#!/bin/bash#/home/root/monthlyf5backup.sh
#
#UCS Files will be saved to /var/local/ucs/
#SCF Files will be saved to /var/local/scf/
#Create current date/time stamp variable
DATETIME=$( date '+%Y-%m-%d_%H-%M' )
#Create hostname variable
HOSTNAME=$(uname -n| cut -f 1 -d".")
#Uncomment the following line for troubleshooting
#echo $DATETIME
#Create filename variable
UCS_FILENAME="${DATETIME}_$HOSTNAME"
#Uncomment the following line for troubleshooting
#echo $UCS_FILENAME
#Save the system config before saving UCS and SCF files.
tmsh save /sys config
#Create a UCS archive with the filename specified above
#The file extension will be .ucs will be available in the GUI
tmsh save sys ucs "${UCS_FILENAME}"
#Also create an SCF file with the same filename
#The file extension will be .scf won't be available in the GUI
tmsh save sys config file "${UCS_FILENAME}.scf"
#If you don’t delete these files /var will become full fairly quickly
#Change the +31 value (10 days) to whatever suits you
#find /var/local/ucs/*.ucs -mtime +10 -type f -delete
#find /var/local/scf/*.scf -mtime +10 -type f -delete\
#find /var/local/scf/*.scf.tar -mtime +10 -type f -delete
#EOF
To make the above scripts executable run the below command –
Chmod u+x /home/root/backup.sh ( I shored my scripts @ /home/root
..and finally both the scripts to the cronjob daily to run every day, monthly to run on first of every month.
crontab -e
type: crontab -e and add the following line, as the bigip is using vim as editor for crontab, you have just to type i for insert first. After inserting the line below just hit Escape and type :wq for write and quit
@daily /home/root/dailyf5backup.sh
@monthly /home/root/maonthlyf5.backup.sh
One thing to remember is when the BigIP devices are updated with newer versions of firmware, the copy config does not move these backup scripts so you have to do this all over again or store these scripts in /shared/tmp folder.
Enjoy...!
No comments:
Post a Comment