Bob's Notepad

Notes on projects I have done and things I have learned saved for my reference and for the world to share

Tuesday, November 20, 2007

Using lock files in a script

When dealing with automated rsync backup scripts you sometimes run in to the issue of a large file taking longer than expected and cron launching another rsync -- eventually spiraling things down. The way to resolve this is create a lock file so that your script doesn't launch again if it is already running. Just put the following in your script under the #!/bin/sh statements:

LOCKFILE="/var/lock/rsync/lockfile"
if [ -f $LOCKFILE ]
then
echo Lock file exists...Exiting...
exit 0
else
touch $LOCKFILE
fi


This declares what the lock file is (you can make the file anything as long as the user running the script has access to read and write). Then it checks to see if the lockfile exists and exits if it does. If it doesnt exist, then it creates it.

All thats left is to add the following to the end of the script so that it cleanly deletes the lockfile after running:

rm -f $LOCKFILE
exit 0


Thanks to @linuxchic for helping out :)

Labels: , ,

Reference Link


Monday, October 22, 2007

Automated SSH login using keys

I always seem to forget how to do this when I need to. When setting up an automated backup you obviously don't want the script to ask for a password so you set up a key pair.

Machine sending the backups (must be logged in as the user that will be doing the backups):

  • ssh-keygen -t dsa -b 2048 -f /any/directory/filename


Then you copy the resulting filename.pub file (NOT the file with no extension) to the authorized_keys file on the receiving machine in the .ssh directory under the user that will receive the backups. If the authorized_keys file doesnt exist, just rename the file you copied... if it does exist, append it to that file.

If your using rsync, use this command:

rsync -e 'ssh -i /any/directory/filename' source/ user@host:/destination/

Labels: , , , , ,

Reference Link