Pages

Sunday, April 10, 2011

Copy named files to remote host and recreate directories


A script to copy only certain file name from tree to remote host and recreate
the directory structure. Had thought scp -r or something would do it. or rsync
directly but well, the rsync manual leaves me cold for things like what to
include and what to exclude… It's probably a lot simpler than this of course ;(





#!/bin/bash
host=${1:-dev}
filename=$2
destdir=$3

if [ "${host}" = "" ]; then
echo "No destination host specified"
echo "Usage : copy-files HOSTNAME FILENAME DESTDIR"
exit
fi

if [ "${filename}" = "" ]; then
echo "No FILENAME specified"
echo "Usage : copy-files HOSTNAME FILENAME DESTDIR"
exit
fi

if [ "${destdir}" = "" ]; then
echo "No DESTDIR specified"
echo "Usage : copy-files HOSTNAME FILENAME DESTDIR"
exit
fi

find . -type f -iname "${filename}" 2> /dev/null | while read -r FILE
do
REMDESTDIR="~/${destdir}/`dirname ${FILE#./}`"
echo "Found : ${FILE} .. Attempting : rsync -avz ${FILE} ${host}:${REMDESTDIR}"
rsync -avz "${FILE}" "${host}:${REMDESTDIR}/"
done




--
My Emacs Files At GitHub

No comments:

Post a Comment