<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://freebsdwiki.net/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://freebsdwiki.net/index.php?action=history&amp;feed=atom&amp;title=Mysql%2C_backing_up</id>
		<title>Mysql, backing up - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://freebsdwiki.net/index.php?action=history&amp;feed=atom&amp;title=Mysql%2C_backing_up"/>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php?title=Mysql,_backing_up&amp;action=history"/>
		<updated>2026-04-08T07:44:00Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.18.0</generator>

	<entry>
		<id>http://freebsdwiki.net/index.php?title=Mysql,_backing_up&amp;diff=11983&amp;oldid=prev</id>
		<title>Jimbo: Created page with 'Sometimes, mysqldump just isn't quite what you want, by itself, for backing up large servers with many databases.  In one case, I found myself needing a single dump of ALL da…'</title>
		<link rel="alternate" type="text/html" href="http://freebsdwiki.net/index.php?title=Mysql,_backing_up&amp;diff=11983&amp;oldid=prev"/>
				<updated>2011-01-24T00:57:35Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;Sometimes, &lt;a href=&quot;/index.php?title=Mysqldump&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Mysqldump (page does not exist)&quot;&gt;mysqldump&lt;/a&gt; just isn&amp;#039;t quite what you want, by itself, for backing up large servers with many databases.  In one case, I found myself needing a single dump of ALL da…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Sometimes, [[mysqldump]] just isn't quite what you want, by itself, for backing up large servers with many databases.&lt;br /&gt;
&lt;br /&gt;
In one case, I found myself needing a single dump of ALL databases on a given server, in a consistent state with one another (ie, no updates done to one db after another db had been dumped).  The only way to do that with mysqldump is to use the --single-transaction flag and dump all databases in a single command... but if you do that, you end up with one big monolithic dump file, which can be seriously inconvenient if you ''do'' need to restore (or just examine the contents of) a single db at a time.&lt;br /&gt;
&lt;br /&gt;
The following script was my solution to this problem.  It reads host, username, and password as single lines from a config file, like so:&lt;br /&gt;
&lt;br /&gt;
 127.0.0.1&lt;br /&gt;
 root&lt;br /&gt;
 mypassword&lt;br /&gt;
&lt;br /&gt;
Which you specify as the single argument given to the perl script, like so:&lt;br /&gt;
&lt;br /&gt;
 me@box:~$ perl /usr/local/bin/mysqlbak.pl /etc/mysqlbak.conf&lt;br /&gt;
&lt;br /&gt;
Like it says in the comments, the script will output a single dump file for each database, in the current directory at the time the script is run.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 #!/usr/bin/perl&lt;br /&gt;
 &lt;br /&gt;
 #&lt;br /&gt;
 # reads $host, $user, and $password as one single line in that order &lt;br /&gt;
 # from config file specified as the argument passed to the program.&lt;br /&gt;
 # backs up all databases at $host to individual gzipped dumpfiles&lt;br /&gt;
 # in the current directory.&lt;br /&gt;
 #&lt;br /&gt;
 &lt;br /&gt;
 open CONF, $ARGV[0];&lt;br /&gt;
 $host = &amp;lt;CONF&amp;gt;;&lt;br /&gt;
 $user = &amp;lt;CONF&amp;gt;;&lt;br /&gt;
 $password = &amp;lt;CONF&amp;gt;;&lt;br /&gt;
 close CONF;&lt;br /&gt;
 chomp $host; chomp $user; chomp $password;&lt;br /&gt;
 &lt;br /&gt;
 $mysql = '/usr/bin/mysql';&lt;br /&gt;
 $mysqldump = '/usr/bin/mysqldump';&lt;br /&gt;
 $grep = '/bin/grep';&lt;br /&gt;
 $gzip = '/bin/gzip';&lt;br /&gt;
 &lt;br /&gt;
 @databases = `echo show databases | $mysql -u $user -h $host -p'$password' | $grep -v information_schema`;&lt;br /&gt;
 delete $databases[0];&lt;br /&gt;
 $dblist = join (' ', @databases);&lt;br /&gt;
 $dblist =~ s/\n//g;&lt;br /&gt;
 &lt;br /&gt;
 open DUMP, &amp;quot;$mysqldump -C --flush-logs --single-transaction -u $user -h $host -p'$password' --databases$dblist |&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 do {&lt;br /&gt;
         $line =	&amp;lt;DUMP&amp;gt;;&lt;br /&gt;
         push @header, $line;&lt;br /&gt;
 } until ($line =~ /^-- Current Database/); &lt;br /&gt;
 &lt;br /&gt;
 pop @header;&lt;br /&gt;
 &lt;br /&gt;
 open OUT, &amp;quot;&amp;gt; /dev/null&amp;quot;;&lt;br /&gt;
 do {&lt;br /&gt;
         if ($line =~ /^-- Current Database/) {       &lt;br /&gt;
                 close OUT;&lt;br /&gt;
                 (my $db) = ($line =~ /.*\`(\w*)\`/);&lt;br /&gt;
                 open OUT, &amp;quot;| $gzip --rsyncable &amp;gt; $db.sql.gz&amp;quot;;&lt;br /&gt;
 		print OUT @header;&lt;br /&gt;
 		print OUT $line;&lt;br /&gt;
         } else {&lt;br /&gt;
 		print OUT $line;&lt;br /&gt;
 	}&lt;br /&gt;
  } while ($line = &amp;lt;DUMP&amp;gt;);&lt;br /&gt;
 &lt;br /&gt;
 close OUT;&lt;br /&gt;
 close DUMP;&lt;/div&gt;</summary>
		<author><name>Jimbo</name></author>	</entry>

	</feed>