Distribute files to multiple servers using rsync and ssh

From dbawiki
Jump to: navigation, search

From Windows, if PuTTY is installed, pscp can be used[edit]

e.g.

pscp -q -batch -i 'C:\home\ibmtools\etc\oracle\id_dsa' 'J:\oraclebackup\ATHARCHIV\BACKUP_EXPORT_10G_SE_ATHARCHIV_20160122001001_01.DMP.gz' oraibm@solax100:/tmp

Using scp[edit]

#!/bin/ksh

servers=`cat /home/ibmtools/ini/system/servers`

for server in $servers
do
        echo $server
        if [ ${server} != "`uname -n`" ]
        then
                scp -p $1 ${server}:$2
        fi
done

Using rsync[edit]

Sending:[edit]

#!/usr/bin/ksh
# ==============================================================================
# Name         : rsync.ksh
# Description  : Synchronise files from mgmt server to the Oracle servers
#                As Unix team do not want to install cfengine...
#
# Parameters   : none
#
# Notes        : This should be run from root crontab on mgmt server only
#                There is a sister script on delta that syncs files to mgmt srv
#                thus completing the fake cfengine circle
#
# Modification History
# ====================
# When      Who               What
# ========= ================= ==================================================
# 00-NOV-14 Stuart Barkley    Created
# ==============================================================================



# ------------------------------------------------------------------
# loop over the target servers, sending the item to each one in turn
# ------------------------------------------------------------------
function sync {
ITEM=$1

SERVERLIST=`cat /home/ibmtools/etc/oracle/oracle_servers | xargs`
for SERVER in $SERVERLIST
do
    rsync -auv -e ssh --rsync-path=/usr/local/bin/rsync $ITEM $SERVER:$ITEM
    if [[ $? -eq 0 ]] ;then
       echo "$ITEM --> $SERVER OK"
    else
       echo "$ITEM --> $SERVER NOK"
    fi

done
}



# ======================================================================
# list of objects (directories and/or files) to sync
# IMPORTANT: if syncing a directory, make sure you have a trailing slash
# ======================================================================

sync "/home/ibmtools/etc/oracle/"
sync "/home/ibmtools/scripts/oracle/"

Pushing with rsync[edit]

Could put these in crontab to maintain the directories/files sync'd

rsync -av -e ssh --rsync-path=/usr/local/bin/rsync /home/ibmtools/scripts/oracle/ bemauora901:/home/ibmtools/scripts/oracle/

Push a directory to multiple servers (note the trailing slash for directories)

#!/bin/ksh
ikke=`uname -n`
servers=`cat /home/ibmtools/ini/system/servers | grep -v ${ikke}`
for server in ${servers}
do
        echo "Synchronise to ${server}"
        echo "---------------------------"
        /usr/local/bin/rsync -av -e ssh --rsync-path=/usr/local/bin/rsync /home/ibmtools/scripts/system/ ${server}:/home/ibmtools/scripts/system/
        sleep 5
done

Pulling with rsync[edit]

/usr/local/bin/rsync -av --progress --rsync-path=/usr/local/bin/rsync adminsrv:/path/to/dir2sync/ /local/destination/dir/ >> /var/adm/rsync.log

or

rsync -avzu <user name>@<server name>:<server directory> <local directory>

References[edit]