Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a shell script running in AIX that will do the backup for each SVN repository every night. The execution is as follow:

Download the each repository save as a dump file:

svnrdump on repo_A

svnrdump on repo_B

svnrdump on repo_X ## X is the subsequent number of repository

After this download process complete, the content will be zipped:

bzip2 --compress repo_A_yyyymmdd.dump

bzip2 --compress repo_B_yyyymmdd.dump

bzip2 --compress repo_X_yyyymmdd.dump ## X is the subsequent number of backup copy

Then I will do a housekeeping on the backup copies by removing those copy that is longer than 5th day old from today (I only need to keep the last 5 latest backup copies):

rm `ls -t repo_A_????????.dump.bz2 | tail -n +6`

rm `ls -t repo_B_????????.dump.bz2 | tail -n +6`

rm `ls -t repo_X_????????.dump.bz2 | tail -n +6` ## X is the subsequent number of backup copy

My question

Is there a way to optimize this code by putting those repo name into an array and then execute every command for each repo in a loop?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Or alternatively:

repodir=/some/path
backupdir=/some/otherpath
maxagedays=6
for repo in repo_A repo_B repo_X; do
   svnadmin dump $repodir/$repo | bzip2 -9 > $backupdir/$repo-`date +%F`.dump.bz2
done
find /$backupdir -type f -iname *dump.bz2 -mtime +$maxagedays -exec rm -f {} \;
share|improve this answer
    
I like this answer because the code looks clean. –  huahsin68 Apr 17 '13 at 7:02
add comment

Try something like this:

First form, array of suffixes:

#!/bin/bash

array=(A B C D E F)
for i in "${array[@]}"
do
    echo svnrdump on repo_$i
    echo bzip2 --compress "repo_$i.dump"
done

Second form, if there repo names are more diverse:

#!/bin/bash

while read REPO; do
    echo svnrdump on  "${REPO}"
    echo bzip2 --compress "${REPO}".dump
done << REPOLIST
repo0
repo1
repo2
repo3
REPOLIST

You will have to add all repos to the REPOLIST.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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