Skip to content

bash function to sync to and from a remote host by using rsync and ssh

I quickly created two bash functions to use rsync and ssh for syncing directories from the local host to a remote host and vice versa.

The two methods are available on my shell function repository. The two methods are called net_bazzline_sync_from_host and net_bazzline_sync_to_host. Enjoy it :-).

####
# Uses rsync with ssh to copy data from remote to local host
#
# @param string $1 - remote user
# @param string $2 - remote host
# @param string $3 - source path on remote host
# @param string $4 - destination path on local host
#
# @author stev leibelt 
# @since 2013-07-05
####
function net_bazzline_sync_from_host ()
{
    if [[ $# -eq 4 ]]; then
        USER="$1"
        HOST="$2"
        SOURCE="$3"
        DESTINATION="$4"
    else
        echo 'invalid number of variables provided'
        echo 'command user host source destination'
    fi

    rsync -avz -e ssh $USER@$HOST:$SOURCE $DESTINATION
}

####
# Uses rsync with ssh to copy data from local to remote host
#
# @param string $1 - remote user
# @param string $2 - remote host
# @param string $3 - source path on local host
# @param string $4 - destination path on remote host
#
# @author stev leibelt 
# @since 2013-07-05
function net_bazzline_sync_to_host ()
{
    if [[ $# -eq 4 ]]; then
        USER="$1"
        HOST="$2"
        SOURCE="$3"
        DESTINATION="$4"
    else
        echo 'invalid number of variables provided'
        echo 'command user host source destination'
    fi

    rsync -avz -e ssh $SOURCE $USER@$HOST:$DESTINATION
}

tool - Bazzline_Controller_Plugin_Auth - Zend Framework Controller Plugin - now on github.com

I just released my first project on github.com. As mention in the headline, it is called Bazzline_Controller_Plugin_Auth. It is a simple plugin which tests if the called url is a "logged in user" only url or not. If it is "logged in only", the user is redirected to the login. The previously called url is saved in the session. After a successfull login, the user gets redirected to this saved url. All can be configured in a config file.

This plugin is only for simple access controll by "user is logged in" or "user is not logged in". The ACL stuff is still in the pipeline and should not - to follow the KISS principle - be inside an auth plugin.

While setting up this project on github, i had some problems while getting a valid connection to github. I followed the howto but not "pressed enter" when asked for a file to store the key. Thats why:

ssh -T git@github.com
failed. But when i added the -i option like:
ssh -T git@github.com -i path/to/my/private/key
everything runs smoothly. After a short term of searching, i found the ssh issues page on gitub.com. The simple solution is:
Create or open the file at ~/.ssh/config Add the following lines:
Host github.com User git Hostname github.com PreferredAuthentications publickey IdentityFile /path/to/my/private/key
After that, the
git push -u origin master
runs without any errors.

howto - mount filesystem via ssh using sshfs

First of all, you have to install "sshfs". After that you just have to type.

sshfs $user@$host:/path/to/dir /path/to/mount
If fuse throws an error of permission, you have to add your user to the group fuse by typing:
usermod -a -G fuse $user
After you re-logged-in you can see by typing "groups" in your shell that everything is done fin. Do not want to logoff? Try
su $user
in your shell. This is a new login and so your new group is available now. Just another hint, if you want to access symbolic links, try:
-o follow_symlinks
and/or
-o transform_symlinks
The man page will help you to understand what you are doing. Now put everything as an alias in your bashrc (or something similar) and you are done :-).
alias mountMyShare="sshfs $user@host:/path/to/dir /path/to/mount -o follow_symlinks"; alias umountMyShare="fusermount -u /path/to/mount";
Happy mounting and unmounting.