{"id":2508,"date":"2011-01-12T02:21:56","date_gmt":"2011-01-11T20:51:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2508"},"modified":"2016-12-19T14:36:43","modified_gmt":"2016-12-19T09:06:43","slug":"mysql-dump-multipurpose-script","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/mysql-dump-multipurpose-script\/","title":{"rendered":"Mysql Dump multipurpose script."},"content":{"rendered":"<p>Taking MYSQL dump is usual requirement in my project . And Since some of the tables in project are huge so often I have to take dumps of selected tables and often have to ignore some tables.<\/p>\n<p>since we had to do it frequently in our project so I created a script\u00a0 which does following for me.<\/p>\n<ol>\n<li>Takes database dump<\/li>\n<li>Takes dump of specified tables.<\/li>\n<li>Takes dump of all the tables other than specified.<\/li>\n<\/ol>\n<p>Inside the script I have added variables like username and password which are required by Mysql. (So if you are using this ,you will need to change them.)<\/p>\n<p>[shell]<br \/>\nUSER=&quot;username&quot;<br \/>\nPASSWORD=&quot;password&quot;<br \/>\nDATABASE=&quot;$1&quot;<br \/>\nDUMPS=&quot;Dumps&quot;<br \/>\nDUMPDIR=&quot;$DATABASE$DUMPS&quot;<br \/>\nFILENAME=&quot;latest.sql&quot;<br \/>\ncd;mkdir &quot;$DUMPDIR&quot;<\/p>\n<p>if [ $# -eq 0 ]; then<br \/>\n\techo &quot;Usage : &lt;databasename&gt; &lt;tablename*&gt;  # if table name(s) is specified only that\/those table will be dumped&quot;<br \/>\n\techo &quot;Altername Usage : &lt;databasename&gt; -k &lt;table(s)dToBeIgnored&gt;&quot;<br \/>\n\texit<br \/>\nfi<\/p>\n<p>if [ $# -eq 1 ];then<br \/>\necho &quot;Dumping $DATABASE to  $DUMPDIR\/$FILENAME&quot;<br \/>\nmysqldump &#8211;user=&quot;$USER&quot; &#8211;password=&quot;$PASSWORD&quot; &quot;$DATABASE&quot; &gt; ~\/&quot;$DUMPDIR&quot;\/&quot;$FILENAME&quot;<br \/>\nfi<\/p>\n<p>COMMAND=&quot;mysqldump &#8211;user=$USER &#8211;password=$PASSWORD $DATABASE&quot;<br \/>\nif [ $# -gt 1 -a $2 != &quot;-i&quot; ];<br \/>\n\tthen<br \/>\n     for i in $*<br \/>\n\t do<br \/>\n\t\t if [ $i != &quot;$DATABASE&quot; -a $i != &quot;-i&quot; ];then<br \/>\n\t\t\t $COMMAND $i &gt; ~\/&quot;$DUMPDIR&quot;\/&quot;$i.sql&quot;;<br \/>\n\t\t fi<br \/>\n\t done<br \/>\nfi<\/p>\n<p>if [ $# -gt 2 -a $2 == &quot;-i&quot; ];<br \/>\n\tthen<br \/>\n\tCOMMAND2=&quot; &quot;<br \/>\n\tfor i in $*<br \/>\n\tdo<br \/>\n\t\tif [ $i != &quot;$DATABASE&quot; -a $i != &quot;-i&quot; ];then<br \/>\n\t\tCOMMAND2=&quot;$COMMAND2 &#8211;ignore-table=$DATABASE.$i&quot;<br \/>\n\t\tfi<br \/>\n\tdone<br \/>\n\t$COMMAND $COMMAND2 &gt;  ~\/&quot;$DUMPDIR&quot;\/&quot;$DATABASE&quot;-&quot;Ignored.sql&quot;<br \/>\nfi<br \/>\n[\/shell]<\/p>\n<p>Now what this can be used in three ways &#8211;<br \/>\n(Note &#8211; I have named this script sqlDump and all of following examples use this name )<\/p>\n<ol>\n<li> .\/sqlDump.sh databasename<br \/>\nThis will take a complete dump of all my tables in specified database and store it in file named latest.sql<\/li>\n<li>.\/sqlDump.sh databasename table1 table2 &#8230; tablenN<br \/>\nThis will take dump of all tables specified.And will store them in file named &#8211; table1.sql , table2.sql &#8230; tableN.sql<\/li>\n<li>.\/sqlDump.sh databasename -i table1 table2 .. tableN<br \/>\nThis will take dump off all the tables in database ignoring specified tables. And will store it in file named &lt;databaseName&gt;-Ignored.sql.<\/li>\n<\/ol>\n<blockquote><\/blockquote>\n<p>If file is executed without any arguments it will just describe its Usage<br \/>\nNote &#8211; All of these tables will be stored in directory named &lt;databasename&gt;Dumps in home directory.<\/p>\n<p><!--more--><\/p>\n<p>This has helped me many times and has saved my time too . Hope this helps you too<\/p>\n<p>_________________________________<\/p>\n<p><em>Hitesh Bhatia<\/em><a href=\"mailto:hitesh@intelligrape.com?subject=Feedback%20On%20Blog\"><\/a><\/p>\n<p>Mail,LinkedIn,<a href=\"http:\/\/www.facebook.com\/home.php?#%21\/profile.php?id=100000114437286\" target=\"_blank\">Facebook<\/a>,<a href=\"http:\/\/twitter.com\/d1_ricky\" target=\"_blank\">Twitter<\/a><\/p>\n<p>_________________________________<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Taking MYSQL dump is usual requirement in my project . And Since some of the tables in project are huge so often I have to take dumps of selected tables and often have to ignore some tables. since we had to do it frequently in our project so I created a script\u00a0 which does following [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[7],"tags":[485,487,488,260,76,486],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2508"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2508"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2508\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}