Commit 3caad3d7 by PotatoGim

add a script for system backup.

parent a1f0dc8a
#!/bin/bash
#===========================================================================
# FILE: backup.sh
# USAGE: ./backup.sh
# DESCRIPTION: 시스템 백업을 위한 스크립트
# OPTIONS: ---
# VERSIOH: 0.01
# NOTES: ---
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# COPYRIGHT: Copyleft(c) 2015 by Ji-Hyeon Gim.
# LICENSE: GNU General Public License v2.0
# DATE: 2015년 11월 09일 12:58
#===========================================================================
##############################################################################
# Preprocess
##############################################################################
DATE=`date +"%Y%m%d"`
PREV_DATE=`date --date '7 days ago ' +"%Y%m%d"`
BACKUP_DIR="/home/potatogim/backup"
SPARE_DIR="/mnt/data/spare"
LOG_DIR="/var/log"
LOG_FILE="${LOG_DIR}/backup_${DATE}.log"
OLD_LOG_FILE="${LOG_DIR}/backup_${PREV_DATE}.log"
##############################################################################
# Ready to log
##############################################################################
if [ -f $LOG_FILE ]; then
rm -f $LOG_FILE
fi
if [ -f $OLD_LOG_FILE ]; then
rm -f $OLD_LOG_FILE
fi
exec 3<>$LOG_FILE
exec 1>&3
exec 2>&3
##############################################################################
# Backup MySQL database
##############################################################################
echo ">>>>> Backup Database Now..."
echo ">> mysqldump -u root --all-databases > ${BACKUP_DIR}/db_${DATE}.sql"
mysqldump -u root --all-databases 1>${BACKUP_DIR}/db_${DATE}.sql
echo ">> chown root.root ${BACKUP_DIR}/db_${DATE}.sql"
chown root.root ${BACKUP_DIR}/db_${DATE}.sql
echo ">> chmod 755 ${BACKUP_DIR}/db_${DATE}.sql"
chmod 755 ${BACKUP_DIR}/db_${DATE}.sql
echo ">> rm -f ${BACKUP_DIR}/db_${DATE}.sql"
rm -f ${BACKUP_DIR}/db_${PREV_DATE}.sql
##############################################################################
# Backup www
##############################################################################
echo ">>>>> Backup www Now..."
echo ">> tar -cpzf ${BACKUP_DIR}/www_${DATE}.tgz -C /mnt/data www"
tar -cpzvf ${BACKUP_DIR}/www_${DATE}.tgz -C /mnt/data www
echo ">> rm -f ${BACKUP_DIR}/www_${PREV_DATE}.tgz"
rm -f ${BACKUP_DIR}/www_${PREV_DATE}.tgz
###
# Backup repository
#
echo ">>>>> Backup Repository Now..."
echo ">> tar -cpzf ${BACKUP_DIR}/git_${DATE}.tgz -C /home git"
tar -cpzvf ${BACKUP_DIR}/git_${DATE}.tgz -C /home git
echo ">> rm -f ${BACKUP_DIR}/git_${PREV_DATE}.tgz"
rm -f ${BACKUP_DIR}/git_${PREV_DATE}.tgz
##############################################################################
# Backup configurations
##############################################################################
echo ">>>>> Backup Configurations Now..."
LIST[0]="/etc/nginx"
LIST[1]="/etc/mysql"
for i in "${LIST[@]}"; do
echo ">> tar -rpf ${BACKUP_DIR}/conf_${DATE}.tar $i"
tar -rpvf ${BACKUP_DIR}/conf_${DATE}.tar $i
done;
echo ">> rm -f ${BACKUP_DIR}/conf_${PREV_DATE}.tar"
rm -f ${BACKUP_DIR}/conf_${PREV_DATE}.tar
##############################################################################
# Create backup spare
##############################################################################
echo ">>>>> Creating Backup Spare..."
echo ">> rsync -avz --del ${BACKUP_DIR}/ ${SPARE_DIR}/"
rsync -avz --del ${BACKUP_DIR}/ ${SPARE_DIR}/
##############################################################################
# Adjust owner & permissions
##############################################################################
chown root.root ${SPARE_DIR}/* 2>/dev/null
chown potatogim.potatogim ${SPARE_DIR}2 2>/dev/null
exec 3>&-
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment