LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   prevent rsync from running if another instance is running - how? (http://www.linuxquestions.org/questions/linux-software-2/prevent-rsync-from-running-if-another-instance-is-running-how-871810/)

van_Zeller 03-29-2011 06:38 PM

prevent rsync from running if another instance is running - how?
 
Hello all,

On my crontab there is an entry to run a script I wrote that backs up my local files to my remote server. It was working great, running once a day. However, I now want to make the script run every hour.

The problem is that if there are too many files to transfer (Example: I ripped a DVD in the last hour) the actual backup may take longer than 60 minutes.

So I want to modify the script to check if my backup script, based on rsync, is running. If rsync is still running then do nothing and exit the script. If it is not running, execute the script as normal.

Any help or pointer is much appreciated. Thanks

Vasco

unSpawn 03-29-2011 08:01 PM

As far as fish goes try 'pgrep rsync >/dev/null 2>&1 && exit 1' or if a fishing rod is more your style:
Code:

function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html
http://www.tldp.org/LDP/abs/html/
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq
http://wooledge.org/mywiki/BashPitfalls"; }


chrism01 03-30-2011 08:11 PM

Another option is to daemonize your backup script and just make it wait 1 hr at the bottom of the loop ie at the end of the last backup.

van_Zeller 04-11-2011 06:06 AM

Hello all,

First of all thank for the replies. Sorry it took me time to come back here, but school has been killing me.

I've decided to go with chrism01's suggestion: I'm going to modify the script behaviour. Right now, it runs at 3 am for about 1 min+the transfer times. I'm going to modify this so that it is called on startup. I will put the script inside a loop that waits for 1h between each run. Like so:

loop start

wait 1h

run the backup script

loop end

In the future, as I learn more bash scripting, I want to change the behaviour again: instead of running the script every hour no matter what, I want it to run every hour ONLY if the files have been modified in that last hour. Here's my alrgorythm:

loop start:

wait 1 hour;

Question: have the files been modified in the last hour?

yes -> run the backup script
no -> do nothing


loop end

Thanks for the help. I'm marking the thread as solved, but I'll be back here to post my script :D

chrism01 04-12-2011 09:06 PM

For that, you could use diff each time, or generate md5sum for each file as you backup and compare at the next loop.
However, by default, rsync does only transmit differences, so maybe you don't need to worry about it?

Depends whether you have one backup dir, in which case rsync will take care of it for you, or a separate one for each hour, in which case diff or md5sum would work.

van_Zeller 04-13-2011 03:43 AM

Quote:

Originally Posted by chrism01 (Post 4322825)
For that, you could use diff each time, or generate md5sum for each file as you backup and compare at the next loop.
However, by default, rsync does only transmit differences, so maybe you don't need to worry about it?

Depends whether you have one backup dir, in which case rsync will take care of it for you, or a separate one for each hour, in which case diff or md5sum would work.

Hi,

I'm affraid I didn't understand what you meant, could you clarify, please?

My backup script is composed of 6 sequential rsync commands, one for each dir. Assuming there is nothing to back up, then the script takes around 30-45 seconds, but causes some slowdown during that time. My objective is to prevent this slowdown, unless needed...but perhaps this is out of my league, I don't know.

Not worrying about it is definitely a possibility! In fact at this point the script works so well, It's mostly for the fun of it that I'm doing it :D


All times are GMT -5. The time now is 05:29 AM.