logotype

Backup Apache2 Access Logs in Ubuntu

This is a followup post to my CRON backup post. To backup the access (and error) logs of apache2 there are a couple of extra steps needed. Primarily moving the log files before zipping and gracefully restarting Apache to release the read lock on the current log files.

 

To backup the apache access logs on Ubuntu you can use the following script:

 

#!/bin/sh
####################################
#
# Backup to NFS mount script.
#
####################################

# What to backup.
backup_files="/var/log/apache2/oreillyit.com-access_log"

# Where to backup to.
dest="/var/www/vhosts/oreillyit.com/logbackups"

# Create archive filename.
thedate=$(date +%Y-%M-%d)
hostname=$(hostname -s)
archive_file="$hostname-$thedate.tgz"

# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo

mv $backup_files $backup_files$thedate
#Gracefully restart apache apachectl graceful
sleep 600


# Backup the files using tar.
tar czf $dest/$archive_file $backup_files$thedate

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest

 

If this was of interest, then so might my article on using cron to run the backup script automatically

Copyright 2018 OReillyIT. All rights reserved.