save and rotate backups

Posted on March 16, 2012

0



This is a really simply and of course my first script to copy firewall backups to a remote server and rotate the backups.
– The script generate log files and list the files that were modified (copied or deleted).
– The first part of the script checks on the remote server the old backups and deletes the files older than 32 days.
– The second part of the script checks on the local system the new backups and copies the files younger then 3 hours.

To understand the operation of the commands and the timing I made one more post here.

#!/bin/sh################################################################################
#
# Define the backup folders (Local and Remote Folders)# Local FolderFW_BACKUPDIR_LOCAL=/var/log/backup/

# Remote Folder

FW_BACKUPDIR_REMOTE=/data/backup/firewall

################################################################################
#
# Route the output to our log file

LOGDIR=/var/log

LOGFILE=`basename $0 | awk -F. ‘{print $1}’`.log
[ -f $LOGDIR/$LOGFILE ] || { touch $LOGDIR/$LOGFILE; }

exec 1>>$LOGDIR/$LOGFILE
exec 2>&1

################################################################################
#
# Start Text with date and hostname to the log File

echo “########## `basename $0` started on `uname -n`: `date` ##########”

################################################################################
#
# Delete old Backups from Remote Host

# Firewall Backups
# older as 32 days

# List the affected files
echo “Deleted Firewall Backups:”
ssh username@remoteserverIP ‘find ‘$FW_BACKUPDIR_REMOTE’ -type f -name *fw*.tgz -ctime +32 -exec ls -lat {} \;’
# Delete them
ssh username@remoteserverIP ‘find ‘$FW_BACKUPDIR_REMOTE’ -type f -name *fw*.tgz -ctime +32 -exec rm -f {} \;’

################################################################################
#
# Copy new Backups to Remote Host

# Firewall Backups
# younger as 3 hours

# List the affected files
echo “Copied Firewall Backups:”
find $FW_BACKUPDIR_LOCAL -type f -name ‘*.tgz’ -cmin -180 -exec ls -lat {} \;
# copy them
find $FW_BACKUPDIR_LOCAL -type f -name ‘*.tgz’ -cmin -180 -exec scp -r -p {} username@remoteserverIP:$FW_BACKUPDIR_REMOTE \;

################################################################################
#
# End Text to the log file

echo “########## `basename $0` completed on `uname -n`: `date` ########”

Advertisement