Because we all love SSH, here is a little reminder of how to backup a MySQL database from command line.
1 2 3 4 5 6 7 8 9 10 11 |
/* -l is for login -p is for password -h is for the host, with or without port number */ mysqldump --add-drop-database --add-drop-table -u mysqlLogin -pThePassword -h localhost:8889 database-name > database-name-backup.sql /* You can also do it that way : */ mysqldump --add-drop-database --add-drop-table --user=mysqlLogin --password=ThePassword -h localhost:8889 database-name > database-name-backup.sql |
This is to know !
It will simple dump all database tables into a file called database-name-backup.sql using login mysqlLogin and the given password.
I made a simple bash script that creates a backup file, so no need to remember that command line :
1 2 3 4 5 6 7 8 9 |
/* Copy/paste to a file, and do 'chmod +x' to the file so it can be executed The backup file will automaticaly have the current date in it */ #!/bin/bash temp_backup_name="my-db-backup-" date_str="$(date +"%d-%m-%Y")" temp_backup_name="$temp_backup_name$date_str" mysqldump --add-drop-database --add-drop-table --user=mysqlLogin --password=ThePassword database-name > "$temp_backup_name.sql" |
To restore it, it’s pretty simple :
1 2 3 4 5 6 |
/* -l is for login -p is for password -h is for the host, with or without port number */ mysql -u mysqlLogin -pThePassword -h localhost:8889 database-name < database-name-backup.sql |