Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqldump with multiple tables with or without where clause

I have a set of tables in my database that I have to take a dump ( :D ) of. My problem is I want to take some data from some tables that only date back certain days and would like to keep the remaining tables in tact.

The query I came up with was something like:

mysqldump -h<hostname> -u<username> -p <databasename> 
<table1> <table2> <table3> 
<table4> --where 'created > DATE_SUB(now(), INTERVAL 7 DAY)',
<table5> --where 'created > DATE_SUB(now(), INTERVAL 7 DAY)
--single-transaction --no-create-info | gzip
> $(date +%Y-%m-%d-%H)-dump.sql.gz

The trouble with the above code is that table1, table2 and table3 will try to take the where clause of table4. I don't want that cause that would spit out an error that created field does not exist in these tables.

I tried putting comma (,) after table names as I did after where clause but it doesn't work.

At this point I'm pretty much stuck and have no more alternative expect create two different sql dump files, which I wouldn't want to do.

like image 809
Sworup Shakya Avatar asked Sep 03 '15 06:09

Sworup Shakya


2 Answers

make two dumps or if you dont want to make two dumps then try two command

a.

mysqldump -h<hostname> -u<username> -p 
<databasename>  <table1> <table2> <table3>
--single-transaction --no-create-info > dumpfile.sql

b.

mysqldump -h<hostname> -u<username> -p <databasename> 
<table4> --where 'created > DATE_SUB(now(), INTERVAL 7 DAY)',
<table5> --where 'created > DATE_SUB(now(), INTERVAL 7 DAY)
--single-transaction --no-create-info >> dumpfile.sql

c.

gzip dumpfile.sql
like image 155
Sandeep Maharjan Avatar answered Nov 09 '22 10:11

Sandeep Maharjan


To dump multiple tables with multiple where conditions.

mysqldump -h <hostname> -u <username> -p'<password>' <db_name> <table_name> -where 'condition=true' --no-create-info > dumpfile.sql

Then, for second table, use ">>". It will append the previous dump file.

--no-create-info >> dumpfile.sql

mysqldump -h <hostname> -u <username> -p'<password>' <db_name> <table_name_2> -where 'condition=true' --no-create-info >> dumpfile.sql
like image 2
Rishabh Jhalani Avatar answered Nov 09 '22 10:11

Rishabh Jhalani