20 mars 2012

Transférer des fichiers par "tar|gzip" à travers un lien "ssh"

Ci joint un exemple de commande pour transférer un grand nombre (ou un gros volume) de fichiers sans créer de ".tar.gz" intermédiaire (ici on transfère le $FILE de deux environnements nommés hraqua et situés sur deux machines distantes) :

tar cvf - /hraqua/file | gzip -c | ssh hraqua@1.2.3.4 "cd /;gunzip -c | tar -xvf -"

  • A exécuter sur la machine / environnement source
  • Derrière le "ssh" mettre le user et le nom (ou l'IP) de la machine / environnement cible
  • En fin de ligne entre quotes se trouve la commande exécutée sur la machine/ environnement cible
NB : le tar gnu des linux a une option de compression "-z" qui permet de se passer du " | gzip -c"

Exemple :
hraqua@old_srvhr:/hraqua> tar cvf - /hraqua/dir |gzip -c |ssh hraqua@new_srvhr "cd /hraqua; gunzip -c |tar -xvf -"
 a /hraqua/dir
 a /hraqua/dir/dir1
 a /hraqua/dir/dir1/dummy1.txt 1 blocks
 a /hraqua/dir/dir2
 a /hraqua/dir/dir2/dummy2.txt 1 blocks
 The authenticity of host 'new_srvhr (1.2.3.4)' can't be established.
 RSA key fingerprint is f1:c1:04:73:64:f9:a5:19:16:8b:06:21:9f:86:06:65.
 Are you sure you want to continue connecting (yes/no)? yes
 Warning: Permanently added '1.2.3.4' (RSA) to the list of known hosts.
 hraqua@new_srvhr's password:
 Warning: untrusted X11 forwarding setup failed: xauth key data not generated
 Warning: No xauth data; using fake authentication data for X11 forwarding.
 x /hraqua/dir
 x /hraqua/dir/dir1
 x /hraqua/dir/dir1/dummy1.txt, 39 bytes, 1 tape blocks
 x /hraqua/dir/dir2
 x /hraqua/dir/dir2/dummy2.txt, 37 bytes, 1 tape blocks

PS : Pour exécuter cette commande en batch (avec un "at now" ou un crontab) vous aurez besoin de paramétrer le ssh pour qu'il réalise une connexion sans authentification (fichier clef + .ssh/authorised_keys).

Aucun commentaire:

Enregistrer un commentaire