Skip to content

Instantly share code, notes, and snippets.

@shantanuo
Created September 20, 2011 09:12
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shantanuo/1228701 to your computer and use it in GitHub Desktop.
Save shantanuo/1228701 to your computer and use it in GitHub Desktop.
MySQLdump filter. Removes or replaces the DEFINER clauses from a dump
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case );
my $replace = undef;
my $delete = undef;
my $help = 0;
GetOptions (
'replace|r=s' => \$replace,
'delete|d' => \$delete,
'help|h' => \$help,
) or help('unrecognized options');
help() if $help;
if ($delete and $replace) {
help( 'you must choose EITHER "delete" OR "replace". Not both');
}
$delete = 1 unless $replace;
while (my $line = <STDIN>) {
if ($delete) {
$line =~ s{(/\*!\d+ )\s*definer\s*=\s*\S+}{$1}i;
}
elsif ($replace) {
$line =~ s{(/\*!\d+\s+definer\s*=\s*)(\S+)}{$1 $replace}i;
}
print $line;
}
sub help {
my ($msg) = @_;
if ($msg) {
print "*** $msg\n";
}
print "dump_filter - mysqldump filter \n",
"(C) Giuseppe Maxia, 2009\n",
"removes/changes DEFINER clauses from MySQL dumps\n",
"USAGE: dump_filter [options]\n",
" -d|--delete removes the DEFINER clauses\n",
" -r|--replace=s replaces every DEFINER clause with the \n",
" new value provided\n",
" -h|--help This text\n";
exit(1);
}
@shantanuo
Copy link
Author

It is necessary when you want to migrate a database to a new server with different users.

Normal Output looks like this...
mysqldump -udeveloper -pmatrixlist -d --routines freshnew1 | more
/!50003 CREATE/ /!50020 DEFINER=root@localhost/ /*!50003 FUNCTION quota_amount(f_service_no int (6),

new output looks like this...
mysqldump -udeveloper -pmatrixlist -d --routines freshnew1 | perl /home/develop/dumper.pl --delete | more
/!50003 CREATE/ /!50020 /!50003 FUNCTION quota_amount(f_service_no int (6),

@erincerys
Copy link

The /*!50020 is related to the DEFINER keyword, and should be removed as well. I forked this gist to include that in the delete regex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment