首頁 / 精華文章 / rsend.sh-免背RSYNC指令,無痛快速搬家
Logo

rsend.sh-免背RSYNC指令,無痛快速搬家

有時候會碰到有些東西臨時要搬移,檔案又是幾百GB,這個時候該怎辦咧! 有位好心人Phil Howard寫了一個resend.sh檔案方便有同樣困擾的人使用,不用設定RSYNC,用完即丟,服用本藥品可說是完全的無感啊 (Y)

Step.1:發送端與接收端都先安裝RSYNC與SCREEN,在SCREEN模式底下做傳輸的好處是不怕斷線就要從頭再來,就算是跑個幾百TB,生性的義薄雲天SCREEN兄弟會幫你跑完任務的 😉

yum install -y rsync screen

*********使用前要關閉傳輸端雙方的防火牆*********

Step.2:首先在要搬家的A主機操作,先進入SCREEN模式然後切換到要搬移的位置

screen -R
cd /web/www/qoo
wget http://acelnmp.googlecode.com/files/rsend.sh
chmod +x rsend.sh
rsend.sh

此時螢幕上會出現三排指令,摳比第一行以IP位置顯示的指令然後切換到要接收檔案的B主機

Step.3:SSH登入要接收檔案的B主機,一樣先進入SCREEN模式然後切換到A主機搬家過來的位置

screen -R
cd /web/www/qoo_backup

把剛剛從A主機利用rsend.sh產生的第一行指令摳比過來貼到B主機的SSH運行

rsync -aHvz rsync://8.8.8.8:26736/. .

這個指令是rsend.sh幫你產生的,在B主機一執行就開始刷刷刷的傳檔囉!

傳送完畢後輸入EXIT退出SCREEN模式,回到A主機按CTRL+C停止RSYNC,就這樣~ 免背RSYNC指令,真正的無痛搬家。 😀

rsend.sh下載 | rsend.sh內容

#!/bin/bash
#-----------------------------------------------------------------------------
# Copyright ?2006,2011 - Philip Howard - All rights reserved
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#-----------------------------------------------------------------------------
# script	rsend
#
# purpose	To start an rsync daemon in the shell foreground to send a
#		specified directory or file when retrieved using one of the
#		rsync command lines shown by pasting it in a shell session
#		on another host.
#
# usage		rsend  [options]  directory | file
#
# options	-c	include checksumming in the rsync command lines
#		-d	change to the specified directory for the daemon
#		-n	include dryrun in the rsync command lines
#		-p	use the specified port number (default is random)
#		-s	include sparse in the rsync command lines
#		-u	user to run as (if started as root)
#		-v	show extra information
#
# author	Phil Howard
# email		domain: ipal.org  username: rsend
#-----------------------------------------------------------------------------
umask 022
hostname=$( exec hostname -f )
whoami=$( exec whoami )
uid="${whoami}"

#-----------------------------------------------------------------------------
# Set defaults.
#-----------------------------------------------------------------------------
checksum=""
delete=""
delmsg=""
dryrun=""
padding="-------"
port=""
sparse=""
verbose=""

bar1="#----------------------------------------------------------------------------"
bar2="#############################################################################"

#-----------------------------------------------------------------------------
# Include paths for ifconfig.
#-----------------------------------------------------------------------------
export PATH="${PATH}:/usr/sbin:/sbin"

