Skip to content

Instantly share code, notes, and snippets.

@jeffhung
Created May 10, 2012 07:30
Show Gist options
  • Select an option

  • Save jeffhung/2651688 to your computer and use it in GitHub Desktop.

Select an option

Save jeffhung/2651688 to your computer and use it in GitHub Desktop.
My script to backup mysql database as SQL as complete as possible. Note that this runs slowly.
#!/bin/sh
__exe_name__=`basename $0`;
__revision__=`echo '$Rev: 37 $' | cut -d' ' -f2`;
__rev_date__=`echo '$Date: 2007-12-25 00:32:28 +0800 (Tue, 25 Dec 2007) $' | cut -d' ' -f2`;
usage()
{
local ex=0;
if [ $# -gt 0 ]; then
ex=$1; shift;
fi;
echo >&2 "\
Usage: $__exe_name__ [ <optoin> ... ] <db-name> [ <dump-file> ]
Formalize mysqldump(1) output to a form that is easy to edit.
Options:
-t,--table <table-name> Dump table(s) <table-name> only. This option can
be specified more than one times. The default is
to dump all tables in database <db-name>.
-u,--user <user> Log into mysql database with username <user>. If
<password> is not specified via --pass option, there
will be a prompt showed up waiting for user to input
the <password>.
-p,--pass <password> Specify login password as <password>.
If <password> is not specified, mysql will prompt
to get it.
-q,--quiet Run quietly.
-h,--help Show this help message.
Version: r$__revision__ ($__rev_date__)
";
exit $ex;
}
msg_exit()
{
local ex=0;
if [ $# -gt 0 ]; then
ex=$1; shift;
fi;
if [ $# -gt 0 ]; then
while [ $# -gt 0 ]; do
echo >&2 "ERROR: $1"; shift;
done;
echo >&2 "";
fi;
echo "Please type '$__exe_name__ --help' for help messages.";
exit $ex;
}
opt_tables='';
opt_user='';
opt_pass='';
opt_verbose='yes';
opt_db_name='';
opt_dump_file='';
while [ $# -gt 0 ]; do
arg="$1"; shift;
case "$arg" in
-t|--table)
if [ $# -gt 0 ]; then
table_name="$1"; shift;
opt_tables="$opt_tables '$table_name'";
else
msg_exit 1 'Missing <table-name>.';
fi;
;;
-u|--user)
if [ $# -gt 0 ]; then
opt_user="$1"; shift;
else
msg_exit 1 'Missing <user>.';
fi;
;;
-p|--pass)
if [ $# -gt 0 ]; then
opt_pass="$1"; shift;
else
msg_exit 1 'Missing <password>.';
fi;
;;
-q|--quiet)
opt_verbose='no';
;;
-h|--help)
usage;
;;
-*)
msg_exit 1 "Unknown option: $arg";
;;
*)
if [ -z "$opt_db_name" ]; then
opt_db_name="$arg";
elif [ -z "$opt_dump_file" ]; then
opt_dump_file="$arg";
else
msg_exit 1 "Unknown argument: $arg";
fi;
;;
esac;
done;
if [ -z "$opt_db_name" ]; then
msg_exit 1 'Missing <db-name>.';
fi;
user_statement='';
if [ -n "$opt_user" ]; then
user_statement="--user $opt_user";
fi;
pass_statement='';
if [ -n "$opt_user" ]; then
if [ -n "$opt_pass" ]; then
pass_statement="--password=$opt_pass";
else
pass_statement="--password";
fi;
#else
# pass_statement="--password='$opt_pass'";
fi;
verbose_statement='';
if [ "x$opt_verbose" = 'xyes' ]; then
verbose_statement='--verbose';
fi;
dump_statement='';
if [ -n "$opt_dump_file" ]; then
dump_statement="> '$opt_dump_file'";
fi;
mysqldump --skip-opt \
--skip-quote-names \
--complete-insert \
--add-drop-table \
--add-locks \
--lock-tables \
--create-options \
$user_statement \
$pass_statement \
$verbose_statement \
"$opt_db_name" \
$opt_table_names \
$dump_statement;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment