How To Setup A Cron Job to Copy Previous Month Apache Log Files To A Backup Directory

Discussion in 'Tips/Tricks/Mods' started by MrCompTech, Dec 8, 2011.

  1. MrCompTech

    MrCompTech New Member

    ISPConfig3 & FC13 - How To Setup A Cron Job to Copy Previous Month Apache Log Files To A Backup Directory

    Desired goal: to maintain an ongoing archive of the http access log files. The standard log file rotation only keeps the last 30 days as part of the log rotation process, log files older than this are deleted. This came about as I wanted to be able to perform offline analysis of the log files after the month had ended, but found out that if I didn't manually move the backup files (*.gz) on the first or second of the month then they would start getting deleted.

    Script:
    #!/bin/bash
    BackupDir=logbackup
    cd ~/log
    for f in *-access.log.gz
    do
    filename=$f
    done
    cd ~
    HomeDir=$PWD
    BackupYear=${f:0:4}
    BackupMonth=${f:4:2}

    # Test to see if the main backup directory exists if not then create it
    if [ ! -d $HomeDir/$BackupDir ]; then
    mkdir $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 $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 $HomeDir/$BackupDir/$BackupYear/$BackupMonth
    fi

    #Step #02 - command to be executed:
    cp ~/log/*-access.log.gz ~/$BackupDir/$BackupYear/$BackupMonth/

    # end of script

    More details at MrCompTech.com
     

Share This Page