Author Topic: [CONTRIB] Restore Backup scripts for web folder files and Mysql database  (Read 1648 times)

0 Members and 1 Guest are viewing this topic.

Offline
*
Hello, until a more suitable solution, I want to share my two backup restore scripts.
When you must restore a big number of domains, It is a great help.

Note: It is assumed that you previously created all users and databases to restore. The restore files script will keep owner and permissions, and the mysql script will drop all tables from database without delete the database (keeping user permissions).

Script 1: restore_backup.sh

Code: [Select]
#!/bin/bash
#
# RESTORE BACKUP SCRIPT from central backup repository
# http://centos-webpanel.com
#

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

if [[ $# -ne 3 ]]; then
   echo "Usage: restore_backup -d|-w|-m username webfolder"
   echo "       where d=daily, w=weekly, m=monthly"
   echo "Example: restore_backup -d user1 public_html"
   echo "         will restore public_html folder files for user1 from daily backup repository"
   exit 1
fi

case "$1" in
   -d)
     BACKUPTYPE="daily"
     ;;
   -w)
     BACKUPTYPE="weekly"
     ;;
   -m)
     BACKUPTYPE="monthly"
     ;;
   *)
     echo "Usage: restore_backup -d|-w|-m username webfolder"
     echo "       where d=daily, w=weekly, m=monthly"
     echo "Example: restore_backup -d user1 public_html"
     echo "         will restore public_html folder files for user1 from daily backup repository"
     exit 1
esac

USERNAME=$2
WEBFOLDERNAME=$3
GETHOMEDIR=`grep $USERNAME /etc/passwd|awk -F: {'print $6'}`

# Some validations
if [ -z "$GETHOMEDIR" ]; then
   echo "Error: home directory for user $USERNAME not found, you must create the user before restore it's files."
   exit 1
fi
if [ ! -e "$GETHOMEDIR" ];then
   echo "Error: $GETHOMEDIR not exists, you must create the user before restore it's files."
   exit 1
fi
if [ ! -e "$BACKUPROOT" ];then
   echo "Error: $BACKUPROOT not exists, cannot restore."
   exit 1
fi
if [ ! -e "$BACKUPROOT/$BACKUPTYPE" ];then
   echo "Error: $BACKUPROOT/$BACKUPTYPE not exists, cannot restore."
   exit 1
fi
if [ ! -e "$BACKUPROOT/$BACKUPTYPE/$USERNAME" ];then
   echo "Error: $BACKUPROOT/$BACKUPTYPE/$USERNAME not exists, cannot restore it."
   exit 1
fi
if [ ! -e "$BACKUPROOT/$BACKUPTYPE/$USERNAME/$WEBFOLDERNAME" ];then
   echo "Error: $BACKUPROOT/$BACKUPTYPE/$USERNAME/$WEBFOLDERNAME not exists, cannot restore it."
   exit 1
fi

# Delete Webfolder only if it exists
if [ -e "/home/$USERNAME/$WEBFOLDERNAME" ];then
    rm -rf /home/$USERNAME/$WEBFOLDERNAME/
fi

# Restore Backup Files with its permissions
cp -ax $BACKUPROOT/$BACKUPTYPE/$USERNAME/$WEBFOLDERNAME /home/$USERNAME

# Restore owner of files
chown $USERNAME:nobody /home/$USERNAME/$WEBFOLDERNAME
chown -R $USERNAME:$USERNAME /home/$USERNAME/$WEBFOLDERNAME/*

Script 2: restore_mysql.sh

Code: [Select]
#!/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 username dbname"
   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
fi

case "$1" in
   -d)
     BACKUPTYPE="daily"
     ;;
   -w)
     BACKUPTYPE="weekly"
     ;;
   -m)
     BACKUPTYPE="monthly"
     ;;
   *)
   echo "Usage: restore_mysql -d|-w|-m username dbname"
   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

Corrections and improvements are welcome
Hope it helps.