My bash script for syncing folders between two or three computers - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

My bash script for syncing folders between two or three computers

I have two desktops and a laptop. I copied my music into each of them from my flash drive, but since then I have added music from various places and they got badly out of sync. So I decided to use rsync and create a script to run once a month or so. I hope someone finds it useful.

The main problem is that it makes me enter my password five times. I'm trying to learn expect so I can automate that process, if any one can help me with that I would appreciate it. If you have any other improvements, such as making it capable of handling any number of computers, or reducing the amount of code, I would like to hear them as well.

I tried using RSYNC_PASSWORD, but apparently it and --password-file no longer work.

The script runs on mark, my fastest computer. I tried to use it in as many exchanges as possible and my old laptop, robby, in the least amount of exchanges possible.

[code]
#!/bin/bash

# sync -- uses rsync to sync the music folders of two or three computers #

# philipk88
# 2-8-11


#### Functions ####


main()
{
read -p 'What is the ip address of Marvin?>' marvin
read -p 'Is Robby connected to the network?[N/y]>' robbyExist
if [ $robbyExist = y ]
then
read -p 'What is the ip address of Robby?>' robby
remotefile="$marvin:$marvinPath"
toMark
fromMark
remotefile="$robby:$robbyPath"
toMark
fromMark
remotefile="$marvin:$marvinPath"
fromMark
echo "Copying is done"
exit
else
remotefile="$marvin:$marvinPath"
toMark
fromMark
echo "Copying is done"
exit
fi
}


# Displays a pretentious welcome message #
welcome()
{
echo " "
echo " "
echo '#############################################'
echo '# #'
echo '# Welcome to your music syncing program!! #'
echo '# Let the synchronization begin! #'
echo '# #'
echo '#############################################'
echo " "
echo " "
return
}

# Copies the files that only exist on the remote computer to the local file #
toMark()
{
rsync -vzr -e ssh philip@$remotefile /home/philip/Music
return
}

# Copies the files that only exist locally to the remote computer #
fromMark()
{
rsync -vzr /home/philip/Music/ -e ssh philip@$remotefile
return
}


#### Program ####


## Assign Variables ##

marvin=192.168.2.2
marvinPath=/home/philip/arch-share/music/
robby=192.168.2.4
robbyPath=/home/philip/music/

## Display a welcome message ##

welcome

## Call main and get to work ##

main


#### End ####
[/code]
Sign In or Register to comment.