Question

I'm trying to write a bash script to automate my backup plan. I use a script which creates a S3 folder each day with the day as folder name. And Each hour he uploads a backup in this folder. exemple: /Application1/20130513/dump.01

My backup plan is to keep 2 days of full backup(each hour) and keep 1 backup by day for the latest 15 days in a s3 folder ("oldbackup").

What is wrong in my script?

#check and clean the S3 bucket
BUCKETNAME='application1';
FOLDERLIST = s3cmd ls s3://$BUCKETNAME
LIMITFOLDER = date --date='1 days ago' +'%Y%m%d'

for f in $FOLDERLIST
do
    if [[ ${f} > $LIMITFOLDER && f != "oldbackup" ]]; then
        s3cmd sync s3://$BUCKETNAME/$f/dump.rdb.0 s3://$BUCKETNAME/"oldbackup"/dump.rdb.$f
        s3cmd del s3://$BUCKETNAME/$f --recursive;
    fi
done

OLDBACKUP = s3cmd ls s3://$BUCKETNAME/"oldbackup"
LIMITOLDBACKUP = date --date='14 days ago' +'%Y%m%d'
for dump in $OLDBACKUP
    if [${dump} > $LIMITOLDBACKUP]; then
                s3cmd del s3://$BUCKETNAME/"oldbackup"/$dump
    fi
done

Thanks

Was it helpful?

Solution

First, you are probably going to want to store FOLDERLIST as an array. You can do so like this: FOLDERLIST=($(command)).

Next, you should always store the output of commands which you intend to use like a string like so OUTPUT="$(command)".

So for example your first three lines should look like:

BUCKETNAME="application1"
FOLDERLIST=($(s3cmd ls s3://$BUCKETNAME))
LIMITFOLDER="$(date --date="1 days ago" +"%Y%m%d")"

Now your first for-loop should work.

That's the only thing I can guess is wrong with your script (the second for-loop suffers the same) but you really gave me nothing better to go on.

Your second for-loop, (besides not iterating over a proper array) has no do keyword, so you should do:

for dump in $OLDBACKUP
do
   # rest of loop body
done

That could be another issue with your script.

Finally, you're only ever using OLDBACKUP and FOLDERLIST to iterate over. The same can be accomplished just by doing:

for f in $(s3cmd ls s3://$BUCKETNAME)
do
  # loop body
done

There's no need to store the output in variables unless you plan to reuse it several times.


As a separate matter though, there's no need to use variable names consisting entirely of the capitalized alphabet. You can use lowercased variable names too so long as you understand that using the names of commands will cause errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top