Skip to content

Instantly share code, notes, and snippets.

@dburger
Created November 23, 2008 21:33
Show Gist options
  • Save dburger/28222 to your computer and use it in GitHub Desktop.
Save dburger/28222 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
if [ $# -eq 2 ]; then
user=$1
dbname=$2
basecmd="mysql -u ${user} -D ${dbname}"
elif [ $# -eq 3 ]; then
user=$1
PASS=$2
dbname=$3
basecmd="mysql -u ${user} -p$PASS -D ${dbname}"
else
echo "usage: mysqltrunc user [pass] database" >&2
exit 1
fi
tables=$(${basecmd} -e "SHOW TABLES;" | grep -v "+--" | grep -v "Tables_in_${dbname}")
if [ $? -ne 0 ]; then
echo "Unable to retrieve the table names." >&2
exit 1
fi
cmd=""
for table in ${tables}; do
cmd="${cmd} TRUNCATE ${table};"
done
$(${basecmd} -e "${cmd}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment