diff --git a/README.md b/README.md index 4b7a293..ec8f4e1 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,29 @@ Copy the `vdn-completion.bash` file in `/usr/local/share/bash-completion/complet cp completion/vdn-completion.bash /usr/local/share/bash-completion/completions/vdn grep 'complete -F \w* [a-z-]*' -- completion/vdn-completion.bash | awk '{ print "/usr/local/share/bash-completion/completions/"$NF }' | xargs -I {} ln -s /usr/local/share/bash-completion/completions/vdn {} ``` + +## `vdn-rsync` + +Copy files between the host and the guest system using *rsync*. + +### With a script + +Use the `bin/vdn-rsync` script to have a nice interface with *rsync*. + +```bash +vdn-rsync -av src root@bigboss: +``` + +Add it to your path: + +```bash +PATH="$PATH:~/path/to/vdn-tools/bin/" +``` + +### Without a script + +Specify the `vdn-ssh` command with `rsync`. + +```bash +rsync -av src -e vdn-ssh root@bigboss: +``` diff --git a/bin/vdn-rsync b/bin/vdn-rsync new file mode 100755 index 0000000..4971bf1 --- /dev/null +++ b/bin/vdn-rsync @@ -0,0 +1,50 @@ +#!/bin/bash + +set -eu + +VDN_PATH=$(readlink -f $(dirname $(command -v vdn))/..) +. $VDN_PATH/bin/functions.sh +GUEST_OWNER=$USER + +rewrite_connection() { + if [[ $1 != *:* ]]; then + connection="$1" + else + connection=${1%:*} + local path=${1#*:} + local user=${connection%@*} + guest=${connection#*@} + connection="${user}@localhost:${path}" + fi +} + +determine_redir_port() { + local redirs="$TMPDIR/vdn-$guest-${GUEST_OWNER}-redirs" + [[ ! -e $redirs ]] && error "Impossible de joindre $guest" + sshRedirPort=$(awk -F ':' '$1 == "tcp" && $3 == "22" {print $2}' "$redirs") + [[ -n $sshRedirPort ]] || error "Aucune redirection vers le port 22 du système $guest" +} + +declare -a options=() +declare -a srcs=() +dest='' +guest='' +while [[ $# -ne 0 ]]; do + if [[ $1 == -* ]]; then + options+=("$1") + elif [[ $# -ne 1 ]]; then + rewrite_connection "$1" + options+=("$connection") + else + rewrite_connection "$1" + dest="$connection" + fi + shift +done + +if [[ $guest ]]; then + determine_redir_port +else + error 'Utiliser rsync directement' +fi +rsync "${options[@]}" -e "ssh -g -p $sshRedirPort" "${srcs[@]}" "$dest"