|
@@ -1,6 +1,6 @@
|
|
#!/bin/sh
|
|
#!/bin/sh
|
|
|
|
|
|
-# Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
|
|
+# Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
|
|
#
|
|
#
|
|
# Permission to use, copy, modify, and/or distribute this software for any
|
|
# Permission to use, copy, modify, and/or distribute this software for any
|
|
# purpose with or without fee is hereby granted, provided that the above
|
|
# purpose with or without fee is hereby granted, provided that the above
|
|
@@ -36,6 +36,7 @@ db_name="keatest"
|
|
# lease dump parameters
|
|
# lease dump parameters
|
|
dump_type=0
|
|
dump_type=0
|
|
dump_file=""
|
|
dump_file=""
|
|
|
|
+dump_qry=""
|
|
|
|
|
|
# Include utilities. Use installed version if available and
|
|
# Include utilities. Use installed version if available and
|
|
# use build version if it isn't.
|
|
# use build version if it isn't.
|
|
@@ -143,8 +144,15 @@ mysql_init() {
|
|
# We should not hide them as they may give hints to user what is wrong with
|
|
# We should not hide them as they may give hints to user what is wrong with
|
|
# his setup.
|
|
# his setup.
|
|
#
|
|
#
|
|
- mysql_execute "SHOW TABLES;"
|
|
|
|
- COUNT=`echo $_RESULT | wc -w`
|
|
|
|
|
|
+ RESULT=`mysql_execute "SHOW TABLES;"`
|
|
|
|
+ ERRCODE=$?
|
|
|
|
+ if [ $ERRCODE -ne 0 ]
|
|
|
|
+ then
|
|
|
|
+ log_error "mysql_init table query failed, mysql status = $ERRCODE"
|
|
|
|
+ exit 1
|
|
|
|
+ fi
|
|
|
|
+
|
|
|
|
+ COUNT=`echo $RESULT | wc -w`
|
|
if [ $COUNT -gt 0 ]; then
|
|
if [ $COUNT -gt 0 ]; then
|
|
# Let't start with a new line. mysql could have printed something out.
|
|
# Let't start with a new line. mysql could have printed something out.
|
|
printf "\n"
|
|
printf "\n"
|
|
@@ -160,7 +168,7 @@ mysql_init() {
|
|
|
|
|
|
if [ "$ERRCODE" -eq 0 ]; then
|
|
if [ "$ERRCODE" -eq 0 ]; then
|
|
printf "Lease DB version reported after initialization: "
|
|
printf "Lease DB version reported after initialization: "
|
|
- mysql_version_print
|
|
|
|
|
|
+ mysql_version
|
|
printf "\n"
|
|
printf "\n"
|
|
fi
|
|
fi
|
|
|
|
|
|
@@ -200,7 +208,7 @@ memfile_upgrade() {
|
|
mysql_upgrade() {
|
|
mysql_upgrade() {
|
|
|
|
|
|
printf "Lease DB version reported before upgrade: "
|
|
printf "Lease DB version reported before upgrade: "
|
|
- mysql_version_print
|
|
|
|
|
|
+ mysql_version
|
|
printf "\n"
|
|
printf "\n"
|
|
|
|
|
|
# Check if the scripts directory exists at all.
|
|
# Check if the scripts directory exists at all.
|
|
@@ -223,7 +231,7 @@ mysql_upgrade() {
|
|
done
|
|
done
|
|
|
|
|
|
printf "Lease DB version reported after upgrade: "
|
|
printf "Lease DB version reported after upgrade: "
|
|
- mysql_version_print
|
|
|
|
|
|
+ mysql_version
|
|
printf "\n"
|
|
printf "\n"
|
|
}
|
|
}
|
|
|
|
|
|
@@ -231,34 +239,55 @@ pgsql_upgrade() {
|
|
log_error "NOT IMPLEMENTED"
|
|
log_error "NOT IMPLEMENTED"
|
|
}
|
|
}
|
|
|
|
|
|
-### Functions used for dump
|
|
|
|
-memfile_dump() {
|
|
|
|
- log_error "lease-dump is not supported for memfile"
|
|
|
|
- exit 1
|
|
|
|
|
|
+# Utility function which tests if the given file exists and
|
|
|
|
+# if so notifies the user and provides them the opportunity
|
|
|
|
+# to abort the current command.
|
|
|
|
+check_file_overwrite () {
|
|
|
|
+ local file=$1
|
|
|
|
+ if [ -e ${file} ]
|
|
|
|
+ then
|
|
|
|
+ echo "Output file, $file, exists and will be overwritten."
|
|
|
|
+ echo "Do you wish to continue? (y/n)"
|
|
|
|
+ read ans
|
|
|
|
+ if [ ${ans} != "y" ]
|
|
|
|
+ then
|
|
|
|
+ echo "$command aborted by user."
|
|
|
|
+ exit 1
|
|
|
|
+ fi
|
|
|
|
+ fi
|
|
}
|
|
}
|
|
|
|
|
|
### Functions used for dump
|
|
### Functions used for dump
|
|
-mysql_dump() {
|
|
|
|
|
|
+
|
|
|
|
+# Sets the global variable, dump_qry, to the schema-version specific
|
|
|
|
+# SQL text needed to dump the lease data for the current backend
|
|
|
|
+# and protocol
|
|
|
|
+get_dump_query() {
|
|
|
|
+ local version=$1
|
|
|
|
+
|
|
|
|
+ dump_qry=""
|
|
|
|
+ dump_sql_file="$scripts_dir/${backend}/lease_dump_$version.sh"
|
|
|
|
+ if [ ! -e $dump_sql_file ]
|
|
|
|
+ then
|
|
|
|
+ log_error "lease-dump: cannot access dump_sql_file: $dump_sql_file"
|
|
|
|
+ exit 1;
|
|
|
|
+ fi
|
|
|
|
+
|
|
|
|
+ # source in the dump file which defines the sql text we'll need
|
|
|
|
+ . $dump_sql_file
|
|
|
|
+ if [ $? -ne 0 ]
|
|
|
|
+ then
|
|
|
|
+ log_error "lease-dump: error sourcing dump_sql_file: $dump_sql_file"
|
|
|
|
+ exit 1
|
|
|
|
+ fi
|
|
|
|
|
|
# Construct the SQL text to dump the leases based on protocol type
|
|
# Construct the SQL text to dump the leases based on protocol type
|
|
case ${dump_type} in
|
|
case ${dump_type} in
|
|
4)
|
|
4)
|
|
- qry="\
|
|
|
|
-SELECT 'address', 'hwaddr', 'client_id', 'valid_lifetime', 'expire',\
|
|
|
|
-'subnet_id', 'fqdn_fwd', 'fqdn_rev', 'hostname';\
|
|
|
|
-SELECT INET_NTOA(address), IFNULL(HEX(hwaddr), ''), IFNULL(HEX(client_id), ''),\
|
|
|
|
-valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname from lease4"
|
|
|
|
|
|
+ dump_qry="$lease4_dump_sql";
|
|
;;
|
|
;;
|
|
6)
|
|
6)
|
|
- qry="\
|
|
|
|
-SELECT 'address', 'duid', 'valid_lifetime', 'expire',\
|
|
|
|
-'subnet_id', 'pref_lifetime', 'lease_type', 'iaid', 'prefix_len', 'fqdn_fwd',\
|
|
|
|
-'fqdn_rev', 'hostname', 'hwaddr', 'hwtype', 'hwaddr_source';\
|
|
|
|
-SELECT a.address, IFNULL(HEX(a.duid), ''), a.valid_lifetime,\
|
|
|
|
-a.expire, a.subnet_id, a.pref_lifetime, IFNULL(b.name, ''), a.iaid, a.prefix_len,\
|
|
|
|
-a.fqdn_fwd, a.fqdn_rev, hostname, IFNULL(HEX(hwaddr), ''), IFNULL(hwtype, ''),\
|
|
|
|
-IFNULL(hwaddr_source, '') \
|
|
|
|
-FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"
|
|
|
|
|
|
+ dump_qry="$lease6_dump_sql";
|
|
;;
|
|
;;
|
|
*)
|
|
*)
|
|
log_error "you must specify -4 or -6 for lease-dump"
|
|
log_error "you must specify -4 or -6 for lease-dump"
|
|
@@ -267,6 +296,33 @@ FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"
|
|
;;
|
|
;;
|
|
esac
|
|
esac
|
|
|
|
|
|
|
|
+ if [ "$dump_qry" = "" ]
|
|
|
|
+ then
|
|
|
|
+ log_error "lease-dump: dump query appears to be undefined"
|
|
|
|
+ exit 1
|
|
|
|
+ fi
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+memfile_dump() {
|
|
|
|
+ log_error "lease-dump is not supported for memfile"
|
|
|
|
+ exit 1
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+mysql_dump() {
|
|
|
|
+
|
|
|
|
+ # get the correct dump query
|
|
|
|
+ version=`mysql_version`
|
|
|
|
+ retcode=$?
|
|
|
|
+ if [ $retcode -ne 0 ]
|
|
|
|
+ then
|
|
|
|
+ log_error "lease-dump: mysql_version failed, exit code $retcode"
|
|
|
|
+ exit 1;
|
|
|
|
+ fi
|
|
|
|
+
|
|
|
|
+ # Fetch the correct SQL text. Note this function will exit
|
|
|
|
+ # if it fails.
|
|
|
|
+ get_dump_query $version
|
|
|
|
+
|
|
# Make sure they specified a file
|
|
# Make sure they specified a file
|
|
if [ "$dump_file" = "" ]; then
|
|
if [ "$dump_file" = "" ]; then
|
|
log_error "you must specify an output file for lease-dump"
|
|
log_error "you must specify an output file for lease-dump"
|
|
@@ -275,19 +331,34 @@ FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"
|
|
|
|
|
|
fi
|
|
fi
|
|
|
|
|
|
- # Call mysql directly so we can redirect its output to tr to translate tabs to commas
|
|
|
|
- # then to the output file. "mysql_execute" function stores the query result in
|
|
|
|
- # environment variable and for large DBs this might be an issue.
|
|
|
|
- # We also do not use MySQL's output to file as that requires linux superuser privileges to
|
|
|
|
- # execute the select.
|
|
|
|
- mysql -N -B --user=$db_user --password=$db_password -e "${qry}" $db_name | tr '\t' ',' >$dump_file
|
|
|
|
|
|
+ # If output file exists, notify user, allow them a chance to bail
|
|
|
|
+ check_file_overwrite $dump_file
|
|
|
|
+
|
|
|
|
+ # Check the temp file too
|
|
|
|
+ tmp_file="$dump_file.tmp"
|
|
|
|
+ check_file_overwrite $tmp_file
|
|
|
|
+
|
|
|
|
+ # Run the sql to output tab-delimited lease data to a temp file.
|
|
|
|
+ # By using a temp file we can check for MySQL errors before using
|
|
|
|
+ # 'tr' to translate tabs to commas. We do not use MySQL's output
|
|
|
|
+ # to file as that requires linux superuser privileges to execute
|
|
|
|
+ # the select.
|
|
|
|
+ mysql_execute "${dump_qry}" > $tmp_file
|
|
|
|
+ retcode=$?
|
|
|
|
+ if [ $retcode -ne 0 ]; then
|
|
|
|
+ log_error "lease-dump: mysql_execute failed, exit code $retcode";
|
|
|
|
+ exit 1
|
|
|
|
+ fi
|
|
|
|
|
|
- # Check for errors
|
|
|
|
- if [ ${PIPESTATUS[0]} -ne 0 ]; then
|
|
|
|
- log_error "lease-dump failed";
|
|
|
|
|
|
+ # Now translate tabs to commas.
|
|
|
|
+ cat $tmp_file | tr '\t' ',' >$dump_file
|
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
|
+ log_error "lease-dump: reformatting failed";
|
|
exit 1
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
+ # delete the tmp file on success
|
|
|
|
+ rm $tmp_file
|
|
echo lease$dump_type successfully dumped to $dump_file
|
|
echo lease$dump_type successfully dumped to $dump_file
|
|
exit 0
|
|
exit 0
|
|
}
|
|
}
|
|
@@ -295,33 +366,9 @@ FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"
|
|
### Functions used for dump
|
|
### Functions used for dump
|
|
pgsql_dump() {
|
|
pgsql_dump() {
|
|
|
|
|
|
- # Construct the SQL text to dump the leases based on protocol type
|
|
|
|
- case ${dump_type} in
|
|
|
|
- 4)
|
|
|
|
- qry="\
|
|
|
|
-SELECT 'address', 'hwaddr', 'client_id', 'valid_lifetime', 'expire',\
|
|
|
|
-'subnet_id', 'fqdn_fwd', 'fqdn_rev', 'hostname';\
|
|
|
|
-SELECT ('0.0.0.0'::inet + address) AS address,\
|
|
|
|
-encode(hwaddr,'hex'), encode(client_id,'hex'), valid_lifetime,\
|
|
|
|
-expire, subnet_id, fqdn_fwd::int AS fqdn_fwd, fqdn_rev::int AS fqdn_rev,\
|
|
|
|
-hostname FROM lease4"
|
|
|
|
- ;;
|
|
|
|
- 6)
|
|
|
|
- qry="\
|
|
|
|
-SELECT 'address', 'duid', 'valid_lifetime', 'expire',\
|
|
|
|
-'subnet_id', 'pref_lifetime', 'lease_type', 'iaid', 'prefix_len', 'fqdn_fwd',\
|
|
|
|
-'fqdn_rev', 'hostname';\
|
|
|
|
-SELECT a.address, encode(a.duid,'hex'), a.valid_lifetime,\
|
|
|
|
-a.expire, a.subnet_id, a.pref_lifetime, b.name, a.iaid, a.prefix_len,\
|
|
|
|
-a.fqdn_fwd, a.fqdn_rev, hostname \
|
|
|
|
-FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"
|
|
|
|
- ;;
|
|
|
|
- *)
|
|
|
|
- log_error "you must specify -4 or -6 for lease-dump"
|
|
|
|
- usage
|
|
|
|
- exit 1
|
|
|
|
- ;;
|
|
|
|
- esac
|
|
|
|
|
|
+ # @todo use pgsql_version once implemented. See #3883
|
|
|
|
+ version='1.0'
|
|
|
|
+ get_dump_query $version
|
|
|
|
|
|
# Make sure they specified a file
|
|
# Make sure they specified a file
|
|
if [ "$dump_file" = "" ]; then
|
|
if [ "$dump_file" = "" ]; then
|
|
@@ -331,16 +378,20 @@ FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"
|
|
|
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
+ # If output file exists, notify user, allow them a chance to bail
|
|
|
|
+ check_file_overwrite $dump_file
|
|
|
|
+
|
|
# psql does not accept password as a parameter but will look in the environment
|
|
# psql does not accept password as a parameter but will look in the environment
|
|
export PGPASSWORD=$db_password
|
|
export PGPASSWORD=$db_password
|
|
|
|
|
|
# Call psql and redirect output to the dump file. We don't use psql "to csv"
|
|
# Call psql and redirect output to the dump file. We don't use psql "to csv"
|
|
# as it can only be run as db superuser.
|
|
# as it can only be run as db superuser.
|
|
- echo "$header;$qry" | psql -t -q --user=$db_user --dbname=$db_name -w --no-align --field-separator=',' >$dump_file
|
|
|
|
|
|
+ echo "$dump_qry" | psql --set ON_ERROR_STOP=1 -t -q --user=$db_user --dbname=$db_name -w --no-align --field-separator=',' >$dump_file
|
|
|
|
+ retcode=$?
|
|
|
|
|
|
# Check for errors.
|
|
# Check for errors.
|
|
- if [ ${PIPESTATUS[1]} -ne 0 ]; then
|
|
|
|
- log_error "lease-dump failed";
|
|
|
|
|
|
+ if [ $retcode -ne 0 ]; then
|
|
|
|
+ log_error "lease-dump: psql call failed, exit code: $retcode";
|
|
exit 1
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
|
|
@@ -479,7 +530,7 @@ case ${command} in
|
|
memfile_version
|
|
memfile_version
|
|
;;
|
|
;;
|
|
mysql)
|
|
mysql)
|
|
- mysql_version_print
|
|
|
|
|
|
+ mysql_version
|
|
printf "\n"
|
|
printf "\n"
|
|
;;
|
|
;;
|
|
pgsql)
|
|
pgsql)
|