Debian11.3 : System Backup and Restore

1. System Backup

1.1 Backup under /var/www/html

① Create backup_all.sh script under /opt

# vi /opt/backup_all.sh

backup_all.sh script contents
10-day data storage

#!/bin/bash

#===========================================================
# :initialization
#===========================================================

#date processing
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/www_bak"    #Backup File Output Destination
RUN_FLAG="/var/www/backup/www_bak/www_flag"

#===========================================================
# : backup operation
#===========================================================

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

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

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

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

/bin/rm -f ${RUN_FLAG}

②Create a directory to store backup files

# chmod 777 /opt/backup_all.sh
# mkdir -p /var/www/backup/www_bak
# chmod 777 /var/www/backup/www_bak

③Add settings to cron for periodic backups

# crontab -e
0 6 * * * /opt/backup_all.sh > /dev/null 2>&1

1.2 Mysql database backup

①Create db_backup.sh script under /opt

# vi /opt/db_backup.sh

db_backup.sh script contents
Data storage for 10 days

#!/bin/bash

#===========================================================
# : initialization of the environment
#===========================================================

export LANG=ja_JP.eucJP
export PATH="$PATH":/usr/bin
export MYSQL_HOME=/etc/mysql
export MYLIB=/usr/lib/mysql
export MANPATH=":wq/usr/share/man"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}":"MYLIB"

#MYSQL Connection Settings
MYUSER="root" #MySQL connection user name
MYPASS="********" #MySQL connection password

#Date processing related
TIME_STRING=`/bin/date '+%Y%m%d'`
DAY_AGO=`/bin/date --date '10day ago' +%Y%m%d`

#Output File Related
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

#===========================================================
#  : backup operation
#===========================================================

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

mysqldump -u${MYUSER} -p${MYPASS} --all-databases --events > ${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 a directory to store backup files

# chmod 777 /opt/db_backup.sh
# mkdir -p /var/www/backup/db_bak
# chmod 777 /var/www/backup/db_bak

③Add settings to cron for periodic backups

# crontab -e
0 7 * * * /var/www/system/db_backup.sh > /dev/null 2>&1

2. System Restore

2.1 Restore backup files under HTML

① Store HTML backup files used for backup in the "/ (root)" directory
Select the backup file with the latest timestamp (e.g. www_back_20220501.tar.gz)

# cd /var/www/backup/www_bak
# cp www_back_20220501.tar.gz /
# cd /
# tar zxvf www_back_20220501.tar.gz

2.2 Restore MySQL database

①Save DB backup file to any directory and extract data

# cd /var/www/backup/db_bak
# gzip -d mysql_backup_20220501.dump.gz

②Restore MySQL

# mysql -u root -p < mysql_backup_20220501.dump
Enter password: ←Enter root password for MySQL (not shown)
タイトルとURLをコピーしました