Skip to content

Instantly share code, notes, and snippets.

@Addvilz
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Addvilz/78522418ec0d8bb66294 to your computer and use it in GitHub Desktop.
Save Addvilz/78522418ec0d8bb66294 to your computer and use it in GitHub Desktop.
Snapshot mySql database to another local database
#!/bin/bash
echo "Source database name:"
read sourceDb
if [ -z "${sourceDb-unset}" ]
then
echo "Your mission, should you choose to accept it is to enter a source database name..."
exit 1
fi
echo "Enter snapshot DB name (database snapshot_%name% will be created if does not exists): "
read backupDb
if [ -z "${backupDb-unset}" ]
then
echo "Your mission, should you choose to accept it is to enter a snapshot name..."
exit 1
fi
echo "Enter tables to be backed up (SPACE separated, leave empty for all tables): "
read backupTables
echo "Will copy tables $backupTables from $sourceDb to snapshot_$backupDb, existing data in snapshot will be overwritten."
read -p "Continue (y/n)? " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo "Your choice man, your choice..."
exit 1
fi
mysql -u root -e "CREATE DATABASE IF NOT EXISTS snapshot_$backupDb;"
mysqldump -u root $sourceDb --compress --add-drop-table --disable-keys --no-create-db $backupTables | mysql -B -u root -D snapshot_$backupDb
echo "All done! Check em bananas!"
#!/bin/bash
echo "Snapshot name (without snapshot_):"
read sourceDb
if [ -z "${sourceDb-unset}" ]
then
echo "Your mission, should you choose to accept it is to enter a snapshot name..."
exit 1
fi
echo "Enter target DB (will be created if not exists): "
read targetDb
if [ -z "${targetDb-unset}" ]
then
echo "Your mission, should you choose to accept it is to enter a target database name..."
exit 1
fi
echo "Will copy everything from $sourceDb to $targetDb, existing data will be overwritten."
read -p "Continue (y/n)? " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo "Your choice man, your choice..."
exit 1
fi
mysql -u root -e "CREATE DATABASE IF NOT EXISTS $targetDb;"
mysqldump -u root snapshot_$sourceDb --compress --add-drop-table --disable-keys --no-create-db | mysql -B -u root -D $targetDb
echo "All done! Check em bananas!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment