Batch directories backup
From SHellium Wiki
Contents |
Description and Usage
This script uses rsync and ssh to backup directories specified on a secondary file to their corresponding destinations.
Files:
- Bk.script
- The actual backup script, that does the dirty work.
- Bk.source
- Script containing two arrays. The SOURCES array, which contains the path strings to the directories on that server that will be backed up, and the DESTINATIONS arrays, which contains the corresponding destination strings in the form "serve:path". The sources and destinations are matched by their index on the arrays. There is also a USER variable that needs to be set. The file is well commented.
Ideally the dcdBk.script file should be in the crontab (as a cron job), and of course be executable.
Bk.script
#!/bin/bash
source ./Bk.sources
for (( i = 0 ; i < ${#SOURCES[@]} ; i++ ))
do
source=${SOURCES[${i}]}
destination=${DESTINATIONS[${i}]}
index=$(expr index $destination ':')
destination_server=${destination:0:$(($index - 1))}
destination_directory=${destination:${index}}
comando='[ -a ${destination_directory} ]'
if ! ssh ${USER}@${destination_server} eval $comando; then
ssh ${USER}@${destination_server} mkdir -p ${destination_directory}
fi
if rsync --append-verify -rutq ${source}/ ${USER}@${destination_server}:${destination_directory}; then
echo `date '+%Y%m%d'` >> ${source}/backup_time.log
rsync -utq ${source}/backup_time.log ${USER}@${destination_server}:${destination_directory}/backup_time.log
fi
done
Bk.sources
#On the SOURCES array there should be the path to the folders to be backed up, the corresponding destinations should be on the DESTINATIONS array. #It's important to observe that the path strings should always end with an '/', otherwise rsync will behave badly. #The destination strings should be in the form "server:path". SOURCES=( "/home/erica/laminarinase/2HYK/" "/home/erica/laminarinase/M133C/" ) DESTINATIONS=( "foo.iqm.unicamp.br:/store7/jorge/laminarase/2HYK/" "foo.iqm.unicamp.br:/store7/jorge/laminarase//M133C/" ) #This is the user that will ssh on the destination server. It should poses an public key on that server, so that it wont be necessary to type in the password. USER="Foo"
License
These scripts where written by Sumeniac and are free to be modified, copied, and distributed.