mysqldump with db in a separate file

前端 未结 8 1653
生来不讨喜
生来不讨喜 2020-12-02 15:02

I\'m writing a single line command that backups all databases into their respective names instead using of dumping all in one sql.

Eg: db1 get saved to db1.sql and

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 15:11

    Here an easy script that will:

    • dump all DB a compress the output -> SCHEMA_NAME.sql.gz
    • use [autocommit/unique_checks/foreign_key_checks] to speed up import
    • exclude default schemas

    File: Dump_all.sh

    How to use:
    ./Dump_all.sh -> will dump all DB
    ./Dump_all.sh SCHEMA_NAME -> will dump SCHEMA_NAME DB

    #!/bin/bash
    MYSQL_USER="root"
    MYSQL_PASS="YOUR_PASS"
    
    echo "-- START --"
    
    echo "SET autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;" > tmp_sqlhead.sql
    echo "SET autocommit=1;SET unique_checks=1;SET foreign_key_checks=1;" > tmp_sqlend.sql
    
    if [ -z "$1" ]
      then
        echo "-- Dumping all DB ..."
        for I in $(mysql -u $MYSQL_USER --password=$MYSQL_PASS -e 'show databases' -s --skip-column-names); 
        do
          if [ "$I" = information_schema ] || [ "$I" =  mysql ] || [ "$I" =  phpmyadmin ] || [ "$I" =  performance_schema ]  # exclude this DB
          then
             echo "-- Skip $I ..."
           continue
          fi
          echo "-- Dumping $I ..."
          # Pipe compress and concat the head/end with the stoutput of mysqlump ( '-' cat argument)
          mysqldump -u $MYSQL_USER --password=$MYSQL_PASS $I | cat tmp_sqlhead.sql - tmp_sqlend.sql | gzip -fc > "$I.sql.gz" 
        done
    
    else
          I=$1;
          echo "-- Dumping $I ..."
          # Pipe compress and concat the head/end with the stoutput of mysqlump ( '-' cat argument)
          mysqldump -u $MYSQL_USER --password=$MYSQL_PASS $I | cat tmp_sqlhead.sql - tmp_sqlend.sql | gzip -fc > "$I.sql.gz" 
    fi
    
    # remove tmp files
    rm tmp_sqlhead.sql
    rm tmp_sqlend.sql
    
    echo "-- FINISH --"
    

提交回复
热议问题