How to create automated Linux/Unix backups 자동화를 만드는 방법에 리눅스 / 유닉스 백업

by Sukrit Dhandhania on November 18, 2008 Sukrit Dhandhania 2008년 11월 18일에 의해

리눅스

“A stitch in time saves nine”, goes an old saying. "시간에 바늘을 9", 옛 속담을 절약할 수있습니다. In the world of computers we refer to that stitch as backups. 우리는 백업 컴퓨터의 세계로 스티치를 참조하십시오. When running a production IT infrastructure, it is imperative that we achieve an uptime on our servers and applications of as close to a hundred percent as possible. IT 인프라 때, 그것의 첩경이라고 우리는 최대한의 100 %에 가까운 우리의 서버와 애플 리케이션에 대한 생산 실행 시스템 가동 시간을 달성했다. While there are a number of ways to make an IT infrastructure solid, it is impossible to make it failure proof. 반면 IT 인프라에는 고체하게하는 방법에는 여러가지가있다, 그것이 실패를 증명하게하는 것은 불가능합니다. So we make a plan B. 그래서 우리가 만드는 차선책

Servers are made of three broad layers - the hardware, the operating system and applications, and the data. 3 광범위한 계층의 만들어진 서버 - 하드웨어, 운영 체제 및 애플 리케이션과 데이터. In case of a hardware failure it is rather simple to replace the machine with a new one. 차라리 하나를 가진 시스템을 대체할 새로운 하드웨어 오류의 경우는 간단하다. Installing the operating system and applications too are relatively simple. 운영 체제 및 애플 리케이션 설치도 비교적 간단합니다. The part that is hard to replace is the data and configuration that your applications use. 그 대신 데이터 및 구성하여 애플 리케이션을 사용하는 어려운 부분입니다. In case of failure this is the part that can not be replaced. 실패의 경우는 대체될 수없고이 부분입니다. So we backup that data on a regular basis. 그래서 우리는 정기적으로 데이터 백업. We back it up in such a way that it becomes easy to retrieve in case of emergency. 우리가 이런식으로 그 비상의 경우를 검색할 백업 쉬워집니다.

I will guide you through writing a script to take daily backups on your system. 당신의 시스템에 일일 백업을 받아들이 기엔 좀 늦은 원고 집필을 통해 여러분을 안내할 것입니다. For this exercise I will use a server that runs the Apache web server, a MySQL database server, and has a directory on the /home partition that contains some data that needs to be backed up. 나는 아파치 웹 서버, MySQL 데이터베이스 서버를 실행하는 서버를 사용하고 그 요구를 백업한되고 일부 데이터가 들어있는 / home 파티션에있는 디렉터리가이 운동을하십시오. We will also backup the previous day’s Apache access logs in a separate backup directory. 우리는 또한 전날의 아파치 접근 로그는 별도의 백업 디렉토리에 백업된다. First, we need to create a directory where our backups will reside. 첫째, 우리는 어디에 상주하게되며 우리의 백업 디렉터리를 만들어야합니다. As part of the exercise I have created a directory called BACKUP on the /opt partition. 난 디렉토리가 / 파티션을 선택할에서 백업라는 운동의 일환으로 만들었습니다. We will organize the backups in this directory so that each days backup resides in a directory formed like this - /opt/BACKUP/YEAR/MONTH/DATE/ . 각 일이 같은 형성에있는 백업 디렉터리 그래서 우리는이 디렉토리에 백업을 조직할 것입니다 - / 수신 거부 / 백업 / 년 / 월 / 일 /. So on the 11th of August 2008 the backups will go into /opt/BACKUP/2008/08/11/ . 그래서 2008 년 8 월 11 일 백업 / opt/BACKUP/2008/08/11 안으로 가라앉을 것이다 /. Let’s create these directories. 이 디렉토리를 만들 자고했다. Run the following set of commands as the root user. 루트 사용자로 명령의 실행을 다음 설정합니다.

# mkdir -p /opt/backup/DATA/2008 #에서 mkdir - p 서비스 / opt/backup/DATA/2008
# cd /opt/backup/DATA/2008 # CD / 비디오 opt/backup/DATA/2008
# mkdir 01 02 03 04 05 06 07 08 09 10 11 12 #에서 mkdir 01 02 03 04 05 06 07 08 09 10 11 12
# cd /opt/backup/DATA/ # CD / 비디오 수신 거부 / 백업 / 데이터 /
# cp -r 2008 2009 #은 cp - R은 2008 2009
# cd /opt/backup/ # CD / 비디오 수신 거부 / 백업 /
# cp -r DATA LOG #은 cp - R에 데이터를 기록

See note below before issuing this last command. 이 마지막 명령을 발급하기 전에 아래 참조하십시오.

# chown -R calvin.calvin /opt/backup # chown - R을 calvin.calvin / 수신 거부 / 백업

We now have two sets of backup directories - one for data and another for log files. 이제 백업 디렉터리의 두 세트 - 하나의 데이터에 대한 다른 로그 파일에 대한. With the backups directories created we are now ready to start writing the script. Note: I’m using the user calvin throughout this article. 백업 디렉터리와 함께 이제 우리는 준비가 만든 스크립트를 쓰기 시작했다. 참고 :이 기사를 통해 사용자가 캘빈를 사용하고있습니다. Please replace calvin with your own username. 제발 자신의 사용자 이름과 캘빈 바꿉니다.


########################################################### ################################################## #########
# CODE BEGINS HERE # 코드 시작 여기
########################################################### ################################################## #########

#!/bin/bash #! / 빈 / bash는

# SERVER DATA BACKUP V1.0 # 서버 데이터 백업 v1.0을

#########################
# BACKUP CONFIG # 백업 CONFIG
#########################

#Configure the month, date, and day # 구성 달, 날짜, 그리고 하루
YEAR=`date +”%Y”` # 2008 년 =`데이트 + "% Y"는`# 2008
MONTH=`date +”%m”`# 11 한달 =`데이트 + "% M"이`# 11
DAY=`date +”%d”` # 14 하루 =`데이트 + "% d는"`# 14
YESTERDAY=`date –date=”yesterday” +%Y-%m-%d` # 2008-11-13 어제 =`데이트 - 날짜 = "어제"+ % Y - % 난 - % d 개의`# 2008년 11월 13일
YESTERDAY_DATE=`date –date=yesterday +%d` # 13 YESTERDAY_DATE =`데이트 - 날짜 = 어제 + % d 개의`# 13
YESTERDAY_MONTH=`date –date=yesterday +%m` # 11 YESTERDAY_MONTH =`데이트 - 날짜 = 어제 + % 난`# 11
YESTERDAY_YEAR=`date –date=yesterday +%Y` # 2008 YESTERDAY_YEAR =`데이트 - 날짜 = 어제 + % Y`# 2008
TODAY=`date +%Y-%m-%d` # 2008-11-14 오늘 =`데이트 + % Y - % 난 - % d 개의`# 2008년 11월 14일
TODAY_DATE=`date +%d` # 14 TODAY_DATE =`데이트 + % d 개의`# 14

# directories to backup to 백업 # 디렉토리
DATA_BACKUP_PARENT_DIR=/opt/backup/DATA/ # <-- Change this your data backup dir DATA_BACKUP_PARENT_DIR = / 수신 거부 / 백업 / 데이터 / # <- 변경이 귀하의 데이터 백업에 dir
LOG_BACKUP_PARENT_DIR=/opt/backup/LOG/ # <-- Change this your log backup dir LOG_BACKUP_PARENT_DIR = / 수신 거부 / 백업 / 로그 / # <-이 귀하의 로그 백업을 해줄 변경

# Apache Document Root Directory # 아파치 문서 루트 디렉토리
APACHE_DOCROOT=/var/www/html/ APACHE_DOCROOT =는 / var / www /에서 html로 /

# MySQL Database config # MySQL 데이터베이스 구성
DB_IP="localhost" # <-- Change this to the IP of your database server DB_IP = "로컬"<- 당신의 데이터베이스 서버의 IP 방법이 변경 #
DB_USER="calvin" # <-- Change this to your database username DB_USER = "캘빈"<- 당신의 데이터베이스 사용자 이름이 변경 #
DB_PASS="calvin_password" # <-- Change this to your database password DB_PASS = "calvin_password"<- 당신의 데이터베이스에 암호 방법이 변경 #
DB_NAME="clientlist" # <-- Change this to your database name DB_NAME = "clientlist"<- 당신의 데이터베이스 이름이 변경 #

# Apache Log Dir # 아파치 로그에 dir
APACHE_LOG_DIR="/var/log/httpd/" # <-- Change this to the path data of your logs APACHE_LOG_DIR = "는 / var / log / httpd는 /"<- 귀하의 로그의 경로를 데이터로이 변경 #

# Application Files' Dir # 응용 프로그램 파일 '디렉토리
APP_FILES="/home/calvin/application_files/" # <-- Change this to the path of the data directory APP_FILES = "/ 가정 / 캘빈 / application_files /"<- 데이터 디렉터리의 경로로이 변경 #

# Config Dir # 구성 디렉토리
CONFIG_DIR="/etc/" # <-- Change this to the path data directory CONFIG_DIR = "는 / etc /"<- 경로 데이터 디렉토리에이 변경 #

# Mail report address # 메일 신고 주소
EMAIL_ID="you@youremailid.com" # <-- Change this to your e-mail ID EMAIL_ID = "you@youremailid.com"<- 귀하의 e - 메일 ID를이 변경 #

########################################################### ################################################## #########
# DATA BACKUP # 데이터 백업
########################################################### ################################################## #########

# Create and go into backup directory 만들기 # 및 백업 디렉터리로 가서
cd $DATA_BACKUP_PARENT_DIR/$YEAR/$MONTH $ DATA_BACKUP_PARENT_DIR / $ 년 / $ 달에 CD를
mkdir $DAY 에서 mkdir $ 하루
cd $DAY CD를 $ 하루

# Database backup # 데이터베이스 백업
mysqldump -h ${DB_IP} -u ${DB_USER} -p${DB_PASS} ${DB_NAME} > ${DB_NAME}.db mysqldump - (DB_USER) - (DB_PASS) $ (DB_NAME)> $ $ P는 (DB_NAME). DB를 $ (DB_IP)은 - u가 $ h
tar -zcvf ${DB_NAME}.tar.gz ${DB_NAME}.db 타르 - zcvf $ (DB_NAME). tar.gz $ (DB_NAME). DB를
rm -f ${DB_NAME}.db 하는 rm - f를 $ (DB_NAME). DB를

# /etc backup # / etc 해당 백업
tar -zcf etc.tar.gz /etc 타르 - zcf etc.tar.gz은 / etc

# Application backup # 응용 프로그램 백업
tar -zcf Apache_Doc_Root.tar.gz ${APACHE_DOCROOT} 타르 - zcf Apache_Doc_Root.tar.gz $ (APACHE_DOCROOT)
tar -zcf App_Files.tar.gz ${APP_FILES} 타르 - zcf App_Files.tar.gz $ (APP_FILES)

########################################################### ################################################## #########
# LOG BACKUP # 로그 백업
########################################################### ################################################## #########

# Create and go into backup directory 만들기 # 및 백업 디렉터리로 가서
cd $LOG_BACKUP_PARENT_DIR/$YESTERDAY_YEAR/$YESTERDAY_MONTH $ LOG_BACKUP_PARENT_DIR / $ YESTERDAY_YEAR / $ YESTERDAY_MONTH CD를
mkdir $YESTERDAY_DATE 에서 mkdir $ YESTERDAY_DATE
cd $YESTERDAY_DATE $ YESTERDAY_DATE CD를

# HTTPD Log Backup # httpd는 로그 백업
for i in ${APACHE_LOG_DIR}/access_log.${YESTERDAY}*; do cp $i .; done $ (APACHE_LOG_DIR) / access_log에 대한. $ (어제) *; 할 전화기 $ i가.; 완료
for i in access_log.${YESTERDAY}*; do gzip $i; done access_log에 전. $ (어제) *; 내가 gzip을 $ 할; 완료

########################################################### ################################################## #########
# Send out mail notifications 메일 통지를 보내 #
########################################################### ################################################## #########

# Mail notify # 메일 통지
du -shc $DATA_BACKUP_PARENT_DIR/$YEAR/$MONTH/$DAY/* | mail -s “Backup for ${HOSTNAME}::DATA done :)” ${EMAIL_ID} 다다오 -. SHC $ DATA_BACKUP_PARENT_DIR / $ 년 / $ 월 / $ 주 / * | 메일 - "$위한 백업 (HOSTNAME) : : 데이터 다 :)"$ (EMAIL_ID)
du -shc $LOG_BACKUP_PARENT_DIR/$YESTERDAY_YEAR/$YESTERDAY_MONTH/$YESTERDAY_DATE/* | mail -s “Backup for ${HOSTNAME}::LOG done :)” ${EMAIL_ID} 다다오 -. SHC $ LOG_BACKUP_PARENT_DIR / $ YESTERDAY_YEAR / $ YESTERDAY_MONTH / $ YESTERDAY_DATE / * | 메일 - "$ (HOSTNAME 백업을위한) : : 한 일지 :)"$ (EMAIL_ID)

########################################################### ################################################## #########
# CODE ENDS HERE # 코드가 끝나는 여기
########################################################### ################################################## #########

Save this script in a directory such as /home/calvin/scripts/backup.sh. 디렉토리에로 / home / 캘빈 / 스크립트 / backup.sh 등이 스크립트를 저장합니다. Give executable permissions to the file: 파일 실행 권한을 부여합니다 :

# chmod +x /home/calvin/scripts/backup.sh # chmod를 +는 x와 / home / 캘빈 / 스크립트 / backup.sh

Now you are good to go. 지금은 잘 가고있습니다. Run the script form your command line: 스크립트를 실행하여 명령줄 양식 :

# /home/calvin/scripts/backup.sh # / 가정 / 캘빈 / 스크립트 / backup.sh

You can also optionally add this script as a cron job so that it will be executed on a daily basis at a time of your choice. 당신은 선택적으로 그렇게 매일 매일 당신의 선택의 시간에 실행됩니다 cron 작업으로이 스크립트를 추가할 수있습니다. If you want to run this script at 4 AM daily do the following: 이 스크립트를 실행하려는 경우 4 일간 다음과 같이 할시 :

# crontab -e # crontab을 - e로

Create a new line in the file that opens and add the following: 그 다음 추가를 열고 파일에 새로운 라인을 만들기 :

0 4 * * * /home/calvin/scripts/backup.sh > /dev/null 0 4 * * * / 가정 / 캘빈 / 스크립트 / backup.sh>는 / dev / null로

There you go. 여기요. Your server will now backup your data, database, configuration files, application files, and log files to /opt/backup everyday at 4 AM. 귀하의 서버가 이제 귀하의 데이터, 데이터베이스, 구성 파일, 응용 프로그램 파일, 백업 및 로그 파일 / 수신 거부 / 4 일 오전 2시 매일 백업합니다. A smart thing to do now would be to export these backups to a remote machine so that you don’t lose your backups if your server faces problems. 할 수있는 현명한 일이 이제는 원격 기계에 너무 귀하의 서버에 문제가있다면 귀하의 백업을 잃지 말고 이러한 백업을 수출하는 것이다. We will cover how to do that in my next article. 우리가 어떻게 할 수 커버 것이다 다음 문서에있습니다.

Related Posts: 관련 게시물 :
  • Increase your Linux/Unix Productivity: How to use crontab 여러분의 리눅스 / 유닉스의 생산성 증가율 : crontab을 사용하는 방법
  • Walkthrough: Time Machine for OS X 10.5 Leopard 연습 : 타임 머신 OS X의 10.5 레오파드에 대한
  • How to backup your files to your Dreamhost account (OS X) 귀하의 Dreamhost 계정 (OS X의) 파일을 백업하는 방법
  • How to create a custom Windows Vista Installation DVD Windows Vista를 설치하는 방법을 정의 DVD를 만듭니다
  • Review: BackBlaze backup service - 25 Beta Invites for Simple Help readers 리뷰 : BackBlaze 백업 서비스 - 간단한 도움말을 독자들에게 25 베타 초대
  • Get Simple Help tutorials just like this one in your email inbox every day - for free! 귀하의 이메일받은 편지로 매일 - 자유에 대한 간단한 도움말 튜토리얼이 하나처럼 들어가! Just enter your email address below: 바로 아래에 귀하의 이메일 주소를 입력 :

    You can always opt out of this email subscription at any time. 당신은 항상 언제든지이 이메일 구독의 선택을 취소할 수있습니다.


    Bookmark and Share 즐겨찾기 및 공유

    { 0 comments… (0 코멘트 ... add one now 지금 하나 추가 } )

    Leave a Comment 코멘트를 남겨주세요

    You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> 이러한 HTML 태그와 특성 : title="">은 <a href="" 사용할 수있습니다 <abbr title=""> <acronym title="">의 <b> <blockquote cite=""> <cite>의 <code> <del datetime="">의 <em>가 <i> <q cite=""> <strike>의 <strong>