Skip to content

Instantly share code, notes, and snippets.

@lunr
Created July 19, 2012 18:50
Show Gist options
  • Select an option

  • Save lunr/3145955 to your computer and use it in GitHub Desktop.

Select an option

Save lunr/3145955 to your computer and use it in GitHub Desktop.
Remote Asset Rsync Script to easily rsync assets from one server to another
#!/bin/bash
# name: rarsync - Remote Asset Rsync
# author: [email protected]
# date: 2012-01-23
# version 0.9.2
DEST_PATH=$2
DEST_REMOTE=0
SOURCE_PATH=$1
SOURCE_REMOTE=0
OPTIONS='-avz'
# STOP CONF HERE
ME=`basename $0`
# Check for empty usage
if [[ -z $DEST_PATH ]] || [[ -z $SOURCE_PATH ]];
then
echo "Usage:"
echo " $ME source destination"
echo "Example:"
echo " $ME web1:/var/www/html/teamddm.com ~/Sites/teamddm.com"
echo "Notes:"
echo " - This works best with key-based logins through SSH"
echo " - No trailing slashes on your parameters"
echo " - Offers dry-run of folder sync with option to cancel a specific folder"
echo " - Uses -avz options during rsync"
echo " - Excludes .svn folders from rsync"
exit 1
fi
# Check to see which one is remote or if both are remote
echo $DEST_PATH | grep -i '[a-z0-9]:' > /dev/null
if [ $? -eq 0 ];
then
DEST_REMOTE=1
fi
echo $SOURCE_PATH | grep -i '[a-z0-9]:' > /dev/null
if [ $? -eq 0 ];
then
SOURCE_REMOTE=1
fi
if [ $DEST_REMOTE -eq 1 ] && [ $SOURCE_REMOTE -eq 1 ];
then
echo "Source and destination cannot both be remote. Rsync doesn't like that!";
exit 1;
fi
# Now, Figure out if source is remote and get its directory structure
if [ $SOURCE_REMOTE -eq 1 ]
then
SHOST=`echo "$SOURCE_PATH" | cut -d ':' -f1`
SPATH=`echo "$SOURCE_PATH" | cut -d ':' -f2`
# User may have to login to their shell account with a password prompt
FOLDER_LIST=$(ssh $SHOST ls $SPATH)
else
FOLDER_LIST=$(ls $SOURCE_PATH)
fi
for FOLDER in ${FOLDER_LIST[*]}; do
echo -e "\n"
echo -e "Starting rsync preview of folder: $FOLDER"
read -p "Press enter to continue or 'skip' to skip or Ctrl-C to quit: " SKIP
if [ "$SKIP" == "skip" ]; then
echo -e "\nSkipping $FOLDER"
else
DRYRUN=`echo "$OPTIONS""n"`
# User may have to login to their shell account with a password prompt
rsync $DRYRUN --exclude="*.svn" $SOURCE_PATH/$FOLDER/ $DEST_PATH/$FOLDER
read -p "Would you like to do this for real? (yes/no) " RESP
if [ "$RESP" == "yes" ]; then
# User may have to login to their shell account with a password prompt
rsync $OPTIONS --exclude="*.svn" $SOURCE_PATH/$FOLDER/ $DEST_PATH/$FOLDER
echo -e "\nCOMPLETED RSYNC OF $FOLDER"
else
echo -e "\nSkipping $FOLDER"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment