Jump to content

bash script for backup


abazoskib

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/166921-bash-script-for-backup/
Share on other sites

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
}

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.

  • 3 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.