Contents
1. System backup
1.1 Backup under /var/www/html
①Create the /var/www/system directory
1 |
# mkdir /var/www/system |
②Create a script "backup_all.sh" under /var/www/system
1 |
# vi /var/www/system/backup_all.sh |
Contents of the "backup_all.sh" script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#!/bin/bash #=========================================================== # : Environment initialization #=========================================================== #Date processing TODAY=`/bin/date +%Y%m%d` DAY_AGO=`/bin/date --date '7day ago' +%Y%m%d` #Backup source-related HTML_DIR="/var/www/html" #Output file related BACKUP_DIR="/var/www/backup/www_bak" #Backup file output location RUN_FLAG="/var/www/backup/www_bak/www_flag" #Processing flag #=========================================================== # : Backup process #=========================================================== /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} |
1 2 3 |
# chmod 777 /var/www/system/backup_all.sh # mkdir -p /var/www/backup/www_bak # chmod 777 /var/www/backup/www_bak |
1 2 |
# crontab -e 0 6 * * * /var/www/system/backup_all.sh > /dev/null 2>&1 |
1.2 Mysql Database Backup
①Create a script "db_backup.sh" under /var/www/system
1 |
# vi /var/www/system/db_backup.sh |
Contents of "db_backup.sh" script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#!/bin/bash #=========================================================== # : Initial environment settings #=========================================================== 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" #------------ Edit items start---------------------------------------- #MYSQL接続設定 MYUSER="root" #MySQL connection user name MYPASS="********" #MySQL connection password #------------ Edit items end ---------------------------------------- #Date processing related TIME_STRING=`/bin/date '+%Y%m%d'` DAY_AGO=`/bin/date --date '7day ago' +%Y%m%d` #Output file related DB_BAK_DIR="/var/www/backup/db_bak" #Backup file output location RUN_FLAG="${DB_BAK_DIR}/db_my_flag" #Processing flag MYSQL_BACKUP_FILE=${DB_BAK_DIR}/mysql_backup_"${TIME_STRING}".dump #=========================================================== # # STEP020 : Backup process # #=========================================================== /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} |
1 2 3 |
# chmod 777 /var/www/system/db_backup.sh # mkdir -p /var/www/backup/db_bak # chmod 777 /var/www/backup/db_bak |
1 2 |
# crontab -e 0 7 * * * /var/www/system/db_backup.sh > /dev/null 2>&1 |
2. System Restore
2.1 Restore files under HTML with backup files
① Store HTML backup files used for backup in the "/ (root)" directory
Select the backup file with the latest timestamp (e.g. www_back_20211201.tar.gz)
1 2 3 4 |
# cd /var/www/backup/www_bak # cp www_back_20211201.tar.gz / # cd / # tar zxvf www_back_20211201.tar.gz |
2.2 Restore MySQL database
①Save the Mysql database backup file to any directory and unzip the data
1 2 |
# cd /var/www/backup/db_bak # gzip -d mysql_backup_20211201.dump.gz |
1 2 |
# mysql -u root -p < mysql_backup_20211201.dump Enter password: ←Enter the root password for MySQL (not shown) |