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