Pushout profile

Published: Fri 19 August 2011
By Sirex

In Linux.

I had a problem, normally i checkout an svn folder of my dotfiles and create symlinks to these files, however sometimes this isn't possible, as the end machines dont have svn installed, or cant reach the internet. I've found recently a more useful use case is being able to checkout to a single machine and push out the dot files from there, having 1 master set of files, and one master ssh machine per location. Here's what I came up with, feel free to reuse if it helps you.

  
#!/bin/bash
#########################################################################################
# Small script to push out some common files to various machines in differing locations #
#########################################################################################

work_machines=`echo myusername@{hostnameA,hostnameB}`
home_machines=`echo myuser@{hostnameA,hostnameB}`

#####################
### NO EDIT POINT ###
#####################

function update_progress () {
    echo -ne '.'
}

echo -n "At Work (1) or Home (2): "
read location

set=""
if [ "$location" == "1" ]; then
    set=$work_machines
    echo -e "\n\n*** Syncing work machines ***\n\n"
elif [ "$location" == "2" ]; then
    set=$home_machines
        echo -e "\n\n*** Syncing home machines ***\n\n"
else
    echo "Error, please enter valid input"
        exit 1
fi

for host in $set
do
    # Check host is reachable   
    ping -c 1 `echo "$host" | cut -d '@' -f2` > /dev/null 2>&1
    if [ "$?" -ne 0 ]; then
        echo -e "Host `echo $host | cut -d '@' -f2` \t\t unreachable, skipping"
        continue
    fi


    echo -ne "Syncing to ($host) \t["

    ssh "$host" mkdir .ssh 2> /dev/null && update_progress
    if [ "$location" == "1" ]; then
        rsync global_authorized_keys_work "$host":.ssh/authorized_keys && update_progress
    elif [ "$location" == "2" ]; then
        rsync global_authorized_keys_home "$host":.ssh/authorized_keys && update_progress
    fi

    rsync .bashrc "$host":. && update_progress
    rsync .vimrc "$host":. && update_progress

    echo -ne "]\t: Sync to `echo "$host" | cut -d '@' -f2` finished\n"
done

social