User Tools

Site Tools


2019:01:01:using-tar-to-duplicate-directories-between-servers

Using tar to duplicate directories between servers

Introduction

Ordinarily, rsync is the go-to for transferring directories (or files) with attributes from a remote server to a local machine, or visa versa. But, what if you want to use a Bastion host to directly transfer from an old server to a new server? If you try it with rsync, you might use something to the effect of:

sUser='user'
sHost='server-1.example.com'
sPath='/home/user/www.example.com/'
 
dUser='user'
dHost='server-2.example.com'
dPath='/home/user/www.example.com/'
 
rsync \
	--archive \
	--verbose \
	--progress \
	--partial \
	--delete \
	--delete-excluded \
	--links \
	"${sUser}@${sHost}:${sPath}" \
	"${dUser}@${dHost}:${dPath}" \
	;

Try this, and rsync will throw an error: The source and destination cannot both be remote.

I'd love to recommend Rclone, but at the time of writing for this article, maintaining attributes during transfer is not supported. 1) 2)

Implementation

So, this is where tar over SSH comes into play! The following will yield the desired results, albeit employing a different technique:

Please note: if you're not interested in retaining file & group ownership, you can use this line instead:

"mkdir -pv '${dPath}' && cd '${dPath}' && tar -xzvf - . && chown -Rv ${dUser}: ."
sUser='user'
sHost='server-1.example.com'
sPath='/home/user/www.example.com/'
 
dUser='user'
dHost='server-2.example.com'
dPath='/home/user/www.example.com/'
 
ssh \
	"${sUser}@${sHost}" \
	"cd '${sPath}' && tar -czvf - ." \
	| \
	ssh \
		"${dUser}@${dHost}" \
		"mkdir -pv '${dPath}' && cd '${dPath}' && tar -xzvf - ." \
	;

References

2019/01/01/using-tar-to-duplicate-directories-between-servers.txt · Last modified: 2019/07/12 12:25 by Louis T. Getterman IV