#!/bin/bash
######################################################################
#
# https://hasu0707.duckdns.org의 백업 사이트를 구축한다.
#
######################################################################
CURRENT_DIR=`pwd -P`
SITE_URL="hasu0707.duckdns.org"
TIMESTAMP_STR1=`date "+%Y%m%d-*_backup_www-${SITE_URL}.tar.gz"`
TIMESTAMP_STR2=`date "+%Y%m%d-*_backup_db-${SITE_URL}.sql.gz"`
TMP_DIR="/tmp_work"
WEBDAV_ID="armbian"
WEBDAV_PW='myPass@12'
WEBDAV_URL="https://${SITE_URL}:5005/backup/${SITE_URL}"
DB_ROOTPW='myPass@12'
DB_NAME="hasu0707"
DB_USER="hasu0707"
DB_USERPW='myPass@12'
get_system_ip_addr() {
echo "######################################################################"
echo "#"
echo "# IP주소 알아내기"
echo "#"
echo "######################################################################"
SYSTEM_IP_ADDR=$(hostname -I | awk '{print $1}')
if [ -z ${SYSTEM_IP_ADDR} ]
then
echo "ERROR: Unknown IP Address."
exit 1
fi
echo "IP Address: ${SYSTEM_IP_ADDR}"
}
mysql_root_login_test() {
echo "######################################################################"
echo "#"
echo "# mysql root 계정 로그인 테스트"
echo "#"
echo "######################################################################"
mysql -uroot -p${DB_ROOTPW} -e "exit"
if [ $? -ne 0 ];
then
echo "mysql root 비밀번호가 이 스크립트와 맞지 않습니다. 아래와 같은 방법으로 변경해 주세요."
echo "ex) mysqladmin -u root -p password 'P@ss1234'"
exit 1
fi
}
download_backup() {
echo "######################################################################"
echo "#"
echo "# 데이터 다운로드"
echo "#"
echo "######################################################################"
# 임시 디렉토리 초기화
rm -rf ${TMP_DIR}
mkdir ${TMP_DIR}
wget -P ${TMP_DIR} --no-check-certificate --user ${WEBDAV_ID} --password ${WEBDAV_PW} -r -A "${TIMESTAMP_STR1}" "${WEBDAV_URL}"
if [ $? -ne 0 ];
then
echo "Failed to download-www"
exit 1
fi
wget -P ${TMP_DIR} --no-check-certificate --user ${WEBDAV_ID} --password ${WEBDAV_PW} -r -A "${TIMESTAMP_STR2}" "${WEBDAV_URL}"
if [ $? -ne 0 ];
then
echo "Failed to download-db"
exit 1
fi
mv ${TMP_DIR}/${SITE_URL}:5005/backup/${SITE_URL}/*.gz ${TMP_DIR}
rm -rf ${TMP_DIR}/${SITE_URL}:5005
sync
}
uncompress_backup() {
echo "######################################################################"
echo "#"
echo "# 압축 해제"
echo "#"
echo "######################################################################"
mkdir ${TMP_DIR}/1
mkdir ${TMP_DIR}/2
tar -C ${TMP_DIR}/1 -xzf ${TMP_DIR}/*.tar.gz
gzip -d ${TMP_DIR}/*.sql.gz
mv ${TMP_DIR}/*.sql ${TMP_DIR}/2
rm -f ${TMP_DIR}/*.tar.gz
}
install_www() {
echo "######################################################################"
echo "#"
echo "# /var/www 설치"
echo "#"
echo "######################################################################"
rm -rf /var/www
mv ${TMP_DIR}/1/var/www /var
chown -R armbian:armbian /var/www
}
change_hostname() {
echo "######################################################################"
echo "#"
echo "# 호스트 명 변경"
echo "#"
echo "######################################################################"
sed -i "s/${SITE_URL}/${SYSTEM_IP_ADDR}/g" /var/www/index.html
}
install_db() {
echo "######################################################################"
echo "#"
echo "# DB 설치"
echo "#"
echo "######################################################################"
# 기존 DB 삭제
echo "DROP DATABASE IF EXISTS ${DB_NAME};" > /tmp/tmp.sql
echo "DROP USER IF EXISTS '${DB_USER}'@'localhost';" >> /tmp/tmp.sql
echo "DROP USER IF EXISTS '${DB_USER}'@'%'" >> /tmp/tmp.sql
mysql -uroot -p${DB_ROOTPW} < /tmp/tmp.sql
# DB 생성
echo "CREATE DATABASE ${DB_NAME} CHARACTER SET utf8mb4;" > /tmp/tmp.sql
echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_USERPW}';" >> /tmp/tmp.sql
mysql -uroot -p${DB_ROOTPW} < /tmp/tmp.sql
# DB dump 밀어넣기
mysql -uroot -p${DB_ROOTPW} ${DB_NAME} < ${TMP_DIR}/2/*.sql
rm -f /tmp/tmp.sql
}
clean_data() {
echo "######################################################################"
echo "#"
echo "# 임시파일 삭제"
echo "#"
echo "######################################################################"
rm -rf ${TMP_DIR}
}
get_system_ip_addr
mysql_root_login_test
download_backup
uncompress_backup
install_www
change_hostname
install_db
clean_data