Click here for "Error Codes for Commercial Air Conditioners".

Rocky Linux 8.4 ; Data Backup and Restore

1.Web site data backup

1-1.Backup under /var/www/html

①Create a backup script file

[root@Lepard ~]# cd /opt
opt]# vi backup_html.sh
#!/bin/bash

TODAY=`/bin/date +%Y%m%d`
DAY_AGO=`/bin/date –date ’10day ago’ +%Y%m%d`

HTML_DIR=”/var/www/html”

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}

➁authorized permission

[root@Lepard ~]# cd /opt
opt]# chmod 700 backup_html.sh

➂Create storage directory

[root@Lepard ~]# mkdir -p /var/www/backup/html_bak
[root@Lepard ~]# chmod 777 /var/www/backup/html_bak

④periodic execution
Add configuration to cron and backup periodically

[root@Lepard ~]# crontab -e
0 6 * * * /opt/backup_html.sh > /dev/null 2>&1  ←Runs backups every day at 6 a.m.

2.MySQL database backup

①Create a backup script file

[root@Lepard ~]# cd /opt
[root@Lepard ~]# vi db_my_backup.sh
#!/bin/bash

export LANG=ja_JP.eucJP
export PATH=”$PATH”:/usr/local/mysql/bin
export MYSQL_HOME=/usr/local/mysql
export MYLIB=${MYSQL_HOME}/lib
export MANPATH=”${MANPATH}”:${MYSQL_HOME}/man
export LD_LIBRARY_PATH=”${LD_LIBRARY_PATH}”:”${MYLIB}”

MYUSER=”root” #MySQL connection user name
MYPASS=”xxxxxxxxxxx” #MySQL connection password

TIME_STRING=`/bin/date ‘+%Y%m%d’`
DAY_AGO=`/bin/date –date ‘7day ago’ +%Y%m%d`

DB_BAK_DIR=”/var/www/backup/db_bak”
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}

➁authorized permission

[root@Lepard ~]# cd /opt
opt]# chmod 700 db_my_backup.sh

➂Create storage directory

[root@Lepard ~]# mkdir -p /var/www/backup/db_bak
[root@Lepard ~]# chmod 777 /var/www/backup/db_bak

④periodic execution
Add configuration to cron and backup periodically

[root@Lepard ~]# crontab -e
0 7 * * * /opt/db_my_backup.sh > /dev/null 2>&1  ←Perform backup at 7:00 a.m. every day

3.Restore backup files under html

① Store HTML backup files in the “/ (root)” directory

[root@Lepard ~]# cd /var/www/backup/html_bak
html_bak]# cp html_back_20211010.tar.gz /    ←Specify the backup file with the latest date.

➁Extract the file

html_bak]# cd /
[root@Lepard  /]# tar zxvf  html_back_20211010.tar.gz

4.Restore Mysql backup file

①Save the DB backup file in a directory of your choice and extract the data.
(In the example, the directory is “/var/www/backup/db_bak”.) Unzip the data.

[root@Lepard ~]# cd /var/www/backup/db_bak
db_bak]# gzip -d mysql_backup_20211010.dump.gz  ←Specify the backup file with the latest date.

➁Restore MySQL

db_bak]# mysql -u root -p < mysql_backup_20211010.dump
Enter password: ←Root password for MySQL
タイトルとURLをコピーしました