Shell Script for Remote Database Subversion Checkin
.The MySQL backup script I posted the other day backs up the databases on the local machine. I have a case where the local machine doesn’t have the subversion client, and so I need to pull them onto a machine that does, and check them in.
First, the setup section:
#!/bin/sh pathname=/path/to/backup/directory prefix=$1 svnprefix=$2 svnpath=/path/to/subversion/sandbox/$svnprefix idfile=/path/to/ssh/key filename=$prefix"_backup_latest.sql" remotefile=remotehost:/path/to/backup/directory/$filename.gz
The first two arguments are the prefix for the backup file (which in this script always end with “_backup_latest.sql”) and the directory within the subversion sandbox that that particular backup file should go into.
The idfile
variable holds the location of the public key that rsync will use to log on to the server without requiring password entry. I used this article to help me with that: Using ssh-agent with ssh.
This script uses timestamps to check for file modifications. The next step is to get those timestamps:
oldtimestamp=0 if [ -r $pathname/$filename.gz ]; then oldtimestamp=`ls -alT $pathname/$filename.gz | awk '{print $6$7$8$9}'` fi rsync -auv -e "ssh -i $idfile" $remotefile $pathname newtimestamp=0 if [ -r $pathname/$filename.gz ]; then newtimestamp=`ls -alT $pathname/$filename.gz | awk '{print $6$7$8$9}'` fi
ls -alT
gives the full time info for the file, and awk '{print $6$7$8$9}'
gives a single string for it. The rsync line is keeping time data from the remote file and using the specified public key file.
Next, compare the two timestamps and do the commit if they’re different:
if [ $oldtimestamp != $newtimestamp ]; then cp $pathname/$filename.gz $pathname/"temp_"$filename.gz gzip -d temp_$filename.gz -o $filename mv $filename $svnpath svn add $svnpath/$filename svn ci $svnpath/$filename -m "" fi
I have the actual commit commented out in my version, because I have a separate file that does one commit after all the database files are pulled over.
Full script:
#!/bin/sh pathname=/path/to/backup/directory prefix=$1 svnprefix=$2 svnpath=/path/to/subversion/sandbox/$svnprefix idfile=/path/to/ssh/key filename=$prefix"_backup_latest.sql" remotefile=remotehost:/path/to/backup/directory/$filename.gz oldtimestamp=0 if [ -r $pathname/$filename.gz ]; then oldtimestamp=`ls -alT $pathname/$filename.gz | awk '{print $6$7$8$9}'` fi rsync -auv -e "ssh -i $idfile" $remotefile $pathname newtimestamp=0 if [ -r $pathname/$filename.gz ]; then newtimestamp=`ls -alT $pathname/$filename.gz | awk '{print $6$7$8$9}'` fi if [ $oldtimestamp != $newtimestamp ]; then cp $pathname/$filename.gz $pathname/"temp_"$filename.gz gzip -d temp_$filename.gz -o $filename mv $filename $svnpath svn add $svnpath/$filename svn ci $svnpath/$filename -m "" fi