locvfx

dump all mysql tables into separate files automagically

May 25th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.06 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # dump-tables-mysql.sh
  4. # Descr: Dump MySQL table data into separate SQL files for a specified database.
  5. # Usage: Run without args for usage info.
  6. # Author: @Trutane
  7. # Ref: http://stackoverflow.com/q/3669121/138325
  8. # Notes:
  9. #  * Script will prompt for password for db access.
  10. #  * Output files are compressed and saved in the current working dir, unless DIR is
  11. #    specified on command-line.
  12.  
  13. [ $# -lt 3 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && exit 1
  14.  
  15. DB_host=$1
  16. DB_user=$2
  17. DB=$3
  18. DIR=$4
  19.  
  20. [ -n "$DIR" ] || DIR=.
  21. test -d $DIR || mkdir -p $DIR
  22.  
  23. echo -n "DB password: "
  24. read -s DB_pass
  25. echo
  26. echo "Dumping tables into separate SQL command files for database '$DB' into dir=$DIR"
  27.  
  28. tbl_count=0
  29.  
  30. for t in $(mysql -NBA -h $DB_host -u $DB_user -p$DB_pass -D $DB -e 'show tables')
  31. do
  32.     echo "DUMPING TABLE: $DB.$t"
  33.     mysqldump -h $DB_host -u $DB_user -p$DB_pass $DB $t | gzip > $DIR/$DB.$t.sql.gz
  34.     tbl_count=$(( tbl_count + 1 ))
  35. done
  36.  
  37. echo "$tbl_count tables dumped from database '$DB' into dir=$DIR"
Add Comment
Please, Sign In to add comment