Importing and Exporting of MYSQL Database with Command Line SSH

Importing MYSQL Database

Syntax

$ mysql -u username -p database_name < path_to_mysql_file_here

Importing existing MYSQL database with filename "wordpress.sql" stored at directory "/home/nishant/", Username is "root", terminal will prompt for asking password.

$ mysql -u root -p wordpress < /home/nishant/wordpress.sql

Exporting MYSQL Database

Syntax

$ mysqldump -p -u username database_name > path_to_mysql_file_here

Exporting MYSQL database to filename "db.sql" stored at directory "/home/nishant/", Username is "root", terminal will prompt for asking password.

$ mysqldump -p -u root wordpress > /home/nishant/db.sql

Exporting MYSQL Database by creating file with TimeStamp

Learn here => How To Create file with TimeStamp in Shell Script

Following shell script written for exporting mysql database to file along with timestamp.

#!/bin/sh
# Database backup configuration
DB_USER="root"
DB_PASS="root"
FILE_PATH=~/Desktop/
cd $FILE_PATH

# WordPress Backup configuration
DATABASE="wordpress"
FILE="$DATABASE_$(date +%d%m%Y_%H%M%S).sql"
ZIPFILE="$FILE.zip"
mysqldump -p$DB_PASS -u $DB_USER $DATABASE > $FILE_PATH$FILE
zip $FILE_PATH$ZIPFILE $FILE
sleep 10
rm $FILE

Nishant Vaity

I am Software Engineer, Technology Mentor & Entrepreneur. I am passionate about the technologies I use and always eager to share & learn more from that passion. I am an experienced Software Engineer with 8+ years of experience in coding high-traffic websites. Proficient in HTML, JavaScript, jQuery, CSS with LAMP / MAMP / WAMP Stack. Contributor for WordPress open source plugins and Translator (Marathi) for WordPress 4.5.x.

%d bloggers like this: