Contents
1. Server Backup
1.1 Backup under /srv/www/
①Create backup script file
1 2 |
# cd /srv/www/system/ # vi www_backup.sh |
Contents of www_backup.sh
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 |
#!/bin/bash #Dates Processing TODAY=`/bin/date +%Y%m%d` DAY_AGO=`/bin/date --date '7day ago' +%Y%m%d` #backup source HTDOCS_DIR="/srv/www/htdocs" SYSTEM_DIR="/srv/www/system" #output file BACKUP_DIR="/srv/www/backup/www_bak" #Backup File Output Destination RUN_FLAG="/srv/www/backup/www_bak/www_flag" /bin/mkdir -p ${BACKUP_DIR} /bin/touch ${RUN_FLAG} /bin/tar vfcz ${BACKUP_DIR}/www_back_${TODAY}.tar.gz ${HTDOCS_DIR} ${SYSTEM_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 |
# chmod 700 www_backup.sh |
➁Create directory to store
1 2 |
# mkdir -p /srv/www/backup/www_bak # chmod 777 /srv/www/backup/www_bak |
➂Perform backup
1 |
# /srv/www/system/www_backup.sh |
④Add settings to cron for periodic backups
1 2 |
# crontab -e 0 6 * * * /srv/www/system/www_backup.sh > /dev/null 2>&1 |
1.2. MariaDB Database Backup
①Create backup script file
1 2 |
# cd /srv/www/system # vi db_my_backup.sh |
Contents of db_my_backup.sh
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 |
#!/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" #MariaDB connection username MYPASS="xxxxxxx" #MariaDB connection password TIME_STRING=`/bin/date '+%Y%m%d'` DAY_AGO=`/bin/date --date '10day ago' +%Y%m%d` DB_BAK_DIR="/srv/www/backup/db_bak" RUN_FLAG="${DB_BAK_DIR}/db_my_flag" MYSQL_BACKUP_FILE=${DB_BAK_DIR}/mariadb_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} |
1 |
# chmod 700 db_my_backup.sh |
➁Create directory to store
1 2 |
# mkdir -p /srv/www/backup/db_bak # chmod 777 /srv/www/backup/db_bak |
③Backup Execution
1 |
# /srv/www/system/db_my_backup.sh |
④Add settings to cron for periodic backups
1 2 |
# crontab -e 0 7 * * * /srv/www/system/db_my_backup.sh > /dev/null 2>&1 |
2.Data Restore
2.1 Restore backup files under www
① Save backup files used for restore to the "/ (root)" directory of the server
1 2 |
# cd /srv/www/backup/www_bak # cp www_back_[time-stamp].tar.gz / |
② Extract the file
1 2 |
# cd / # tar zxvf www_back_[time-stamp].tar.gz |
2.2 Restore MariaDB data
① Save the database backup file to any directory
Extract data
1 2 |
# cd /srv/www/backup/db_bak # gzip -d mariadb_backup_[time-stamp].dump.gz |
② Restore MariaDB database
1 2 |
# mysql -u root -p < mariadb_backup_[time-stamp].dump Enter password: ←MariaDB root password |