• Blogs (9)
    • 📱 236 - 992 - 3846

      đź“§ jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • Bash: MySQL Database backup

    Blogs20112011-05-31


    Here is a complete example of MySQL Database backup script (dump out & compress):

    #!/bin/bash
    # 1. if exists, quit
    # 2. if not, do the backup
    # 3. compress
    # put into cron job, every week based.
    DB="DATABASE"
    USER="test"
    PASS='test'
    DIR='/home/test/DBs/'
    
    DATE=`date '+%d%h%Y'`
    FILE="${DIR}/${DB}${DATE}.sql"
    ZFILE="${FILE}.gz"
    
    if [ -f ${ZFILE} ]; then
     echo "$ZFILE already exists."
     exit;
    fi
    
    mysqldump -u ${USER} -p"${PASS}" -h localhost ${DB} > ${FILE}
    gzip -f $FILE

    Put this script into crontab will make the backup automatically. such as:

    $ crontab -e
    0 0 * * 0 $HOME/bin/this_script.sh