mysqldump entire structure but only data from selected tables in a single command

前端 未结 5 608
感情败类
感情败类 2021-01-30 05:39

My database has 3 tables: table1, table2 and table3

I would like to do a mysqldump on this database with the following conditions:

  • Dump structure for all t
相关标签:
5条回答
  • 2021-01-30 05:54

    You can't combine them in one command but you can execute both commands at the same time and output to the same file.

    mysqldump -u user -p --no-data db > structure.sql; mysqldump -u user -p db table1 table2 >> structure.sql
    

    to avoid having to enter the password twice you can do -ppassword (note the lack of space!). Also use --no-data in the first command or you end up with the data as well. -d isn't needed when you're doing just one database.

    0 讨论(0)
  • 2021-01-30 05:55

    I don't think you can do it in one command. But you definitely can merge the output to one file. Why not to wrap it in some shell script that does following:

    mysqldump -u $1 -p$2 -d db > dump.sql && mysqldump -u $1 -p$2 db --ignore-table=db.table3 >> dump.sql
    

    You will run this script with two parameters: username and password.

    0 讨论(0)
  • 2021-01-30 06:05

    You can remove the INSERT INTO ... part:

    mysqldump \
      --opt \
      -u ${DB_USER} -p${DB_PASS} \
      ${DB_NAME} \
      | grep -v 'INSERT INTO `table3`' \
      | grep -v 'INSERT INTO `table4`'
    
    0 讨论(0)
  • 2021-01-30 06:06

    It's actually pretty simple, use --where clauses on the tables where you don't want data and give it an always false condition. For instance, load data on foo and gah and only schema on bar:

    mysqldump -u ... -p... myDatabase foo bar --where='1=2' gah > myfile.sql
    

    So YES you can do this on one line.

    0 讨论(0)
  • 2021-01-30 06:13

    Given you may want to pipe the output to another command, as I did, instead of just redirecting to a file and appending to that file in the next command, you could try (modified from the example of stask):

    (mysqldump -u $1 -p$2 -d db && mysqldump -u $1 -p$2 db --ignore-table=db.table3) |\
    your_command
    

    ... in my case:

    (mysqldump -u $1 -p$2 -d db && mysqldump -u $1 -p$2 db --ignore-table=db.table3) |\
    gzip -9 > filename.sql.gz
    

    Enclosing the two mysqldump commands in parentheses creates a subshell whose output we pipe into gzip and then redirect that into a file.

    PS: I've also been unable to combine it into one single mysqldump invocation, though.

    0 讨论(0)
提交回复
热议问题