patternbashMinor
Can I speed up this simple versioning/backup script?
Viewed 0 times
thisscriptcansimpleversioningspeedbackup
Problem
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
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.
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
Solution
Implementing solutions to problems like this is good for exercise purposes. It is good for learning.
But it is almost never good to re-invent the wheel in production systems. Please have a look at well-established data synchronization software such as rsync and perform some research around it and other data synchronization/backup/snapshot techniques.
So, your question is good until "I intend to run this as a cron job, every 30 minutes or so." :-) I am not that bash expert myself, to others might point out the potential weaknesses of your approach.
But it is almost never good to re-invent the wheel in production systems. Please have a look at well-established data synchronization software such as rsync and perform some research around it and other data synchronization/backup/snapshot techniques.
So, your question is good until "I intend to run this as a cron job, every 30 minutes or so." :-) I am not that bash expert myself, to others might point out the potential weaknesses of your approach.
Context
StackExchange Code Review Q#15905, answer score: 8
Revisions (0)
No revisions yet.