abazoskib Posted July 22, 2009 Share Posted July 22, 2009 i want my bash script to check if a lock file exists, if it exists, the script should keep checking till the lock file is gone, and then proceed. so far, ive made it so the script exits if the lock file is found. whats the change i need to make? i know i need a while loop, but im still learning bash syntax so hopefully someone could shed some light on this topic for me if [ -f /usr/xxxx/xxxx.lock ] then exit 1 else #!/bin/sh DATE=`date +%Y-%m-%e` mysqldump -uxxx -pXXXXX XXX_db xx_tbl > /ebs/MYSQL_BACKUPS/xx_$DATE.sql mysqldump -uxxx -pXXXXX XXX_db xx_tbl > /ebs/MYSQL_BACKUPS/xx_$DATE.sql cd /ebs/MYSQL_BACKUPS tar -czf mysql_backup_$DATE.sql.tar.gz xx_$DATE.sql xx_$DATE.sql rm -f *.sql fi Quote Link to comment Share on other sites More sharing options...
trq Posted July 22, 2009 Share Posted July 22, 2009 Not tested. #!/bin/sh while true ; do attemptbackup; done function attemptbackup() { if [ -f /usr/xxxx/xxxx.lock ] then return else DATE=`date +%Y-%m-%e` mysqldump -uxxx -pXXXXX XXX_db xx_tbl > /ebs/MYSQL_BACKUPS/xx_$DATE.sql mysqldump -uxxx -pXXXXX XXX_db xx_tbl > /ebs/MYSQL_BACKUPS/xx_$DATE.sql cd /ebs/MYSQL_BACKUPS tar -czf mysql_backup_$DATE.sql.tar.gz xx_$DATE.sql xx_$DATE.sql rm -f *.sql fi exit } Quote Link to comment Share on other sites More sharing options...
abazoskib Posted July 22, 2009 Author Share Posted July 22, 2009 so basically 'while true' will keep looping until the lock file is gone, the commands are executed and an exit is given? Quote Link to comment Share on other sites More sharing options...
trq Posted July 22, 2009 Share Posted July 22, 2009 Yeah. Your not going to try running this as some sort of service though are you? Quote Link to comment Share on other sites More sharing options...
abazoskib Posted July 22, 2009 Author Share Posted July 22, 2009 i want to use it for backing up two specific tables of a mysql db every night with cron..not ok? Quote Link to comment Share on other sites More sharing options...
trq Posted July 22, 2009 Share Posted July 22, 2009 Should be fine. What exactly is the lock file for? I'm just a little worried the you may get into an infinite loop. Quote Link to comment Share on other sites More sharing options...
abazoskib Posted July 22, 2009 Author Share Posted July 22, 2009 the lock file is created by another script while running. the script that creates the file also removes the file at the end of completion. and the reason i check for it is because the script that creates the script works hourly, and adds to the tables i am backing up. mysqldump locks the tables, so i cant risk losing data. Quote Link to comment Share on other sites More sharing options...
MyUser Posted August 7, 2009 Share Posted August 7, 2009 Why you do not use zyxBackup? It is a high-customizable bash script that can do a lot of things like encryption, uploading to ftp. There are a lot of backup bash scripts You can read more about it here Secure backup on remote server using zyxBackup and GPG Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.