{"id":467,"date":"2015-03-24T09:11:53","date_gmt":"2015-03-24T08:11:53","guid":{"rendered":"http:\/\/www.dereckson.be\/blog\/?p=467"},"modified":"2017-02-09T02:48:11","modified_gmt":"2017-02-09T01:48:11","slug":"follow-up-a-bash-script-to-split-a-mysql-dump-by-database","status":"publish","type":"post","link":"https:\/\/www.dereckson.be\/blog\/2015\/03\/24\/follow-up-a-bash-script-to-split-a-mysql-dump-by-database\/","title":{"rendered":"Follow-up: a BASH script to split a MySQL dump by database"},"content":{"rendered":"<p>In <a title=\"Split a large SQL dump by database\" href=\"https:\/\/www.dereckson.be\/blog\/2015\/03\/21\/split-a-large-sql-dump-by-database\/\">this post<\/a>, we&#8217;ve seen how to split a large MySQL dump by database.<\/p>\n<p>I&#8217;ve been asked a script to automate the process. Here you are.<\/p>\n<p><strong>Note:\u00a0<\/strong>On FreeBSD, replace AWK=awk by\u00a0AWK=<strong>g<\/strong>awk and install lang\/gawk port, so we can use GNU awk.<\/p>\n<pre>#!\/usr\/bin\/env bash\r\n\r\nAWK=awk\r\nREGEX_NAME=\"Current Database: \\`(.*)\\`\"\r\n\r\n# Checks argument and prints usage if needed\r\nif [ \"$#\" -lt \"1\" ]\r\nthen\r\n    echo \"Usage: $0 &lt;dump.sql&gt;\"\r\n    exit 1\r\nfi\r\n\r\n# Splits dump into temporary files\r\n$AWK '\/Current Database\\: .*\/{g++} { print $0 &gt; g\".tmpsql\" }' $1\r\n\r\n# Renames files or appends to existing one (to handle views)\r\nfor f in *.tmpsql\r\ndo\r\n    DATABASE_LINE=`head -n1 $f`\r\n    [[ $DATABASE_LINE =~ $REGEX_NAME ]]\r\n    TARGET_FILE=\"${BASH_REMATCH[1]}.sql\"\r\n    if [ -f $TARGET_FILE ]; then\r\n        cat $f &gt;&gt; $TARGET_FILE\r\n        rm $f\r\n    else\r\n        mv $f ${BASH_REMATCH[1]}.sql\r\n    fi\r\ndone\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we&#8217;ve seen how to split a large MySQL dump by database. I&#8217;ve been asked a script to automate the process. Here you are. Note:\u00a0On FreeBSD, replace AWK=awk by\u00a0AWK=gawk and install lang\/gawk port, so we can use GNU awk. #!\/usr\/bin\/env bash AWK=awk REGEX_NAME=&#8221;Current Database: \\`(.*)\\`&#8221; # Checks argument and prints usage if needed [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[304],"class_list":["post-467","post","type-post","status-publish","format-standard","hentry","category-sysadmin","tag-mysql"],"_links":{"self":[{"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/posts\/467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/comments?post=467"}],"version-history":[{"count":3,"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/posts\/467\/revisions"}],"predecessor-version":[{"id":472,"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/posts\/467\/revisions\/472"}],"wp:attachment":[{"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/media?parent=467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/categories?post=467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/tags?post=467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}