Do You Want to Learn Marketing or Coding? Do You Want to Learn Marketing or Coding?

Auto restoring all mysql
databases within a directory

Share on facebook
Share on twitter
Share on linkedin

Here is a simple ssh code to restore all databases within a directory.  This is an automated batch restore.  Meaning you don’t have to sit there entering commands all night long.  This is great if you are migrating databases to a new server.  Or, if God forbids you have to store all mysql databses on your server for whatever reason.

First dump all of your mysql databases into a temporary directory as sql.gz archived files. Make sure the sql.gz file is the same name as your database name.   Meaning if the database name is milesanimates then make sure the archived file is titled milesanimates.sql.gz.  This command only works if the  mysql database archived as a .sql.gz file.  It takes the name of the file and stores it into a database named the same.  CD into the directory and run the following command.

ls *.gz | awk -F. ‘{print $1}’ | while read A; do zcat $A.sql.gz | mysql $A; done

If it doesn’t run correctly it means that the database doesn’t exist on the server. If that is the case then run this command first to create all of the databases.

ls *.gz | awk -F. ‘{print $1}’ | while read A; do echo “mysqladmin create $A”; done

I Really Need

Reviewing nerdy stuff is expensive.

Consider clicking on this ad & buying that item you REALLY wanted from Amazon.

It helps a ton!

Leave Your Thoughts