Search

'백업서버'에 해당되는 글 1건

  1. 2009.05.17 [Linux] ssh, rsync를 이용한 상호 원격 백업 설정

[sshrsync을 이용한 상호 원격 백업설정]


현재 두곳의 서버로 부터 호스팅을 이용하고 있다. 두 곳에서 블로그를 운영하고 있는데, 최근 백업에 대한 고민을 하게 됐다. 물론 서비스를 제공해 주시는 분들의 서버에 대한 안정성을 의심하거나 문제가 있어서는 아니다. 원래 백업이라는 것은 평소에 잘 해놓아야 하는 작업아닌가.


따로 백업서버가 없는 내 경우, 다행히도 두 곳에서 호스팅을 받기 때문에 두대의 서버를 각각에 대한 백업서버의 개념으로 설정하여 백업을 하기로 한다. 현재 운영중인 서비스는 아래와 같다

  • readytoact.gnukorea.com (서버1) : 블로그

  • readytoact.u1i.info (서버2) : 사진앨범


여러가지 방법으로 데이터를 백업하고 보존하는 방법이 있지만, 여기서는 sshrsync를 이용하여 작업하기로 한다. 각 용어나 명령어 추가 옵션에 대해서는 포럼과 인터넷을 참조 바란다


1.사전 점검사항

원격 백업 을 구축하기 위해서 다음 사항들이 기본적으로 준비되어 있어야 한다

  • 서버내 sshd rsync 데몬 작동

  • 원격에서 ssh 접속 가능

  • 유저 crontab 사용가능


백업작업 및 스크립트 사용을 위하여 아래와 같이 디렉토리 및 스크립트를 구성하였다.

디렉토리 경로

용도

/home/readytoact/bin

/home/readytoact/bin/backup_local.sh

로컬 데이터 백업 스크립트

/home/readytoact/bin/get_$remote.sh

원격 서버의 데이터를 가져오는 스크립트

/home/readytoact/bin/del_backup.sh

오래된 백업 데이터 삭제 스크립트

/home/readytoact/backup

/home/readytoact/backup/local

로컬서버의 백업파일 저장

/home/readytoact/backup/backup.$remotehost

가져온 백업 데이터를 저장

/home/readytoact/backup/log

작업 결과에 대한 오류 정보 저장


2. ssh 설정

원격 백업을 자동화 하기 위하여 ssh암호화 키를 생성, 각각의 서버가 서로 접속할 때 추가적인 인증(사용자의 패스워드 입력 작업 등)을 거치지 않도록 설정하는 과정이다.


    2.1 서버1(gnukorea)에서 서버2(u1i)로 자동접속 설정

    • ssh 설정을 위해 서버1로 접속한다

$ ssh readytoact@readytoact.gnukorea.com

readytoact@readytoact.gnukorea.com's password:

Last login: Sat May 16 11:06:36 2009 from 124.63.111.41


  • 암호키생성

$ ssh-keygen -t rsa : ssh 키 생성

Generating public/private rsa key pair.

Enter file in which to save the key (/home/readytoact/.ssh/id_rsa): : Enter

Created directory '/home/readytoact/.ssh'.

Enter passphrase (empty for no passphrase): : Enter

Enter same passphrase again: : Enter

Your identification has been saved in /home/readytoact/.ssh/id_rsa.

Your public key has been saved in /home/readytoact/.ssh/id_rsa.pub.

The key fingerprint is:

xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx: xx readytoact@remote

: ‘xx’에 암호화된 키값이 표시된다


$ ls -al .ssh : ssh-keygen 실행 후 홈 디렉토리내 .ssh가 생성된다

total 16

drwx------ 2 readytoact readytoact 4096 2009-05-16 11:15 .

drwxr-xr-x 6 readytoact readytoact 4096 2009-05-16 11:15 ..

-rw------- 1 readytoact readytoact 1675 2009-05-16 11:15 id_rsa

-rw-r--r-- 1 readytoact readytoact 398 2009-05-16 11:15 id_rsa.pub


  • id_rsa.pub (공용키)파일을 서버2~/.ssh/authorized_keys 이름으로 복사

  • 서버2ssh로그인

readytoact@hardy:~/.ssh$ ssh readytoact@readytoact.u1i.info

The authenticity of host 'readytoact.u1i.info (221.143.48.146)' can't be established.

RSA key fingerprint is e2:5b:be:17:8f:86:f3:e1:cb:6a:c0:37:80:00:de:eb.

Are you sure you want to continue connecting (yes/no)? yes

: 최초로그인시 나옴

Warning: Permanently added 'readytoact.u1i.info,221.143.48.146' (RSA) to the list of known hosts.

Linux ubuntu -server #1 SMP Thu Nov 27 19:19:15 UTC 2008 i686

Last login: Sat May 16 11:02:27 2009 from 124.63.111.41

readytoact@ubuntu:~$ exit

logout

Connection to readytoact.u1i.info closed.

readytoact@hardy:~/.ssh$ ssh readytoact@readytoact.u1i.info

: 다시 로그인하면 별도의 인증절차 없이 로그인이 가능하다

Linux ubuntu 2.6.24-23-server #1 SMP Thu Nov 27 19:19:15 UTC 2008 i686


Last login: Sat May 16 11:08:21 2009 from 211.189.69.37

readytoact@ubuntu:~$ cd .ssh

readytoact@ubuntu:~/.ssh$ ls -al

