Stangoesagain
Stangoesagain

Reputation: 75

Can rsync use shorthand/alias for an address?

I often rsync files to the same directory on the remote machine. Is it possible to give it an alias so that I don't have to type it in full every time?

Upvotes: 0

Views: 2046

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84579

Yes. Generally I will set up short scripts that define the rsync options and src and dest directories or perhaps arrays containing the directories of interest, for transferring files between hosts I admin. I usually simply pass the file to transfer src and dest files as arguments to an alias for that host (so I avoid having to type the hostname, etc..). Just make sure the script is executable, and define the alias in your .bashrc. It works fine. (I generally use rs_host for sending to a remote host, and aliases of rsf_host (the f indicating 'from').

For example, say I have a host valkyrie.3111skyline.com that is reachable only within my LAN. I so a lot of mirroring, and storage of software there for updating the kids computers as well as backups of my stuff. While I have backup scripts for bulk directories, sometimes I just was to shoot a file to the server or get a file from it. I don't like to type, so I make that process as short as possible. To do that I set of aliases (as described above) with rsvl to rsync to valkyrie and rsfvl to rsync from valkyrie. If I have updated my .bashrc and I want to send it to valkyrie, I only want to type:

rsvl ~/.bashrc

Similarly, if I want to get a file from valkyrie (say some C file in ~/dev/src-c/tmp/getline_ex.c and transfer it to my current directory, I only want to type:

rsfvl ~/dev/src-c/tmp/getline_ex.c

I want the scripts to take it from there. Since I know there can be corner cases and I often want to confirm a transfer involving a large number of files, I have the scripts accept the -n (--dry-run) option so that the scripts will show what will be done before they actually do it. (handy when your not quite sure)

I keep my rsync scripts in ~/scr/net (not too creative, I know...). In my .bashrc, the aliases are simply:

alias rsvl='/home/david/scr/net/rsyncvl.sh'
alias rsfvl='/home/david/scr/net/rsyncfvl.sh'

The scripts themselves are just quick and dirty helper scripts to get rsync to do as I want, e.g. the rsyncvl.sh scritpt (to valkyrie) is:

#!/bin/bash --norc

desthost=valkyrie.3111skyline.com
excl='--exclude=.~* --exclude=*.class'

usage() {       ## simple usage function to display usage
cat >&2 << USG

  usage: ${0##*/} [[-n|--dry-run|'options'] src [dest (~/Documents)]

for  

  rsync [-uai[n]]'options' \$src ${desthost}:\$dest [${desthost}:~/Documents]

USG

    exit 0
}

## test for '-h' or '--help'
[ -z "$1" ] || [[ "${1:0:3}" =~ '-h' ]] && usage

## test for additional options passed to rsync
if test "${1:0:1}" == '-'; then

  # options present so src is $2, dest is $3
  src="$2"
  destfn=${3:-~/Documents}
  dest="${desthost}:${destfn}"

  # preserve '-n' capability for '-uain' test
  if test "$1" == '-n' || test "$1" == '--dry-run'; then
    echo "[DRY RUN]"
    opts='-uain'
  else
    # use options supplied for rsync
    opts="$1"
  fi

else

  # default use
  src="$1"
  destfn=${2:-~/Documents}
  dest="${desthost}:${destfn}"
  opts='-uai'

fi

## output the text of the comman executed
echo "rsync $opts $excl ${src} ${dest}"

if [[ "$src" =~ '*' ]]; then
  rsync $opts $excl ${src} "${dest}"   # allow file globbing expansion
else
  rsync $opts $excl "${src}" "${dest}"
fi

And the script to get files from valkyrie:

#!/bin/bash --norc

src="$1"        ## source file on srchost
dest="${2:-.}"  ## destination on local machine
srchost=valkyrie.3111skyline.com

usage() {       ## simple usage function to display usage
cat >&2 << USG
  Usage: ${0##*/} src dest ($HOSTNAME) --> rsync -uav [${srchost}:]\$src \$dest
USG

    exit 0
}

[[ -z "$1" ]] && usage

## allow '.' shorthand to set pwd as source/dest dir
[[ $src == '.' ]] && src="$(pwd)" && {
  dest="$(pwd)"
  dest="${dest%/*}"
}
[[ $src == './' ]] && src="$(pwd)/"

[[ $src =~ '/' ]] || src="$(pwd)/$src"

srcstr="${srchost}:${src}"  ## form source string

## echo command executed and execute
echo -e "\n  rsync -uav ${srcstr} ${dest}\n"
rsync -uai "${srcstr}" "${dest}"

They can always be improved upon, and I wasn't all that particular when I wrote the first one a decade or so ago (which somehow has been duplicated for all the other hosts I have now...) So feel free to improve and tailor them to your needs. For basic use, all you need to do is change the hostname to the hostname of your remote machine and they will work fine. Both will display a short Usage if run without arguments and the to valkyrie script will respond to -h or --help as well. Good luck with your transfers.

Upvotes: 2

i.kimiko
i.kimiko

Reputation: 24

You can add alias like that alias myrsync='rsync -av -e "ssh" user@server:/path/to/sync /path/to/local/destination' and You can add that to your .bashrc file.
After that you type "myrsync" and command "rsync -av -e "ssh" user@server:/path/to/sync /path/to/local/destination" will be execute.

Upvotes: -1

Related Questions