[TriLUG] Some script help

Brian Blater via TriLUG trilug at trilug.org
Fri May 8 10:36:53 EDT 2015


I'm trying to get a basic bash script put together to copy some backup
files from one location to another. I've got the basics done, I think, but
one section could use some regex or similar and my regex abilities are bad.

Here is the script:

#!/bin/bash

## Establish DATE ##
NOW=$(date +"%F")
DELDATE=$(date --date="7 days ago" +"%F")

## Remote Server ##
## RSERVER="servername"
RPATH="/path/to/remote/dir/"

## Local Path ##
LPATH="/path/to/local/dir/"

## Copy files ##
for i in $( ls $RPATH ); do
## some regex or ? to get only the date from file        ##
## assign that to $FILE                                  ##
## file looks like: backup_2014-06-13_7-00-07_14.1R1.tgz ##
## only want to compare the 2014-06-13 of file with $NOW ##

## if it is today's file copy it ##
if [ "$FILE" = "$NOW" ]; then
cp $i $LPATH
else
## if it is older than 7 days mv it ##
if [ "$FILE" < "$DELDATE" ]; then
mv $i $LPATH
fi
fi
done

For now, my issue is in the Copy Files section. As the note says I need to
look at each file and compare just the date portion. If it matches $NOW I
want to copy it.

The other concern is the next IF in that section. I basically want to leave
a weeks worth of files in that directory before copying them to remote
storage. Can I just do a < there, or do I need some more magic to do that
comparison?

Thank you for your help.
Brian


More information about the TriLUG mailing list