Script to syncro one folder or more to a remote server using RSYNC and SSH tunnel.
First point, install a rsync package to both systems:
# apt-get install rsync
Second point, generate a suite of keys (public and private) to use with SSH. Tutorial
Example:
#!/bin/bash DEST=IP # Remote IP PATHKEY=/root/identity_key # Path DSA key MYCMD=$(basename $0) MYDATALOG=$(date +%Y%m%d-%H%M) MYLOGDIR=/etc/SCRIPT/log # Where to put Logs MYLOGFILE=${MYLOGDIR}/${MYDATALOG}-${MYCMD}.log LCK_FLE='/var/run/repo-sync.lck' RSYNC_OPTS="-rtlHq --delete-after --delay-updates --copy-links --safe-links --max-delete=1000 --bwlimit=${BW_LIMIT} --delete-excluded --exclude=.*" source_master_directory="/" destination_target_directory="/" master_server_desc="Aggiornamento" # Make sure only 1 instance runs if [ -e "$LCK_FLE" ] ; then OTHER_PID=$(cat $LCK_FLE) echo "Another instance already running: $OTHER_PID" exit 1 fi echo $$ > "$LCK_FLE" directory_2align="home/BACKUP/mysql/" # Folders to align separated by a white space. Example: home/users/ /home/backup ws_date=$(date) echo "- - - - - - - Started: $ws_date - - - - - - -" > ${MYLOGFILE} echo ' --------------------------------------------------------- LOOP sending the newest file to other servers --------------------------------------------------------- ' >> ${MYLOGFILE} echo ' --------------------------------------------------------- ESECUZIONE IN BACKG sending the newest file to other servers --------------------------------------------------------- ' for x in $DEST do for y in $directory_2align do destination=${x} directory=${y} echo [${destination}] "Directory: $y" >> ${MYLOGFILE} echo \>\>\> sending newest files to server ${destination} ... >> ${MYLOGFILE} MAX_RETRIES=10 i=0 false while [ $? -ne 0 -a $i -lt $MAX_RETRIES ] do i=$(($i+1)) rsync -avz --progress --delete --partial --recursive -e 'ssh -i $PATHKEY' ${source_master_directory}${directory} ${destination}:${destination_target_directory}${directory} >> ${MYLOGFILE} done if [ $i -eq $MAX_RETRIES ] then echo "Hit maximum number of retries, giving up." >> ${MYLOGFILE} fi ws_date=$(date) echo '' >> ${MYLOGFILE} echo "- - - - - - - Ended: ${ws_date} - - - - - - -" >> ${MYLOGFILE} echo '' >> ${MYLOGFILE} done done # Cleanup rm -f "$LCK_FLE" exit 0