From 70704f9702d95598a55471205fe37896d5738381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yavuz=20Ayd=C4=B1n?= Date: Tue, 12 Oct 2021 17:09:09 +0200 Subject: [PATCH] Recovery instructions updated and made sure it also works for databases with a dash in the name --- README.md | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 27645c3..67a88f3 100644 --- a/README.md +++ b/README.md @@ -63,13 +63,23 @@ This will add a cronjob to root which will run this script daily at 0:10 am. Adj ## Recover database ## -Assuming you want to restore mytestdb from your backups made on 20190121-1540: +Assuming you want to restore my-testdb from your backups made on 20211012-0010, define variables: +``` +DB='my-testdb' +NEWDB='my-testdb' +BACKUPDIR='/var/backup/mysql/20211012-0010' +``` +Make sure you have a backup of the current database which will be replaced: ``` -DB='mytestdb' -BACKUPDIR='/var/backup/mysql/20190121-1540' -sudo mysqldump ${DB} > /root/${DB}-$(date +%Y%m%d).sql -sudo mysql -e "DROP DATABASE ${DB}" -sudo mysql -e "CREATE DATABASE ${DB}" -for table in ${BACKUPDIR}/${DB}/*; do gunzip -c $table | sudo mysql ${DB}; done +sudo mysqldump ${NEWDB} > /root/${NEWDB}-$(date +%Y%m%d).sql ``` + +And now the actual recovery (Need to be in the same session! Do not enter this in another SSH window / terminal): +``` +sudo mysql -e 'DROP DATABASE IF EXISTS `'${NEWDB}'`' +sudo mysql -e 'CREATE DATABASE `'${NEWDB}'`' +for table in ${BACKUPDIR}/${DB}/*; do gunzip -c $table | sudo mysql ${NEWDB}; done +``` + +You can change NEWDB to any other name if you wish to restore to an alternate database name! \ No newline at end of file