1

I've a backup script (bash). Part of it is shown below. This script does a 14 day rotation of the backup. If I want to change this to say 30 days, I'd have to write out 30 such if-then blocks. I'm sure this could be replaced by a nifty for-loop. What would it be?

# step 1: delete the oldest snapshot, if it exists:
if [ -d $BACKUP_DIR/daily.14 ] ; then                   \
$RM -rf $BACKUP_DIR/daily.14 ;                          \
fi ;

# step 2: shift the middle snapshot(s) back by one, if they exist
if [ -d $BACKUP_DIR/daily.13 ] ; then                   \
$MV $BACKUP_DIR/daily.13 $BACKUP_DIR/daily.14 ; \
fi;

if [ -d $BACKUP_DIR/daily.12 ] ; then                   \
$MV $BACKUP_DIR/daily.12 $BACKUP_DIR/daily.13 ; \
fi;

if [ -d $BACKUP_DIR/daily.11 ] ; then                   \
$MV $BACKUP_DIR/daily.11 $BACKUP_DIR/daily.12 ; \
fi;

if [ -d $BACKUP_DIR/daily.10 ] ; then                   \
$MV $BACKUP_DIR/daily.10 $BACKUP_DIR/daily.11 ; \
fi;

if [ -d $BACKUP_DIR/daily.9 ] ; then                    \
$MV $BACKUP_DIR/daily.9 $BACKUP_DIR/daily.10 ;  \
fi;

if [ -d $BACKUP_DIR/daily.8 ] ; then                    \
$MV $BACKUP_DIR/daily.8 $BACKUP_DIR/daily.9 ;   \
fi;

if [ -d $BACKUP_DIR/daily.7 ] ; then                    \
$MV $BACKUP_DIR/daily.7 $BACKUP_DIR/daily.8 ;   \
fi;

if [ -d $BACKUP_DIR/daily.6 ] ; then                    \
$MV $BACKUP_DIR/daily.6 $BACKUP_DIR/daily.7 ;   \
fi;

if [ -d $BACKUP_DIR/daily.5 ] ; then                    \
$MV $BACKUP_DIR/daily.5 $BACKUP_DIR/daily.6 ;   \
fi;

if [ -d $BACKUP_DIR/daily.4 ] ; then                    \
$MV $BACKUP_DIR/daily.4 $BACKUP_DIR/daily.5 ;   \
fi;

if [ -d $BACKUP_DIR/daily.3 ] ; then                    \
$MV $BACKUP_DIR/daily.3 $BACKUP_DIR/daily.4 ;   \
fi;

if [ -d $BACKUP_DIR/daily.2 ] ; then                    \
$MV $BACKUP_DIR/daily.2 $BACKUP_DIR/daily.3 ;   \
fi;

if [ -d $BACKUP_DIR/daily.1 ] ; then                    \
$MV $BACKUP_DIR/daily.1 $BACKUP_DIR/daily.2 ;   \
fi;


# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot, if that exists
if [ -d $BACKUP_DIR/daily.0 ] ; then                    \
$CP -al $BACKUP_DIR/daily.0 $BACKUP_DIR/daily.1 ;       \
fi;
  • Please try searching on Google atleast once before posting here. There are hundreds of websites that show you how to get this done. – darnir Aug 9 '12 at 19:25
  • 1
    Perhaps, but I'm new to bash and a Google search for 'bash loop' doesn't produce anything (perhaps it does, but since I'm not all that familiar with bash, I may have missed it or not gotten a hang of it) that applies to this particular case. Thanks though! – Sparctus Aug 9 '12 at 20:09
1
#!/bin/bash
MAX=29

for i in `seq 1 $MAX` 
do 
    echo " check file exists directory.$i"
    echo "mv directory.$i to directory" `expr $i - 1`
done

This might be a good starting point

  • Thank you Goblin for pointing me in the right direction. After a fair bit of trail and error, I was able to get it done. Your seq cmd only increments. I needed a decreasing number sequence. I've posted my solution. Thank you again. – Sparctus Aug 9 '12 at 21:53
  • sequence command can also be used for decrement. You can use seq $MAX -1 1 – Goblin Aug 14 '12 at 14:50
0
for i in {7..1};
do
        #echo "$i"

        if [ -d $BACKUP_DIR/daily.${i} ]
        then
                z=$(($i+1));
                #echo "$z"
                echo "moving daily.$i to daily.$z"
                $MV $BACKUP_DIR/daily.$i $BACKUP_DIR/daily.$z ;
        fi;
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.