#-----------------------------------------------------------------------------
# Scan options.
#-----------------------------------------------------------------------------
while [[ $# -gt 0 && "x${1:0:1}" = "x-" ]]; do
    case "x${1}" in
	( x-c | x--checksum )
	checksum="c"
	;;
	( x--delete )
	delete=" --delete"
	delmsg="/delete"
	padding=""
	;;
	( x-d | x--directory )
	shift
	cd "${1}" || exit 1
	;;
	( x--directory=* )
	cd "${1:12}" || exit 1
	;;
	( x-n | x--dry-run )
	dryrun="n"
	;;
	( x-p | x--port )
	shift
	port="${1}"
	;;
	( x--port=* )
	port="${1:7}"
	;;
	( x-s | x--sparse )
	sparse="S"
	;;
	( x-u | x--user )
	shift
	uid="${1}"
	;;
	( x--user=* )
	uid="${1:7}"
	;;
	( x-v | x--verbose )
	verbose=1
	;;
    esac
    shift
done
[[ -z "${abort}" ]] || exit 1

#-----------------------------------------------------------------------------
# Get a random number for a port.
#-----------------------------------------------------------------------------
if [[ -z "${port}" || "${port}" = 0 || "${port}" = . ]]; then
    port=$( dd if=/dev/urandom ibs=2 obs=2 count=1 2>/dev/null | od -An -tu2 | tr -d ' ' )
    port=$[ $port % 16384 ]
    port=$[ $port + 12288 ]
fi

#-----------------------------------------------------------------------------
# Make up names for temporary files to be used.
#-----------------------------------------------------------------------------
conffile="/tmp/rsync-${whoami}-${port}-$$.conf"
lockfile="/tmp/rsync-${whoami}-${port}-$$.lock"

#-----------------------------------------------------------------------------
# This function adds quotes to strings that need them.
# Add single quotes if it has one of these: space $ " `
# Add double quotes if it has one of these: '
# Not all combinations will work.  Something more advanced is needed.
#-----------------------------------------------------------------------------
function q {
    local s
    s=$( echo "${1}" | tr -d ' $"`' )
    if [[ "${s}" != "${1}" ]]; then
	echo "'${1}'"
	return
    fi
    s=$( echo "${1}" | tr -d "'" )
    if [[ "${s}" != "${1}" ]]; then
	echo '"'"${1}"'"'
	return
    fi
    echo "${1}"
    return 0
}

#-----------------------------------------------------------------------------
# Only one name can be handled.
#-----------------------------------------------------------------------------
if [[ $# -gt 1 ]]; then
    echo "Only one name (directory or file)" 1>&2
    exit 1
elif [[ $# -eq 1 ]]; then
    name="${1}"
else
    name=$( exec pwd )
fi

#-----------------------------------------------------------------------------
# Set up a temporary config file.
#-----------------------------------------------------------------------------
function configout {
    [[ -z "${listen}" ]] || echo "address = ${listen}"
    echo "lock file = ${lockfile}"
    echo "log file = /dev/stderr"
    echo "use chroot = false"
    echo "max connections = 32"
    echo "socket options = SO_KEEPALIVE"
    echo "list = yes"
    echo "[.]"
    echo "path = ${1}"
    echo "read only = yes"
    echo "uid = ${uid}"
    echo "comment = ${2}"
    if [[ -n "${3}" ]]; then
	echo "include = **/${3}"
	echo "exclude = **"
    fi
}

#-----------------------------------------------------------------------------
# Get directory and file.
#-----------------------------------------------------------------------------
if [[ ! -e "${name}" ]]; then
    echo "does not exist:" $( q "${name}" ) 1>&2
    exit 1
elif [[ -d "${name}" ]]; then
    p=$( exec dirname "${name}" )
    b=$( exec basename "${name}" )
    d="${name}"
    f=""
    r=$( cd "${name}" && exec pwd )
    announce="${d}"
    rsyncopt="-a${checksum}${dryrun}H${sparse}vz${delete}"
    configout "${d}/." "directory:${d}/" >"${conffile}"
elif [[ -f "${name}" ]]; then
    p=$( exec dirname "${name}" )
    b=$( exec basename "${name}" )
    d="${p}"
    f="${b}"
    r=$( cd "${p}" && exec pwd )
    r="${r}/${b}"
    announce="${d}/${f}"
    rsyncopt="-a${checksum}${dryrun}${sparse}vz"
    configout "${d}/." "file:${d}/${f}" >"${conffile}"
elif [[ -L "${name}" ]]; then
    p=$( exec dirname "${name}" )
    b=$( exec basename "${name}" )
    d="${p}"
    f="${b}"
    r=$( cd "${p}" && exec pwd )
    r="${r}/${b}"
    announce="${d}/${f}"
    rsyncopt="-a${checksum}v"
    configout "${d}/." "symlink:${d}/${f}" "${f}" >"${conffile}"
fi

#-----------------------------------------------------------------------------
# Show config file if verbose is requested.
#-----------------------------------------------------------------------------
if [[ -n "${verbose}" ]]; then
    echo "${bar2}"
    ls -ld "${conffile}"
    echo "${bar2}"
    cat "${conffile}"
fi

#-----------------------------------------------------------------------------
# This function outputs example receive commands for a host and destination.
#-----------------------------------------------------------------------------
function showrsync {
    if [[ -n "${oldfmt}" ]]; then
	echo "rsync ${rsyncopt}" "--port=${port}" $( q "${1}::${2}" ) $( q "${3}" )
    else
	echo "rsync ${rsyncopt}" $( q "rsync://${1}:${port}/${2}" ) $( q "${3}" )
    fi
    return
}

#-----------------------------------------------------------------------------
# This function gets IP addresses and executes a command for each.
#-----------------------------------------------------------------------------
function ipaddr {
    case $( exec uname -s ) in
	( SunOS )
	netstat -i -n | awk '{print $4;}'
	;;
	( Linux )
	ifconfig -a | awk '{if($1=="inet")print substr($2,6);}'
	;;
	( * )
	netstat -i -n | awk '{print $4;}'
	;;
    esac | egrep '^[0-9]*.[0-9]*.[0-9]*.[0-9]*$' | egrep -v '^0.|^127.' | head -2 | while read ipv4 more ; do
	showrsync "${ipv4}" "$@"
    done
    return
}

function showcmd {
    ipaddr "${2}" "${3}"
    showrsync "${1}" "${2}" "${3}"
    return
}

#-----------------------------------------------------------------------------
# Announce the shell commands that can be used to receive this data.
#-----------------------------------------------------------------------------
echo "${bar2}"
echo "# sending ${announce}"
echo "# paste ONE of the following commands in a remote shell to receive ..."

if [[ -d "${name}" ]]; then
    echo "${bar1}"
    showcmd "${hostname}" . .

    echo "${bar1}"
    showcmd "${hostname}" . "${b}"

    if [[ "${d}" != "${b}" && "${d}" != "${r}" ]]; then
	echo "${bar1}"
	showcmd "${hostname}" . "${d}"
    fi

    echo "${bar1}"
    showcmd "${hostname}" . "${r}"
else
    echo "${bar1}"
    showcmd "${hostname}" "./${f}" "${b}"

    s=$( exec basename "${d}" )
    s="${s}/${f}"
    if [[ "${s}" != "${b}" ]]; then
	echo "${bar1}"
	showcmd "${hostname}" "./${f}" "${s}"
    fi

    if [[ "${name}" != "${b}" && "${name}" != "${s}" && "${name}" != "${r}" ]]; then
	echo "${bar1}"
	showcmd "${hostname}" "./${f}" "${name}"
    fi

    echo "${bar1}"
    showcmd "${hostname}" "./${f}" "${r}"
fi

echo "${bar1}"
echo "# press ^C here when done"
echo "${bar2}"

#-----------------------------------------------------------------------------
# Start rsync in daemon mode.
#-----------------------------------------------------------------------------
opt=( --port="${port}" )
[[ -z "${listen}" ]] || opt=( --address="${listen}" "${opt[@]}" )

s="DONE"
trap 's="SIGINT ... DONE"' INT
trap 's="SIGTERM ... DONE"' TERM
rsync --daemon --no-detach "--config=${conffile}" "${opt[@]}"
rm -f "${conffile}" "${lockfile}"
echo "${s}"

  rsend.sh (9.6 KiB, 128 hits)

臉書回應

篇回應

關於 窮苦人家的小孩

In every democracy, the people get the government they deserve. ~Alexis de Tocqueville
上一篇:
下一篇:

您可能會喜歡

在VMware快速部署CentOS 7或是Windows 10

首先說CentOS,下載osb …

3 回應

  1. 太棒了 😀 😀 😀 😀 😀 😀 😀 😀
    收起來放 (Y) (Y) (Y) (Y) (Y) (Y)
    無痛搬家 是重點 XD XD XD XD XD XD XD

  2. 太厉害了,收藏了!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料