I'm writing a simple automatic backup/versioning bash script.
It should basically mirror a directory structure somewhere, and then copy files over there when they've been altered. The ghost files should then be named filename.YYYYMMDD-HHMMSS.ext
(the included date/time being the time of last modification).
This is what I came up with - it seems to work already, sort of. but since I've never written shell scripts before, I suspect I might be doing some things fundamentally wrong and inefficiënt.
How can I make this faster? The iterating over files seems to be really slow. Is this robust? It seems to "lock up" without error messages sometimes, and I can't explain or understand why. What am I doing wrong?
I intend to run this as a cron job, every 30 minutes or so.
#!/bin/sh
# $1 : backup "root"
# $2 : from: directory to be backed up
# $3 : to: destination
rpath=$1
for f in $(find $2); do
r=$(./rel.sh $rpath $f)
if [ -f $f ]
then
basename=$(basename $f)
dir=$(dirname $r)
name=$(echo $basename | cut -d'.' -f1)
ext=$(echo $basename | cut -d'.' -f2)
mod=$(stat --format=%y $f | awk -F'.' '{printf $1}' | sed 's/[-:]//g' | sed 's/ /-/g')
if [ ! -f "$3/$dir/$name.$mod.$ext" ]
then
echo "+f $mod $r"
# cp $f "$3/$dir/$name.$mod.$ext"
fi
elif [ -d $f ]
then
if [ ! -d "$3/$r" ]
then
echo "+dir $r"
mkdir "$3/$r"
fi
fi
done
exit