2

I have simple script that backing up data from remote server via ssh using rsync.

I have external config file for that. In this config file I have variables: OPTIONS, REMOTE_IP, SOURCE and DESTINATION.

Now I need to add more remote servers and start using single script for multiple servers. I want define it with sections (like [SERVER_01],[SERVER_02]...) in config.

Script:

# You can provide external configuration file if you specify it with -c option
# Then if you haven't specified it, use one from ~/rsync_script/config.cfg

if [[ $1 == -c ]]; then
    CONFIG_FILE=$2
else
    CONFIG_FILE=~/rsync_script/config.cfg
fi


# Add constants from config file to script's environment

if [[ -f $CONFIG_FILE ]]; then
    . $CONFIG_FILE
fi


# Create full path before running rsync, because rsync cannot mkdir with -p option
# Run rsync with parameters from config.cfg and put files to $DESTINATION/$REMOTE_IP/YYYY-MM-DD

if [[ -d $DESTINATION ]]; then
    mkdir -p $DESTINATION$REMOTE_IP/$(date +"%A")
    rsync -avx \
        --timeout=30 \
        $OPTIONS \
        rsync@$REMOTE_IP:$SOURCE $DESTINATION$REMOTE_IP/$(date +"%F")
else
        echo "failure"
fi

Config:

# Set extra options for rsync command
OPTIONS="--itemize-changes --log-file=changes.log"
# Set IP address of server the you want to backup
REMOTE_IP="192.168.11.123"
# Set the folder on remote server to backup
SOURCE="/home/rsync/somedata"
# Set the destination folder on local machine
DESTINATION="/backup/"

Suggest me a best way to solve this, please

Any code comments and advices are welcomed :)

Thanks

1 Answer 1

1

Here's a possible scenario. Put your existing rsync code (if [[ -d $DESTINATION ...) into a shell function, say runbackup, and replace the part that does . $CONFIG_FILE by a loop that reads the file and looks for the [SERVER_...] section separator. When it finds one, it calls the runbackup function (except for the first one). For the other lines it does eval on each line just like . does. To ensure runbackup is called on the last section, a dummy [END] section is added to the input.

(cat $CONFIG_FILE; echo '[END]') |
while read line
do if [[ "$line" =~ ^\[([A-Z_0-9]+)\] ]]
   then if [ -n "$OPTIONS" -a -n "$REMOTE_IP" ]
        then echo "section $section"
             runbackup
        fi
        section=${BASH_REMATCH[1]} # captured from =~ regex above
        unset OPTIONS REMOTE_IP SOURCE DESTINATION
   else eval $line
   fi
done 
1
  • Didn't you missed dollar sign for REMOTE_IP in first case?
    – Roman
    Oct 12, 2015 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .