Get Database Backup and Restore Database from Backup Using SSH(Terminal)
In this post we’ll let you know how to get Database Backup and how to Restore Database Using SSH. In this post we have mention both types of commands for get database backup/restore from .sql file or get database backup/restore from compressed file.
If you want to run sql query and you need to go into the mysql mode then following command will helps you on that. you just need to replace “root” with your database username and run this command after that it’ll ask you for your database user’s password. you just need to enter the password of your database user and press enter that it.
Connect To Mysql
mysql -u root -p
For run below commands you don’t need to login into the mysql on your terminal. You just need to loged in to your server using ssh and run following commands and you’ll get database backup or you’ll restore database Using this command.
Take Database Backup
Following Command will helps to create Database backup without compressed. You just need to replace “root” with your database username and DBUSER_PASS
with you Database User’s Password and also you need to replace DATABASE_NAME
with your Database name and PATH
with your directory path and don’t forgot to replace file FILE_NAME.sql
with filename which you want.
mysqldump --opt -u root -pDBUSER_PASS DATABASE_NAME > /PATH/YOU/WANT_SAVE/FILE_NAME.sql
Take Compressed Database Backup
With this command we are able to create Database Backup with compressed file and it’s size lower then normal backup. And we are always suggest you to take backup with compressed file because it’s really helps you when your database size is big. You just need to run following command for take Database Backup with compressed file. And you just need to replace root with your database username and DBUSER_PASS
with you Database User’s Password and also you need to replace DATABASE_NAME
with your Database name and PATH
with your directory path and don’t forgot to replace file FILE_NAME.sql.gz with filename which you want.
mysqldump --opt -u root -pDBUSER_PASS DATABASE_NAME | gzip -9 > /PATH/YOU/WANT_SAVE/FILE_NAME.sql.gz
Restore Database
Following command will helps you to restore database from backup. To restore Database you just need to run following command and replace root with your database username and DBUSER_PASS
with you Database User’s Password and also you need to replace DATABASE_NAME
with your Database name and PATH
with your directory path and don’t forgot to replace file FILE_NAME.sql with your backup filename.
mysql -u root -pDBUSER_PASS DATABASE_NAME < /PATH/YOU/WANT_SAVE/FILE_NAME.sql
Restore Database from Compressed file.
Using with following command you are able to restore database from Compressed file. To restore Database you just need to run following command and replace "root" with your database username and DBUSER_PASS
with you Database User's Password and also you need to replace DATABASE_NAME
with your Database name and PATH
with your directory path and don't forgot to replace file FILE_NAME.sql with your backup filename.
gunzip < /PATH/YOU/WANT_SAVE/FILE_NAME.sql.gz | mysql -u root -pDBUSER_PASS DATABASE_NAME
We hope this post helped you to Get Database Backup Or Restore Database Backup Using SSH(Terminal). Bookmark it for your future reference. Do comment below if you have any other questions or doubts.
P.S. Do share this post with your team.
Review other articles maybe it'll help you too.