Skip to content

Instantly share code, notes, and snippets.

@seqizz
Last active August 1, 2021 03:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save seqizz/8002376 to your computer and use it in GitHub Desktop.
Save seqizz/8002376 to your computer and use it in GitHub Desktop.
Splitting --all-databases backup to per-database .sql files.
#!/usr/bin/perl -w
#
# Credit goes to: http://stackoverflow.com/questions/1876754/split-up-a-mysqldump-file-with-multiple-databases-by-database
#
# splitmysqldump - split mysqldump file into per-database dump files.
use strict;
use warnings;
my $dbfile;
my $dbname = q{};
my $header = q{};
while (<>) {
# Beginning of a new database section:
# close currently open file and start a new one
if (m/-- Current Database\: \`([-\w]+)\`/) {
if (defined $dbfile && tell $dbfile != -1) {
close $dbfile or die "Could not close file!"
}
$dbname = $1;
open $dbfile, ">>", "$1_dump.sql" or die "Could not create file!";
print $dbfile $header;
print "Writing file $1_dump.sql ...\n";
}
if (defined $dbfile && tell $dbfile != -1) {
print $dbfile $_;
}
# Catch dump file header in the beginning
# to be printed to each separate dump file.
if (! $dbname) { $header .= $_; }
}
close $dbfile or die "Could not close file!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment