Skip to content

Instantly share code, notes, and snippets.

@Adrian0350
Last active August 12, 2017 21:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adrian0350/5db16fc418b6c0d1b13eddafb3d6960c to your computer and use it in GitHub Desktop.
Save Adrian0350/5db16fc418b6c0d1b13eddafb3d6960c to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# This script gets a database (in this case production db)
# and it will modify the sensitive content:
#
# 1. READ SQL file.
# 2. READ SQL User
# 3. READ SQL PASSWORD
# 4. DROP TABLE IF EXISTS
# 5. CREATE TABLE
# 6. POPULATE TABLE
# 7. UPDATE TABLE (Fake the sensitive data)
# 8. DUMP SQL file.
#
DEFAULT_TARGET_DIR=$(cd "$(dirname "$0")"; pwd)
phone()
{
i=0
while [ $i -lt 10 ]
do
RAND=$((1 + RANDOM % 9))
NUMBER="$NUMBER$RAND"
i=$[$i+1]
done
echo $NUMBER
}
if [ -z $1 ]
then
read -e -p "Enter the database file to modify: " DB_IMPORTED
if [ -z $DB_IMPORTED ]
then
echo "No database file entered. Exiting..."
exit 1
fi
fi
read -e -p "MySQL user: " MYSQL_USER
if [ -z $MYSQL_USER ]
then
echo "You must enter a MySQL user."
echo ""
exit 1
else
read -e -s -p "MySQL password: " MYSQL_PASSWORD
if [ -z $MYSQL_PASSWORD ]
then
echo "No password entered. Exiting..."
exit 1
fi
fi
DATE=`date +%Y-%m-%d`
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD --reconnect --unbuffered <<-TRANSACTION
DROP DATABASE IF EXISTS fake_db;
CREATE DATABASE fake_db DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
use fake_db;
SOURCE $DB_IMPORTED;
SET AUTOCOMMIT=0;
BEGIN;
UPDATE table_name
SET email_field = 'fake@email.com', phone_field = $(phone), cellphone_field = $(phone);
COMMIT;
TRANSACTION
if [ $? -eq 0 ]
then
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD <<-TRANSACTION
\! mysqldump -u$MYSQL_USER -p$MYSQL_PASSWORD fake_db > $DEFAULT_TARGET_DIR/fake_db-$DATE.sql;
TRANSACTION
# Show a message for success or unsuccess db dump respectively.
if [ $? -eq 0 ]
then
echo "Database was succesfully created as fake_db. Update Successful."
echo "Database succesfully faked and saved at: $DEFAULT_TARGET_DIR/fake_db-$DATE.sql"
exit 0
else
echo "Database was succesfully created as fake_db. Update Successful."
echo "Could not export database at $DEFAULT_TARGET_DIR/fake_db-$DATE.sql"
exit 0
fi
else
# If an error happened erase the databse because it might have
# client's private data.
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD --reconnect --unbuffered <<-TRANSACTION
BEGIN;
DROP TABLE IF EXISTS fake_db;
COMMIT;
TRANSACTION
echo "An error happened... Rolling back..."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment