Author Topic: Restore MYSQL Backup from Daily Backup  (Read 9769 times)

0 Members and 1 Guest are viewing this topic.

Offline
*
Restore MYSQL Backup from Daily Backup
« on: January 23, 2018, 01:25:54 AM »
I want to restore one of my MYSQL Backup from Daily Backup.

I tried this script http://forum.centos-webpanel.com/backup/(contrib)-restore-backup-scripts-for-web-folder-files-and-mysql-database/?action=post;last_msg=6046

Quote
#!/bin/bash
#
# RESTORE MYSQL BACKUP SCRIPT from central backup repository
# http://centos-webpanel.com
#
# Note: the database must exist before restored
#

# Root folder of backups repository (change accordly)
BACKUPROOT="/backups"

if [[ $# -ne 3 ]]; then
   echo "Usage: restore_mysql -d|-w|-m appanage appanage_mailer"
   echo "       where d=daily, w=weekly, m=monthly"
   echo "Example: restore_mysql -d user1 db1"
   echo "         will restore appanage_mailer mysql database from daily backup repository"
   exit 1
fi

case "$1" in
   -d)
     BACKUPTYPE="daily"
     ;;
   -w)
     BACKUPTYPE="weekly"
     ;;
   -m)
     BACKUPTYPE="monthly"
     ;;
   *)
      echo "Usage: restore_mysql -d|-w|-m appanage appanage_mailer"
      echo "       where d=daily, w=weekly, m=monthly"
      echo "Example: restore_mysql -d user1 db1"
      echo "         will restore user1_db1 mysql database from daily backup repository"
     exit 1
esac

USERNAME=$2
DBNAME=$3
MYSQLPWD=`grep password= /root/.my.cnf|awk -F= {'print $2'}`

# Some validations
if [ ! -e "$BACKUPROOT" ];then
   echo "Error: $BACKUPROOT not exists, cannot restore."
   exit 1
fi
if [ ! -e "$BACKUPROOT/mysql/$BACKUPTYPE" ];then
   echo "Error: $BACKUPROOT/mysql/$BACKUPTYPE not exists, cannot restore."
   exit 1
fi
if [ ! -e "$BACKUPROOT/mysql/$BACKUPTYPE/${USERNAME}_${DBNAME}.sql" ];then
   echo "Error: $BACKUPROOT/mysql/$BACKUPTYPE/${USERNAME}_${DBNAME}.sql not exists, cannot restore it."
   exit 1
fi

# Drop all tables from database
mysqldump -uroot -p"$MYSQLPWD" --add-drop-table --no-data ${USERNAME}_${DBNAME} | grep -e '^DROP \| FOREIGN_KEY_CHECKS' | mysql -uroot -p"$MYSQLPWD" ${USERNAME}_${DBNAME}

# Import mysql backup dump
mysql -uroot -p"$MYSQLPWD" ${USERNAME}_${DBNAME} < $BACKUPROOT/mysql/$BACKUPTYPE/${USERNAME}_${DBNAME}.sql

But I am getting this error

[root@mailer /]# /scripts/restore_db
-bash: /scripts/restore_db: /bin/bash^M: bad interpreter: No such file or directory

I have save the above script in restore_db file and uploaded that file to /scripts folder

Any help is highly appreciated please.

Offline
*
Re: Restore MYSQL Backup from Daily Backup
« Reply #1 on: January 23, 2018, 06:43:15 AM »
I see a ^M after /bin/bash - did you copy/paste script from Windows machine?
Use CentOS editor ‘nano’ or ‘mcedit’ from ‘mc’ package to remove unwanted ^M (DOSCarrige Return)