easy backup of many MySQL databases and tables

  • What?
    At work we have a lot of databases and tables in them that we must have fresh backups on. Until now i used mysqldump to dump ALL databases
    and tables into one dumpfile. The size of that file is 1.8GB and growing. Fetching a table to be restored is a tedious task.

  • The stuff
    The solution to the problem is a simple bash script that first checks what databases exist and then in a loop use mysqldump to dump all the tables into
    separate date/timestamped files. Finally we gzip the files to save space.
    The script could easely be modified to backup an remote MySQL installation.


    #!/bin/bash
    
    mkdir $(date +%Y%m%d)
    cd $(date +%Y%m%d)
    DBS=`/database/mysql/bin/mysql -uTHEUSER -pTHEPASSWORD  -e"show databases"`
    for b in $DBS ;
    do
      echo Database $b
      TAB=`/database/mysql/bin/mysql -uTHEUSER -pTHEPASSWORD -e"show tables from $b"`
      for t in $TAB ;
      do
        /database/mysql/bin/mysqldump -uTHEUSER -pTHEPASSWORD -e -v -f -q -Q -K $b $t >  $b\_\_$t\__$(date +%Y%m%d_%T)_mysql
      done
    done
    gzip -9 *_mysql
    cd ..