Skip to content

Instantly share code, notes, and snippets.

@erincerys
Forked from shantanuo/dumper.pl
Last active August 10, 2016 14:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save erincerys/5470883 to your computer and use it in GitHub Desktop.
Save erincerys/5470883 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) {
# Forked to
# - remove /*!\s portion of DEFINER
# - handle view SQL SECURITY DEFINERs
# - match variances of whitespace
$line =~ s{(/\*!\d+\s*)?definer\s*=\s*\S+\@\S+(\s*sql\ssecurity\sdefiner\s*)?(\s*\*/)?}{}i;
}
elsif ($replace) {
# Incorporates bugfix from tedtop's fork
$line =~ s{definer\s*=\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);
}
@tedtop
Copy link

tedtop commented Aug 2, 2013

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