MySQL backup and restoring database

Posted in Programming by v.udartsev on 22 April 2018

Backup database

time mysqldump -u root -p database > /var/backups/restore_backup.sql

Backup config and data

mv /var/lib/mysql /tmp/backups/mysql-lib.bak
cp /etc/mysql/my.cnf /tmp/backups/my.cnf.bak

Purge library

sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean

Reinstall

sudo apt-get update
sudo apt-get install mysql-server

Restore config

cp /tmp/backups/my.cnf.bak /etc/mysql/my.cnf
sudo service mysql restart

Create database

mysql -u root -p
mysql> CREATE DATABASE database;

Restore backup

time mysql -u root -p database < /var/backups/restore_backup.sql