0% found this document useful (0 votes)
18 views9 pages

F5 Scipt

The script backs up F5 device configurations daily by creating timestamped filenames, deleting old backups, saving configurations, and uploading the files via SFTP. Key-based authentication is also configured for the SFTP connection.

Uploaded by

Pmp Pmp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

F5 Scipt

The script backs up F5 device configurations daily by creating timestamped filenames, deleting old backups, saving configurations, and uploading the files via SFTP. Key-based authentication is also configured for the SFTP connection.

Uploaded by

Pmp Pmp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

#!

/bin/bash

#/home/root/dailyf5backup.sh

HOST="<>" # sftp

USER="<>" # backup_user

PASS="<>" # testpassword

DIR="/" # /data/NP-F5

#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< user $USER $PASS

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
#!/bin/b#!/bin/bash
#/home/root/dailyf5backup.sh

HOST="<10.84.84.54>" # SFTP Server IP


USER="< backup_user>" # SFTP User
PASS="<testpassword>" # SFTP Password
DIR="/data/NP-F5" # SFTP Directory

#Create current date/time stamp variable


DATETIME=$(date '+%Y-%m-%d_%H-%M')

#Create hostname variable


HOSTNAME=$(uname -n | cut -f 1 -d ".")

# Define filenames
UCS_FILENAME="${DATETIME}_${HOSTNAME}.ucs"
SCF_FILENAME="${DATETIME}_${HOSTNAME}.scf"

#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

# Log backup initiation


logger -p local0.info -t BIGIP "Configuration backup started."

# Backup configurations
cd /config
tmsh save /sys config
tmsh save /sys ucs /var/tmp/$UCS_FILENAME
tmsh save sys config file /var/tmp/$SCF_FILENAME

# Log upload initiation


logger -p local0.info -t BIGIP "Configuration upload to $HOST started."

# Upload files to SFTP server