total 24

drwxr-xr-x 2 readytoact readytoact 4096 2009-05-16 11:28 .

drwxr-xr-x 8 readytoact readytoact 4096 2009-05-16 14:45 ..

-rw-r--r-- 1 readytoact readytoact 398 2009-05-16 11:07 authorized_keys : 복사된 암호키

-rw------- 1 readytoact readytoact 1675 2009-05-16 11:24 id_rsa

-rw-r--r-- 1 readytoact readytoact 399 2009-05-16 11:24 id_rsa.pub

-rw-r--r-- 1 readytoact readytoact 884 2009-05-16 11:28 known_hosts

readytoact@ubuntu:~$ exit

logout


    2.2 서버2(u1i)에서 서버1(gnukorea)로 자동접속 설정

    • 2.1 과정과 같다


3.rsync 설정

rsync는 원격지의 데이터를 로컬로 가져오는 rcp와 유사힌 기능을 수행한다. 이 명령줄은 스크립트내 삽입된다.

rsync -avuz --delete --stats -t -e "ssh -p 22" readytoact@readytoact.u1i.info:/home/readytoact/backup/local/ /home/readytoact/backup/backup.u1i/

: sshreadytoact.u1i.info에 접속하여 /home/readytoact/backup/local/ 디렉토리의 내용(파일)을 가져온다.

 rsync 옵션

    -a : archive mode - 심볼릭 링크, 속성, 퍼미션 등을 유지한다
    -v : verbose. 전송과정을 출력한다
    -u : 원본보다 새 파일이 있으면 그냥 skip.
    -z : compress(전송시 압축).
    --delete: 원본 디렉토리에 없는 파일은 없앤다.
    -t : mtime을 유지한다.
    -e : 명령어 실행옵션

 


 

4.백업스크립트

스크립트 작성후 실행권한을 주도록한다. 특별히, backup_local.sh의 경우 데이터베이스 패스워드가 노출될 위험이 있으므로 권한을 711로 설정한다.스크립트는 각각의 서버에 맞도록 수정(호스트이름 및 경로등)하여 작성한다.

(본문 스크립트는 서버1(gnukorea)으로 작성된다)


  • backup_local.sh  : 로컬 데이터 백업

#!/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

cd /home/readytoact/backup/local

date=`date +%Y%m%d`

mysqldump -uuser -ppasswd dbname > DB_$date.sql

tar cvzf DB_gnukorea_$date.tar.gz DB_$date.sql

tar cvzf DATA_gnukorea_$date.tar.gz /home/readytoact/html : 백업할 디렉토리 지정

rm -rf *.sql


  • get_u1i.sh : 원격 서버 백업 데이터 가져오기

#!/bin/sh

rsync -avz --delete --stats -l -t -e "ssh -p 22" readytoact@readytoact.u1i.info:/home/readytoact/backup/local/ /home/readytoact/backup/backup.u1i/


$get_u1i.sh : 스크립트 실행

receiving file list ...

4 files to consider

./

DATA_u1i_20090516.tar.gz

20827966 100% 11.18MB/s 0:00:01 (xfer#1, to-check=2/4)

DB_u1i_20090516.tar.gz

133 100% 0.17kB/s 0:00:00 (xfer#2, to-check=1/4)


Number of files: 4

Number of files transferred: 2

Total file size: 20828099 bytes

Total transferred file size: 20828099 bytes

Literal data: 20828099 bytes

Matched data: 0 bytes

File list size: 145

File list generation time: 0.001 seconds

File list transfer time: 0.000 seconds

Total bytes sent: 70

Total bytes received: 20835356


sent 70 bytes received 20835356 bytes 8334170.40 bytes/sec

total size is 20828099 speedup is 1.00


  • del_backup.sh

#!/bin/sh

rm -fr /home/readytoact/backup/local/DATA_gnukorea_`date +%Y%m%d --date '7 days ago'`.tar.gz

rm -fr /home/readytoact/backup/local/DB_gnukorea_`date +%Y%m%d --date '7 days ago'`.tar.gz

: 7전에 생성된 백업 데이터 삭제

4.1 crontab 설정

0 0 * * 0 /home/readytoact/bin/del_backup.sh

0 0 * * 0 find /home/readytoact/backup/backup.u1i -mtime +7 -print -exec rm -f {} \

30 0 * * * /home/readytoact/bin/backup_local.sh > /home/readytoact/backup/log/backup_local_`date +%Y%m%d`.log 2>&1

30 1 * * * /home/readytoact/bin/get_u1i.sh > /home/readytoact/backup/log/backup_log_u1i_`date +%Y%m%d`.log 2>&1


7일 간격으로 저장된 원격 백업 파일에서 7일전 파일 삭제

0 0 * * 0 find /home/readytoact/backup/backup.u1i -mtime +7 -print -exec rm -f {} \


5. 참고

  • crontab 작성시 각각의 서버에서 백업 파일이 생성되고 전송되는 시간을 고려해서 예약작업
    시간을 조절한다


  • DB복구

mysql -uusername -ppasswd dbname < backup.sql

mysql -uusername -ppasswd dbname --default-character-set=euckr < backup.sql


마지막으로 항상 서비스를 제공하도록 해주시고 항상 많은 도움을 주시는 우분투 포럼(#ubuntu @ irc.hanirc.org)의 강분도님과 MK님께 감사드립니다.