Shell Script To Backup MySql Database Server

Posted on in Categories Backup, MySQL last updated March 12, 2010

Save the script and run it as a cron job:
@daily /path/to/yourmysql.sh

55 comment

  1. Does it output some information if something goes wrong? i´m trying to write to a logfile, just wondering it outputs success or failure results? Sorry, i just lack the skills to tell if it does so.

    TIA,
    Rodrigo

  2. This script is a lifesaver, thank you.

    I do have a quick question, for the IGGY, do you put the databases you do not want backed up just separated by commas?

  3. For some reason when I tried this it seemed to work — it created the .gz files in my backup directory. But when I tried an experiment of dropping one of the databases and then using the backed-up .gz file to restore from (via cpanel utility), it created an empty database with no tables in it. Not sure why :-(

      1. If your file name is db2.11_02_35am.gz, then use the following commands to reinstall the database:

  4. Thanks so much for taking the time to put up this script; you’re a real life-saver. I realised today (after a big scare with fsck) that I have no facility for backing up my databases on my development machine! I shall be adding this forthwith.

  5. Thank you for the handy script.

    At least on our Fedora 10 installation, the gzip binary looks for an env var GZIP and uses it as a default argument list if it’s set.

    So this
    $GZIP -9
    is equivalent to this (for our binaries)
    /bin/gzip /bin/gzip -9

    Since I wasn’t running the script as root, this manifested itself as a somewhat misleading permissions error that took a while to track down since I mistook the /bin/gzip.gz as a reference to a helper binary (like fsck.ext2, for instance):
    gzip: /bin/gzip.gz: Permission denied

    Could I request that something besides GZIP be used (e.g. GZIPBIN, GZ)?

    Thanks!

  6. if i need to backup each database in seperate file and want to know whether the databse dump was succesful or not.

    could anyone please help with that

  7. Nice Post.. I have use this post as you say and It work exactly as I want. but Is it possible to copy backup directory to other server for security If this server is totally crash? Only daily folder has to copy to other server.

  8. Just a note that will hopefully help others. I’m just starting to learn shell scripting all my other ones have been using phpcli so take this with a grain of salt. I was getting a bunch of these errors.

    ==: unexpected operator

    I had to change these lines and use the single = comparison instead of ==

    [ “$db” = “$i” ] && skipdb=1 || :
    done
    fi

    if [ “$skipdb” = “-1” ] ; then

    1. I would think that changing the operator to = would be assigning the “$i” to “$db”

      From my knowledge of bash scripting, or the use of bourne shell commands, it is necessary for the use of == for comparisons as well as the spaces around the subscript operators.

      Did changing those to = instead of == allow it to run properly?

      1. Zeb is right, interesting, the double equal worked for me in Ubuntu 16.04 but did not work in Ubuntu 14.04 (not the same bash versions).
        Actually, to declare or fill a variable you should not leave any space like:
        $db=$i

        This script is still useful almost 2017 so thanks again.

  9. Incredible!!!!!!!

    I have wasted hours trying to do this in perl. One small copy and paste from your site and all of the mysql databases are now being automatically backed up. Thank you so much. You are worth your weight in gold.

  10. Just my one cent:
    run rsync daemon using the backup directory just to be sure you are playing safe. Is worthless to backup MySQL not having a copy in remote location in case of a server crash.

    1. this looks super cool, however can anyone explain me the meaning of

      MYSQL=”$(which mysql)”
      MYSQLDUMP=”$(which mysqldump)”
      CHOWN=”$(which chown)”
      CHMOD=”$(which chmod)”
      GZIP=”$(which gzip)”

      what does the which command do ? do we have to write tht

Leave a Comment