sftp $USER@$HOST << EOF
put /var/tmp/$UCS_FILENAME
put /var/tmp/$SCF_FILENAME
put /var/tmp/*.tar
quit
EOF

# Log upload completion


logger -p local0.info -t BIGIP "Configuration upload to $HOST completed."

# Log backup completion


logger -p local0.info -t BIGIP "Configuration backup completed."

exit 0ash
#/home/root/dailyf5backup.sh
HOST="<FTP_SERVER_IP>" # SFTP Server IP
USER="<BACKUP_USER>" # SFTP User
PASS="<testpassword>" # SFTP Password
DIR="/data/NP-F5" # SFTP Directory

#Create current date/time stamp variable


DATETIME=$(date '+%Y-%m-%d_%H-%M')

#Create hostname variable


HOSTNAME=$(uname -n | cut -f 1 -d ".")

# Define filenames
UCS_FILENAME="${DATETIME}_${HOSTNAME}.ucs"
SCF_FILENAME="${DATETIME}_${HOSTNAME}.scf"

#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

# Log backup initiation


logger -p local0.info -t BIGIP "Configuration backup started."

# Backup configurations
cd /config
tmsh save /sys config
tmsh save /sys ucs /var/tmp/$UCS_FILENAME
tmsh save sys config file /var/tmp/$SCF_FILENAME

# Log upload initiation


logger -p local0.info -t BIGIP "Configuration upload to $HOST started."

# Upload files to SFTP server


sftp $USER@$HOST << EOF
put /var/tmp/$UCS_FILENAME
put /var/tmp/$SCF_FILENAME
put /var/tmp/*.tar
quit
EOF

# Log upload completion


logger -p local0.info -t BIGIP "Configuration upload to $HOST completed."

# Log backup completion


logger -p local0.info -t BIGIP "Configuration backup completed."

exit 0

Chmod u+x / var/tmp/sftp/backup.sh


/var/tmp/sftp

crontab -e

00 7 * * * . /var/tmp/sftp/backup.sh
tmsh save /sys ucs /var/tmp/sftp/$(/bin/hostname).ucs && scp /var/ucs/$(/bin/hostname).ucs
[email protected]:/root/$(/bin/hostname)-$(date +\%Y-\%m-\%d).ucs

Last login: Fri Feb 23 05:44:40 2024 from 172.16.0.102


[root@F5-C4800-01:/S1-green-P::Active:In Sync] config # ssh [email protected]
[email protected]'s password:
This service allows sftp connections only.
Connection to 10.84.84.54 closed.
[root@F5-C4800-01:/S1-green-P::Active:In Sync] config # cd /var/tmp/sftp
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # ll
total 4
-rw-r--r--. 1 root root 1127 Feb 23 05:55 backup.sh
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # vi backup.sh
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # bash backup.sh
find: '/var/tmp/*.tar': No such file or directory
find: '/var/tmp/*.scf': No such file or directory
Saving running configuration...
/config/bigip.conf
/config/bigip_base.conf
/config/bigip_user.conf
Saving active configuration...
/var/tmp/2024-02-23_13-42_F5-C4800-01.ucs is saved.
Data Input Error: "passphrase" passphrase should be provided or use the no-passphrase
option
[email protected]'s password:
Connected to 10.84.84.54.
sftp> put /var/tmp/2024-02-23_13-42_F5-C4800-01.ucs
Uploading /var/tmp/2024-02-23_13-42_F5-C4800-01.ucs to /data/2024-02-23_13-42_F5-
C4800-01.ucs

sftp> put /var/tmp/2024-02-23_13-42_F5-C4800-01.scf


stat /var/tmp/2024-02-23_13-42_F5-C4800-01.scf: No such file or directory
sftp> put /var/tmp/*.tar
stat /var/tmp/*.tar: No such file or directory
sftp> quit
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:lUi4Uo6y2wFNBcHmxY8W9xMQJwWOyVaynviDXPDBxQQ root@F5-C4800-
01.np.futc-int.gcp.ea.com
The key's randomart image is:
+---[RSA 4096]----+
| .o=E=X=o |
| +o*Xo+.. |
| =.=O*o.o. |
| o =*=o..o |
| +.o= S . |
| ...+ |
| oo.o |
| .. . |
| |
+----[SHA256]-----+
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # ssh-copy-id
[email protected]
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are
already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to
install the new keys
[email protected]'s password:
This service allows sftp connections only.
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # echo "ls -lrt" | sftp -q
[email protected]:/data/NP-F5
[email protected]'s password:
[email protected]'s password:
sftp> ls -lrt
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # echo "ls -lrt" | sftp -q
[email protected]:/data/
[email protected]'s password:
sftp> ls -lrt
drwxr-xr-x 1 backup_user 10001 0 Feb 16 09:57 NP-SRX
drwxr-xr-x 1 backup_user 10001 0 Feb 16 09:58 NP-QFX
drwxr-xr-x 1 backup_user 10001 0 Feb 16 09:58 NP-F5
drwxr-xr-x 1 backup_user 10001 0 Feb 16 09:58 NP-EX
drwxr-xr-x 1 backup_user 10001 0 Feb 16 09:58 NP-PA
Last login: Fri Feb 23 05:44:40 2024 from 172.16.0.102
[root@F5-C4800-01:/S1-green-P::Active:In Sync] config # ssh [email protected]
[email protected]'s password:
This service allows sftp connections only.
Connection to 10.84.84.54 closed.
[root@F5-C4800-01:/S1-green-P::Active:In Sync] config # cd /var/tmp/sftp
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # ll
total 4
-rw-r--r--. 1 root root 1127 Feb 23 05:55 backup.sh
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # vi backup.sh
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # bash backup.sh
find: '/var/tmp/*.tar': No such file or directory
find: '/var/tmp/*.scf': No such file or directory
Saving running configuration...
/config/bigip.conf
/config/bigip_base.conf
/config/bigip_user.conf
Saving active configuration...
/var/tmp/2024-02-23_13-42_F5-C4800-01.ucs is saved.
Data Input Error: "passphrase" passphrase should be provided or use the no-passphrase
option
[email protected]'s password:
Connected to 10.84.84.54.
sftp> put /var/tmp/2024-02-23_13-42_F5-C4800-01.ucs
Uploading /var/tmp/2024-02-23_13-42_F5-C4800-01.ucs to /data/2024-02-23_13-42_F5-
C4800-01.ucs

sftp> put /var/tmp/2024-02-23_13-42_F5-C4800-01.scf


stat /var/tmp/2024-02-23_13-42_F5-C4800-01.scf: No such file or directory
sftp> put /var/tmp/*.tar
stat /var/tmp/*.tar: No such file or directory
sftp> quit
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:lUi4Uo6y2wFNBcHmxY8W9xMQJwWOyVaynviDXPDBxQQ root@F5-C4800-
01.np.futc-int.gcp.ea.com
The key's randomart image is:
+---[RSA 4096]----+
| .o=E=X=o |
| +o*Xo+.. |
| =.=O*o.o. |
| o =*=o..o |
| +.o= S . |
| ...+ |
| oo.o |
| .. . |
| |
+----[SHA256]-----+
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # ssh-copy-id
[email protected]
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are
already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to
install the new keys
[email protected]'s password:
This service allows sftp connections only.
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # echo "ls -lrt" | sftp -q
[email protected]:/data/NP-F5
[email protected]'s password:
[email protected]'s password:
sftp> ls -lrt
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # echo "ls -lrt" | sftp -q
[email protected]:/data/
[email protected]'s password:
sftp> ls -lrt
drwxr-xr-x 1 backup_user 10001 0 Feb 16 09:57 NP-SRX
drwxr-xr-x 1 backup_user 10001 0 Feb 16 09:58 NP-QFX
drwxr-xr-x 1 backup_user 10001 0 Feb 16 09:58 NP-F5
drwxr-xr-x 1 backup_user 10001 0 Feb 16 09:58 NP-EX
drwxr-xr-x 1 backup_user 10001 0 Feb 16 09:58 NP-PA
-rw-r--r-- 1 backup_user 10001 8374055 Feb 23 13:43 2024-02-23_13-42_F5-C4800-
01.ucs
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # vi backup.sh
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # bash backup.sh
find: '/var/tmp/*.tar': No such file or directory
find: '/var/tmp/*.scf': No such file or directory
Saving running configuration...
/config/bigip.conf
/config/bigip_base.conf
/config/bigip_user.conf
Saving active configuration...
/var/tmp/2024-02-23_14-11_F5-C4800-01.ucs is saved.
Data Input Error: "passphrase" passphrase should be provided or use the no-passphrase
option
[email protected]'s password:
Connected to 10.84.84.54.
Changing to: /data/NP-F5
sftp> put /var/tmp/2024-02-23_14-11_F5-C4800-01.ucs
Uploading /var/tmp/2024-02-23_14-11_F5-C4800-01.ucs to /data/NP-F5/2024-02-23_14-
11_F5-C4800-01.ucs

sftp> put /var/tmp/2024-02-23_14-11_F5-C4800-01.scf


stat /var/tmp/2024-02-23_14-11_F5-C4800-01.scf: No such file or directory
sftp> put /var/tmp/*.tar
stat /var/tmp/*.tar: No such file or directory
sftp> quit
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # echo "ls -lrt" | sftp -q
[email protected]:/data/NP-F5
[email protected]'s password:
sftp> ls -lrt
-rw-r--r-- 1 backup_user 10001 8378125 Feb 23 14:12 2024-02-23_14-11_F5-C4800-
01.ucs
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # [root@F5-C4800-01:/S1-green-
P::Active:In Sync] sftp # vi backup.sh
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # bash backup.sh
find: '/var/tmp/*.tar': No such file or directory
find: '/var/tmp/*.scf': No such file or directory
Saving running configuration...
/config/bigip.conf
/config/bigip_base.conf
/config/bigip_user.conf
Saving active configuration...
/var/tmp/2024-02-23_14-11_F5-C4800-01.ucs is saved.
Data Input Error: "passphrase" passphrase should be provided or use the no-passphrase
option
[email protected]'s password:
Connected to 10.84.84.54.
Changing to: /data/NP-F5
sftp> put /var/tmp/2024-02-23_14-11_F5-C4800-01.ucs
Uploading /var/tmp/2024-02-23_14-11_F5-C4800-01.ucs to /data/NP-F5/2024-02-23_14-
11_F5-C4800-01.ucs

sftp> put /var/tmp/2024-02-23_14-11_F5-C4800-01.scf


stat /var/tmp/2024-02-23_14-11_F5-C4800-01.scf: No such file or directory
sftp> put /var/tmp/*.tar
stat /var/tmp/*.tar: No such file or directory
sftp> quit
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp # echo "ls -lrt" | sftp -q
[email protected]:/data/NP-F5
[email protected]'s password:
sftp> ls -lrt
-rw-r--r-- 1 backup_user 10001 8378125 Feb 23 14:12 2024-02-23_14-11_F5-C4800-
01.ucs
[root@F5-C4800-01:/S1-green-P::Active:In Sync] sftp #

You might also like