Skip to content

Instantly share code, notes, and snippets.

@wakwanza
Created April 5, 2015 11:57
Show Gist options
  • Save wakwanza/267e93e7d7e0d68ca6a6 to your computer and use it in GitHub Desktop.
Save wakwanza/267e93e7d7e0d68ca6a6 to your computer and use it in GitHub Desktop.
log backup script for nginx and fail2ban
#!/bin/bash
BackupDir=logbackup
HomeDir=/archive/$HOSTNAME
# Set $BackupYear equal to 4 characters of $f starting with the first character, the YYYY
BackupYear=$(date +"%Y")
# Set $BackupMonth equal to 2 characters of $f starting with the fifth character, the MM
BackupMonth=$(date +"%m")
# Test to see if the main backup directory exists if not then create it
if [ ! -d $HomeDir/$BackupDir ]; then
mkdir -p $HomeDir/$BackupDir
fi
# Test to see if the backup directory for the year exists, if not then create it.
if [ ! -d $HomeDir/$BackupDir/$BackupYear ]; then
mkdir -p $HomeDir/$BackupDir/$BackupYear
fi
# Test to see if the backup directory for the month exists, if not then create it.
if [ ! -d $HomeDir/$BackupDir/$BackupYear/$BackupMonth ]; then
mkdir -p $HomeDir/$BackupDir/$BackupYear/$BackupMonth
fi
if [[ `hostname -s` = lb* ]];
then
mv /var/log/nginx/*.gz $HomeDir/$BackupDir/$BackupYear/$BackupMonth/
mv /var/log/fail2ban.log-*.gz $HomeDir/$BackupDir/$BackupYear/$BackupMonth/
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment