#!/bin/bash -
#===============================================================================
#
#         FILE: archive_metadata
#
#        USAGE: ./archive_metadata
#
#  DESCRIPTION: Adds metadata files (metadata_*.csv) to an archive for
#               reference
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.1
#      CREATED: 2015-06-14 12:57:04
#     REVISION: 2015-06-14 12:59:48
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

SCRIPT=${0##*/}
DIR=${0%/*}

#
# Age threshold for archiving files
#
DAYS=120

#
# Add the eligible file names to an array (to count them)
#
declare -a files=($(find . -name "metadata_*" -mtime +$DAYS -print))

if [[ ${#files[@]} > 0 ]]; then
    #
    # Uncompress the archive
    #
    echo "Uncompressing archive..."
    bunzip2 meta.tar.bz2

    #
    # Add a bunch of older metadata files
    #
    echo "Adding files to the archive..."
    tar --verbose --append --file=meta.tar $(find . -name "metadata_*" -mtime +$DAYS)

    #
    # Zip up the archive again
    #
    echo "Compressing the archive..."
    bzip2 meta.tar

    #
    # Delete all those old files
    #
    echo "Deleting archived files..."
    find . -name "metadata_*" -mtime +$DAYS -print -exec rm -f {} \;
else
    echo "Nothing to archive"
fi

exit

# vim: syntax=sh:ts=8:sw=4:et:tw=78:fo=tcrqn21