MiracleLinux ; System Backup & Restore

Web configuration file (under html) backup

①Create a backup script file under html

# cd /var/www/system/
# vi html_backup.sh
Contents of html_backup.sh

#!/bin/bash
#Date processing (save 10 days)
TODAY=`/bin/date +%Y%m%d`
DAY_AGO=`/bin/date –date ’10day ago’ +%Y%m%d`

#backup source
HTML_DIR=”/var/www/html”

#output file
BACKUP_DIR=”/var/www/backup/html_bak”
RUN_FLAG=”/var/www/backup/html_bak/html_flag”

/bin/mkdir -p ${BACKUP_DIR}
/bin/touch ${RUN_FLAG}

/bin/tar vfcz ${BACKUP_DIR}/html_back_${TODAY}.tar.gz ${HTML_DIR}

/bin/chmod 644 ${BACKUP_DIR}/html_back_${TODAY}.tar.gz

if [ -f ${BACKUP_DIR}/html_back_${DAY_AGO}.tar.gz ];
then
/bin/rm -f ${BACKUP_DIR}/html_back_${DAY_AGO}.tar.gz
fi

/bin/rm -f ${RUN_FLAG}

Create and authorize backup storage directories

# cd /var/www/system/
# mkdir -p /var/www/backup/html_bak
# chmod 777 /var/www/backup/html_bak
# chmod 700 html_backup.sh
②Execute backup process.
# /var/www/system/html_backup.sh
③Add configuration to cron and backup periodically
# crontab -e
0 3 * * * /var/www/system/html_backup.sh > /dev/null 2>&1

MySQL database backup

①Create a backup script file
# cd /var/www/system
# vi db_my_backup.sh
Contents of db_my_backup.sh

#!/bin/bash

#MYSQL Connection
MYUSER=”root”
MYPASS=”<root password>”

#Date processing (save 10 days)
TIME_STRING=`/bin/date ‘+%Y%m%d’`
DAY_AGO=`/bin/date –date ’10day ago’ +%Y%m%d`

#output file
DB_BAK_DIR=”/var/www/backup/db_bak” #Backup file output destination
RUN_FLAG=”${DB_BAK_DIR}/db_my_flag”

MYSQL_BACKUP_FILE=${DB_BAK_DIR}/mysql_backup_”${TIME_STRING}”.dump

/bin/mkdir -p ${DB_BAK_DIR}
/bin/touch ${RUN_FLAG}

mysqldump -u${MYUSER} -p${MYPASS} –all-databases > ${MYSQL_BACKUP_FILE}
/bin/gzip ${MYSQL_BACKUP_FILE}

if [ -f ${DB_BAK_DIR}/${MT_DB}”${DAY_AGO}”.dump.gz ];
then
/bin/rm -f ${DB_BAK_DIR}/${MT_DB}”${DAY_AGO}”.dump.gz
fi

if [ -f ${DB_BAK_DIR}/${XOOPS_DB}”${DAY_AGO}”.dump.gz ];
then
/bin/rm -f ${DB_BAK_DIR}/${XOOPS_DB}”${DAY_AGO}”.dump.gz
fi

if [ -f ${DB_BAK_DIR}/${GEEKLOG_DB}”${DAY_AGO}”.dump.gz ];
then
/bin/rm -f ${DB_BAK_DIR}/${GEEKLOG_DB}”${DAY_AGO}”.dump.gz
fi

/bin/rm -f ${RUN_FLAG}

Create and authorize backup storage directories

# chmod 700 db_my_backup.sh
# mkdir -p /var/www/backup/db_bak
# chmod 777 /var/www/backup/db_bak
②Execute backup process.
# /var/www/system/db_my_backup.sh
③Add configuration to cron and backup periodically
# crontab -e
0 7 * * * /var/www/system/db_my_backup.sh > /dev/null 2>&1

Restore web configuration files (under html)

① Store the HTML backup files used for the restore in the server’s “/ (root)” directory.
(root) directory of the server.

# cd /var/www/backup/html_bak
# cp html_back_<time-stamp>.tar.gz /

②Extract the file

# cd /
# tar zxvf html_back_<time-stamp>.tar.gz

Restore MySQL data

① Save the database backup file to any directory.
# cd /var/www/backup/db_bak
# gzip -d mysql_backup_<time-stamp>.dump.gz
② Restore MySQL
# mysql -u root -p < mysql_backup_<time-stamp>.dump
Enter password: ← Enter the root password for MySQL.
タイトルとURLをコピーしました