Simple rsync Helper Script

April 8, 2015

If you have multiple servers that you send files over rsync to regularly then this simple little rsync script might be helpful to you.

I love using rsync, but mostly I use the same options and only a few destinations. To send a file to my homelab server, I would have to type:

rsync -avzP -e "ssh -p 1234" /location/of/source/file.txt user@notmyrealdoamin.com:/location/of/destination/file.txt

True, it’s not that much to remember, but I wanted a faster way to do it. I also wanted to specify a default “Uploads” folder so that I could just send the file and figure out where to put it later. With my script alias’d to ‘rs’ in by .bashrc, all I have to type now is:

rs /location/of/source/file.txt servernickname /destination/file.txt

And if I don’t feel like specifying a location, I can just do:

rs file.txt servernickname

This will send the file to the ~/Uploads folder on the specified server.

The script is pasted below. Just save it somewhere convenient for you. I keep mine in ~/Scripts.

#!/bin/bash

#######################################
#
# A simple rsync helper script to send files to one of several predefined servers.
# rsync is set to use -avzP, if you want different options enter them below
# Check man rsync for a full list of options
# use: rs.sh sourcefile servername destination
# if no destination is specififed, the file will be placed in the ~/Uploads folder on the server
#
# Credit: jay-baker.com
#######################################

#########################################
#
# Defines the sourcefile variable as the first term entered after the script is called,
# and the servername variable as the second term entered after the script is called.
#
#########################################

sourcefile="$1"
servername="$2"

##########################################
#
# This will convert the source file to just its file name, stripping away the directories, but leaving the extension.
# This is useful if we don't feel like specifying a destination on the server. 
#
##########################################

filename="${sourcefile##*/}"

##########################################
#
# This basically says that if we don't specify a destination on the server to use, then make the 
# default destination be the ~/Uploads folder, preserving the orginal filename.
# If you want to specify a different default folder, then change the "destination=~/Uploads/$filename"  
# line to "destination=/$filename".
#
##########################################

if [ "$#" = "2" ]; then
	destination=~/Uploads/$filename
 else
	destination="$3"
fi

##########################################
#
# If you have multiple servers to upload to then this is where you would enter them.
# Just enterer your own server information below. Check out
# the example below for guidance. You can give each of your servers a simple nickname.
# Be sure to specifiy which port your server listens on for ssh. You didn't leave it set to
# port 22 did you? ;) Note that you can also change which options rsync uses here. 
# You could set different options for each server.
#
# Example Server
# if [ "$servername" = "" ]; then
# 	  echo -e "Sending $filename to $servername ... PewPew!"
#	  rsync -avzP -e "ssh -p " $sourcefile @:$destination
# fi
#
##########################################

#Server1
if [ "$servername" = "server1"  ]; then
	 echo -e "Sending $filename to $servername ... PewPew!"
	 rsync -avzP -e "ssh -p 2222" $sourcefile user@server1.com:$destination
fi

#Server 2
if [ "$servername" = "server2" ]; then
	 echo -e "Sending $filename to $servername ... PewPew!"
	 rsync -avzP -e "ssh -p 2222" $sourcefile user@server2.org:$destination
fi

#Server 3
if [ "$servername" = "server3" ]; then
	 echo -e "Sending $filename to $servername ... PewPew!"
	 rsync -avzP -e "ssh -p 3333" $sourcefile user@server3.server.com:$destination
	
fi

exit 0

Don’t forget to make it executable with:

sudo chmod +x rs.sh

And if you want to add an alias for it, just open up your ~/.bashrc in your text editor of choice and add the following line:

alias rs='/home/user/Scripts/rs.sh'

Replace user with your username and the rest with where you saved your script to.

This was a fun little project I did last night. I think I’ll add a little progress bar to it next.