Wednesday 30 October 2013

rsync push example script

rsync Notes:

earlier my rsync dryrun command was:
------------------------------------
/usr/bin/rsync -avvzb --backup-dir=/mnt/rsync-backup/patch-`date +%s`-By-$Username --exclude=$ExcludeFile -n $AbsolutePatchPath/* $RemoteHost:$RemotePath/ | tee /tmp/dry/rsync-push-dryrun-`date +%s`


earlier my rsync command was:
-----------------------------
 /usr/bin/rsync -avvzb --backup-dir=/mnt/rsync-backup/patch-`date +%s`-By-$Username --exclude=$ExcludeFile $AbsolutePatchPath/* $RemoteHost:$RemotePath/     | tee /tmp/rsync-push/rsync-push-`date +%s`


following is the details information about the aboe options:
------------------------------------------------------------
-v, --verbose               increase verbosity  

 What is -a option in rsync. [ You might don't need everything... at that time you can use the only needed options. ]
-a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)

-rlptgoD

-r, --recursive             recurse into directories

-l, --links                 copy symlinks as symlinks
-p, --perms                 preserve permissions   [ NO - Need ]
-t, --times                 preserve modification times [ NO - Need ]
-g, --group                 preserve group [ No- Need ]
-o, --owner                 preserve owner (super-user only)
-D                          same as --devices --specials [ No - Need ]

-z, --compress              compress file data during the transfer [ If the rsync is doing in the same computer then no need... it will take more cpu ]

-b, --backup                make backups (see --suffix & --backup-dir)
[b --backup-dir=/mnt/rsync-backup/patch-`date +%s`-By-$Username]

--backup-dir=DIR        make backups into hierarchy based in DIR
--suffix=SUFFIX         backup suffix (default ~ w/o --backup-dir)

-n, --dry-run               perform a trial run with no changes made
--existing              skip creating new files on receiver  [ IMP - will not create the file if its not there ]

--exclude=PATTERN       exclude files matching PATTERN

--log-file=FILE         log what we're doing to the specified FILE



so the new options can be:
==========================
-vvrlb -backup-dir=<backup_path>  [ if doing the dryrun then use -n ]

===========================

Few mrore option that you can consider:
--------------------------------------
-u, --update                skip files that are newer on the receiver
-W, --whole-file            copy files whole (w/o delta-xfer algorithm)
-e, --rsh=COMMAND           specify the remote shell to use
--existing              skip creating new files on receiver  [ IMP - will not create the file if its not there ]
--ignore-existing       skip updating files that exist on receiver
-C, --cvs-exclude           auto-ignore files in the same way CVS does
--exclude=PATTERN       exclude files matching PATTERN
--exclude-from=FILE     read exclude patterns from FILE
--include=PATTERN       don't exclude files matching PATTERN
--include-from=FILE     read include patterns from FILE
--files-from=FILE       read list of source-file names from FILE
-h, --human-readable        output numbers in a human-readable format
--progress              show progress during transfer
-i, --itemize-changes       output a change-summary for all updates
--log-file=FILE         log what we're doing to the specified FILE


=== rsync with the ssh command inputs ===

Doing a rsync with the remote host where password bashed login id not enable and where you need to login with the key only.

rsync -avvz -e "ssh -p<port> -i <public-key> -l <user>" <source/> Remote-host:/Remote-path

The above command is passing the "ssh" option after "-e"

If needed you need to provide the port and the key and the username by which you need to do the rsync.


















### Start of script ###

#!/bin/bash

# Version: 1
# Date: 30-Oct-2013
# Purpose to deploy the patch
# Dep: ssh config must be updated at ~/.ssh/config to communicate between the hosts.

# Function declaration:

## Function *** fJUsername ***
fJUsername(){
echo "Enter your username as per your email id, without @.j.com "
read -p "Username: " JUsername
echo "${JUsername}"
}

## Function *** fPatchPath ***
fPatchPath(){
echo "Please enter the absolute path of the patch path: "
read -p "AbsolutePatchPath: " AbsolutePatchPath
echo "${AbsolutePatchPath}"
fValidatePatchPath
}

## Function *** fExcludeFile ***
fExcludeFile(){
read -p "Please enter the file that you want to exclude: [ e.g: README ]: " ExcludeFile
echo $ExcludeFile
}

## Function *** fLikeToCont ***
fLikeToCont(){
read -p "Would you line to input the new AbsolutePatchPath: [y,n]" yn
  if [ $yn == y ]; then
    fPatchPath
  else
    echo "Exiting... "
    exit 0
  fi
}

## Function *** fLikeToPatch ***
fLikeToPatch(){
read -p "Would you like to patch the above files on the remote system? [y,n]" findyn
  if [  $findyn = y ]; then
    echo " Continuing the patch deployment..."
  else
     fLikeToCont
  fi
}


## Function *** fValidatePatchPath ***
fValidatePatchPath(){
echo "Validating the PatchPath and giving the file details:"
if [ -d "$AbsolutePatchPath" ]; then
echo -e "Path $AbsolutePatchPath is there on the system...\n"
  echo -e "******* List of files to be deployed? **********\n"
  echo "Following are the file that are there on the source path."
  cd $AbsolutePatchPath && /usr/bin/find .
  echo -e "\n****************************************************\n"
  fLikeToPatch
else
  fLikeToCont
fi
}

## Function *** fRemoteHost ***
fRemoteHost(){
read -p "Please enter remote host(s): [ Valid hosts: crushmypage ]: " RemoteHost
read -p "Deploying On host $RemoteHost [y,n]: " RemoteYN
if [ $RemoteYN == y ]; then
  echo "Deployment command over here";
else
  echo "Existing... "
fi
}

## Funtion *** fRemotePath ***
fRemotePath(){
read -p "Enter the remote path: [ For Ubuntu: /var/www and for fedora: /var/www/html ]: " RemotePath
echo $RemotePath
}


## Function declaration over.
## Write your program after the function declaration only.

fJUsername
fPatchPath
fExcludeFile
fRemotePath
fRemoteHost

echo " This command will copy file from $AbsolutePatchPath/ and will put at $RemoteHost:$RemotePath/"
read -p "Would like to deploy? [y,n] " RemoteDeployYN
if [ $RemoteDeployYN == y ]; then
  echo "*** Running dryrun ***"
## NOTE: in-place of [a] adding [rl] option. See the above note why?
  /usr/bin/rsync -vvrlzb --backup-dir=/mnt/rsync-backup/patch-`date +%s`-By-$JUsername --exclude=$ExcludeFile -n $AbsolutePatchPath/* $RemoteHost:$RemotePath/ | tee /tmp/dry/rsync-push-dryrun-`date +%s`
  echo "Dryrun log file at /tmp/dry/ directory with the latest file..."
  read -p "Please go through the above dryrun... would you like to really update the same [y,n]: " DeployInReal
  if [ $DeployInReal == y ]; then
    /usr/bin/rsync -vvrlzb --backup-dir=/mnt/rsync-backup/patch-`date +%s`-By-$JUsername --exclude=$ExcludeFile $AbsolutePatchPath/* $RemoteHost:$RemotePath/     | tee /tmp/rsync-push/rsync-push-`date +%s`
    echo "Patch deployment log file at /tmp/rsync-push/ directory..."
    echo "On remote system the backed up file stay under the /mnt/rsync-backup/ directoy wht the latest update directory... "
  else
    echo "Closing the script... "
    exit 0
  fi
else
  echo "no deployment"
  exit 0
fi

## End of the script ##






No comments:

Post a Comment