locvfx

Dump all databases using SH

May 25th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.39 KB | None | 0 0
  1. #!/bin/bash
  2. MYSQL_USER="root"
  3. MYSQL_PASS="YOUR_PASS"
  4.  
  5. echo "-- START --"
  6.  
  7. echo "SET autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;" > tmp_sqlhead.sql
  8. echo "SET autocommit=1;SET unique_checks=1;SET foreign_key_checks=1;" > tmp_sqlend.sql
  9.  
  10. if [ -z "$1" ]
  11.   then
  12.     echo "-- Dumping all DB ..."
  13.     for I in $(mysql -u $MYSQL_USER --password=$MYSQL_PASS -e 'show databases' -s --skip-column-names);
  14.     do
  15.       if [ "$I" = information_schema ] || [ "$I" =  mysql ] || [ "$I" =  phpmyadmin ] || [ "$I" =  performance_schema ]  # exclude this DB
  16.      then
  17.          echo "-- Skip $I ..."
  18.        continue
  19.       fi
  20.       echo "-- Dumping $I ..."
  21.       # Pipe compress and concat the head/end with the stoutput of mysqlump ( '-' cat argument)
  22.      mysqldump -u $MYSQL_USER --password=$MYSQL_PASS $I | cat tmp_sqlhead.sql - tmp_sqlend.sql | gzip -fc > "$I.sql.gz"
  23.     done
  24.  
  25. else
  26.       I=$1;
  27.       echo "-- Dumping $I ..."
  28.       # Pipe compress and concat the head/end with the stoutput of mysqlump ( '-' cat argument)
  29.      mysqldump -u $MYSQL_USER --password=$MYSQL_PASS $I | cat tmp_sqlhead.sql - tmp_sqlend.sql | gzip -fc > "$I.sql.gz"
  30. fi
  31.  
  32. # remove tmp files
  33. rm tmp_sqlhead.sql
  34. rm tmp_sqlend.sql
  35.  
  36. echo "-- FINISH --"
  37.  
  38.  
  39. ################
  40. ##How to use:
  41. ##./Dump_all.sh -> will dump all DB
  42. ##./Dump_all.sh SCHEMA_NAME -> will dump SCHEMA_NAME DB
  43. #############################
Add Comment
Please, Sign In to add comment