Home > bash, RHEL, Slackware, Uncategorized > Linux bash: copy particular files in between two dates back in time to a network filer

Linux bash: copy particular files in between two dates back in time to a network filer

friday.logs.to.filer.sh

#!/bin/sh
# friday.logs.to.filer.sh
# seikath@gmail.com, Barcelona
# Fri Feb 18 09:21:49

PERIOD_IN_DAYS_BACK="-7"
DEST_DIR_ROOT="/mnt/nfs/root"
CURRENT_DEST_DIR="${DEST_DIR_ROOT}/logs/${HOSTNAME}/"$(date +"%Y-%m-%d")
NETWROK_FILER_MOUNT="//192.168.5.4/web /mnt/nfs"
SCRIPT_NAME=${0%.*}
LOG_FILE="/var/log/$(basename ${SCRIPT_NAME}).log"
CONF_FILE=${SCRIPT_NAME}.conf
#NEWER_THAN=$(date -d "today ${PERIOD_IN_DAYS_BACK} day " +"%Y%m%d2359")
#OLDER_THAN=$(date -d "today -1 day " +"%Y%m%d2359")

function logit () {
    echo "$(date)::[${HOSTNAME}] : ${1}"
    echo "$(date)::[${HOSTNAME}] : ${1}" >> "${LOG_FILE}"
}

# check the filer mount
mount_status=$(fgrep "${NETWROK_FILER_MOUNT}" /proc/mounts)
if [ "${mount_status}" == "" ] 
then
    logit "ERROR : //192.168.5.4/web is not mounted as /mnt/nfs, skipping the log backup"
    exit 0
fi

# check destination directory
if [ ! -d "${CURRENT_DEST_DIR}" ]
then
    logit "NOTICE : Missing dir ${CURRENT_DEST_DIR}, creating it"
    mkdir -p "${CURRENT_DEST_DIR}"
fi

# check permissions
if ! touch "${CURRENT_DEST_DIR}/test.permissons"
then
    logit "ERROR : Not able to write to ${CURRENT_DEST_DIR}"
    exit 0
else 
    unlink "${CURRENT_DEST_DIR}/test.permissons"
fi



while read settings
do
  if [ "${settings:0:1}" == "#" ];then continue;fi
  dirs_to_backup_o=${settings/,*/}
  dirs_to_backup=$(echo ${dirs_to_backup_o} | sed 's/\/*$//')
  regexp_to_backup=${settings/,*/}
  regexp_to_backup=${settings##${regexp_to_backup},}
  regexp_to_backup=${regexp_to_backup/,*/}
  regexp_to_ignore=${settings##${dirs_to_backup_o},${regexp_to_backup},}   
  if [ "${settings}" == "${regexp_to_ignore}" ];then regexp_to_ignore="";fi
 if [ ! -d "${dirs_to_backup}" ]
  then
      logit "NOTICE : [${dirs_to_backup}] from ${CONF_FILE} is not existing directory"
      continue
  fi
  #echo "find ${dirs_to_backup}/ -maxdepth 1 -type f -name \"${regexp_to_backup}\" ! -name \"${regexp_to_ignore}\" -daystart -mtime +0 -mtime ${PERIOD_IN_DAYS_BACK} -printf '\"%p\"\n'"
  find ${dirs_to_backup}/ -maxdepth 1 -type f -name "${regexp_to_backup}" ! -name "${regexp_to_ignore}" -daystart -mtime +0 -mtime ${PERIOD_IN_DAYS_BACK} -printf '"%p"\n' | \
  xargs -r -I crap cp -vv crap "${CURRENT_DEST_DIR}"
done < ${CONF_FILE}

root@xxxx:[Fri Feb 18 14:24:56]:[/apps/scripts]$ cat friday.logs.to.filer.conf
# dir,file_patern, ignore_file_patern   
/var/log/httpd/,xxxx.*,*request*
#/backups,xxxx.*hmv*
/backups,xxx.*api.log*
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: