'mirroring'에 해당되는 글 1건

  1. 2010.05.24 FreeNAS RAID, Unison setup

FreeNAS RAID, Unison setup

잡기 2010. 5. 24. 20:43
인터넷 뒤져봐도 FreeNAS의 RAID 구성 방법이나 사고 대처 방안, unison 셋업 방법이 잘 나오지 않는다. 할 수 없이 삽질했다. 다음에 다시 셋업할 경우를 대비해 기록을 남긴다.

목적: FreeNAS 로 RAID 1(mirroring)을 구현한다. 이때 HDD 2개로 mirroring을 하면 한쪽 HDD가 망가지더라도 HDD 내용을 날리지는 않는데, 모종의 천재지변(번개를 맞는다던가...)으로 2개의 HDD가 동시에 fail이 나면 대책이 없다. 이런 경우를 방지하기 위해 원격 사이트에 똑같은 형태의 backup server를 구축해 놓고 인터넷으로 두 서버를 mirroring한다. 시스템 구성 예:

사용자 삽입 이미지
FreeNAS는 rsync와 unison을 동시에 지원하는데, rsync는 단방향 sync지만 unison은 양방향 sync가 가능하다. 2개의 시스템 사이의 단방향 백업은 rsync server 및 rsync client를 구축하는 것으로 간단하게 해결된다. rsync는 단순 백업을 할 때 사용하면 되고, 원격 서버와 작업 서버 사이에서 동시에 변경이 일어날 경우에는 unison 외에 답이 없다.

RAID1과 unison을 사용해서 얻을 수 있는 이익: 양쪽 서버에서 동일 컨텐츠에 대해 3중 백업 구현.

FreeNAS의 장점


  • 싸다.
  • 내 맘대로 삽질해서 꾸밀 수 있다.

FreeNAS 시스템 구성


FreeNAS는 0.7.1 버전 사용(이번 여름에 0.8이 나올 것 같다). 놀고 있는 Main board, CPU 등 가용 자원을 그러모아 대충 구성. HDD 만큼은 새로 구입. 값싸고 전기 덜먹고 소음이 적은 Western Digital의 Cavier Green 1T HDD를 지원하므로 그걸 사용하면 좋을 듯. 부팅 디바이스는 USB Memory를 사용.  USB Memory는 128MB 정도면 아무거나 충분히 가능. FreeNAS 셋업 방법은 인터넷에 널려 있으므로 그쪽 참조.

System->General

Time zone: Asia/Seoul
Enable NTP: check use the specified NTP Server
NTP time server: time.windows.com
Time update interval: 60

NTP 서버를 사용해야 시스템 시간을 정확히 맞출 수 있음. windows 역시 NTP를 사용하도록 구성.

RAID1 구성


Disks->Management->Add(+)

Hard disk standby time: 5minutes
Advanced PowerManagement: Level 1 - Minimu power usage with Standby (spindown)
S.M.A.R.T: check. Activate SMART monitoring for this device
Preformatted file system: Software RAID

* 2개의 HDD 를 동일하게 셋업. 전기세 절약을 위한 옵션도 함께 추가. HDD는 대개 15w 미만의 전력을 사용하는데, 전기세를 정말 절약하고 싶으면 CPU가 전기를 덜 먹는 걸 사용하고, System->Advanced의 Power Daemon을 enable하는게 낫다. 편의상 이때 추가된 HDD device를 첫번째 HDD를 ad6, 두번째 HDD를 ad7으로 칭함.

Disks->Software RAID->RAID1

raid name: raid
Type: RAID 1 (mirroring)
Balance algorithm: Round-robin read
Provider: ad6, ad7 을 ctrl-click으로 둘 다 선택

* Balance algorithm은 현재로썬 rount-robin read가 성능이 가장 좋다고 함.  

 Disks->Format

Disk: raid
File System: UFS (GPT & soft updates)

* ZFS가 여러 모로 좋지만, ZFS를 사용하면 시스템 자원을 많이 먹고 전송 퍼포먼스가 떨어진다는 얘기가 있음. UFS도 충분히 좋으므로 그걸 사용.

Disks->Mount Point->Management

Type: Disk
Disk: raid
Partition Type: GPT partition
Partition number: 1
File system: UFS
Mount point name: raid (이렇게 하면 마운트되는 지점은 /mnt/raid 가 된다)
Description: 아무거나 써줌

사용자 추가, 디렉토리 설정

편의상 /mnt/raid/share를 동기화할 디렉토리로, /mnt/raid/user/natas를 작업자 디렉토리로 간주. 사용자 계정을 추가하는 것은 SSH shell로 들어가 작업해야 하기 때문. 아무튼 사용자 계정이 있어야 여러 모로 편리.

Access->Users and Groups->Add(+)

Name: natas
...
Shell: bash
Primary group: admin
Home directory: /mnt/raid/user/natas/
User portal: check Grant access to the user portal.

서비스 설정


Services->CIFS/SMB (Enable)

Authentication: Local User (XP에서는 문제 생길 수 있으므로 anonymous로 할 수도...)
NetBIOS Name: NatasNAS (이름은 아무거나. windows의 컴퓨터 이름으로 나타남)
Workgroup: WORKGROUP (XP의 default workgroup이 WORKGROUP)
Time server: Yes
...
Large read/write: check enable
Use sendfile: check
* use sendfile 및 large read/write는 전송 속도 향상을 위한 파라미터. 전송속도를 향상시키기 위해 System->Advanced->Tuning을 enable 시킴. MTU 변경 역시 전송에 유리하나, LAN Card에 따라 지원하지 않는 것도 있음.

Services->Secure Shell (Enable)


* Unison, Rsync 등을 사용하려면 SSH가 구성되어 있어야 한다.

Password authentication: check Enable...
TCP forwarding: check permit
Compression: check Enable

Services->Unison (Enable)

Working directory: /mnt/raid/.unison/
check Create work directory if it doesn't exist

Services->Dynamic DNS


* 인터넷을 통해 원격지의 backup server를 구성하려면 양쪽 서버 모두 Dynamic DNS 구성을 하는 것이 바람직.

Unison Setup


SSH로 main server(192.168.123.199) 및 backup server (192.168.123.100) 에 로긴 가능한지 확인. unison은 main server에서 돌리는 것으로 가정, 동기화할 디렉토리는 /mnt/raid/share라고 가정하고 진행.

디렉토리 생성

$ mkdir -p /mnt/raid/user/natas
$ mkdir -p /mnt/raid/share

백업 스크립트 생성 (/mnt/raid/user/natas/backup)

$ cat >/mnt/raid/user/natas/backup
#!/bin/sh
export HOME=/mnt/raid/
/usr/local/bin/unison -batch -silent /mnt/raid/share  ssh://192.168.123.100//mnt/raid/share 2>&1
^D
$ chmod +x /mnt/raid/user/natas/backup
* unison은 FreeNAS의 cron이나 web interface에서 바로 실행되지 않는데, 환경변수 HOME이 반드시 지정되어야 한다. 또한 unison의 출력이 표준 에러(stderr)로 나오므로  이것을 표준 출력(stdout)으로 리다이렉트 해줘야(2>&1) 작동 검증을 쉽게 할 수 있다.

SSH 키 생성


* ssh 키를 생성하는 이유: unison이 backup server에 로긴할 때 암호를 묻지 않고 바로 접속이 가능해야 cron 등에서 unison을 주기적으로 실행할 수 있다.

* ssh-keygen으로 키 페어를 생성한다. passphrease 는 그냥 enter 입력.
$ su -
...
# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root//.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
* 생성된 key를 backup server에 복사한다. 이때 backup server로 ssh 로긴이 가능해야 한다. 복사가 끝난 후 암호를 묻지 않고 backup server에 ssh 로긴이 가능한지 확인.
# scp ~/.ssh/id_rsa.pub 192.168.123.100:~/.ssh/authorized_keys
# ssh 192.168.123.100
Last login: Thu May 20 18:41:00 2010 from 192.168.123.199
Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994
        The Regents of the University of California.  All rights reserved.

Welcome to FreeNAS!
# exit
#

Unison Test


backup script를 사용하여 unison을 테스트해 본다. 주의: main server 및 backup server에  /mnt/raid/share 디렉토리가 존재해야 한다. unison을 처음 실행하면 처음 실행되서 그렇다는 경고 메시지가 장황하게 나온다.
# cd /mnt/raid/natas
# ./backup
Connected [//main-nas.local//mnt/raid/share -> //backup-nas.local//mnt/raid/share]
Warning: No archive files were found for these roots, whose canonical names are:
/mnt/raid/share
//backup-nas.local//mnt/raid/share
This can happen either
because this is the first time you have synchronized these roots,
or because you have upgraded Unison to a new version with a different
archive format.  

Update detection may take a while on this run if the replicas are
large.

Unison will assume that the 'last synchronized state' of both replicas
was completely empty.  This means that any files that are different
will be reported as conflicts, and any files that exist only on one
replica will be judged as new and propagated to the other replica.
If the two replicas are identical, then no changes will be reported.

If you see this message repeatedly, it may be because one of your machines
is getting its address from DHCP, which is causing its host name to change
between synchronizations.  See the documentation for the UNISONLOCALHOSTNAME
environment variable for advice on how to correct this.

Donations to the Unison project are gratefully accepted:
http://www.cis.upenn.edu/~bcpierce/unison
main server의 /mnt/raid/share에 몇 개 파일을 생성하고, 마찬가지로 backup serevr의 /mnt/raid/share에 다른 파일을 몇 개 생성해 놓은 다음 backup 스크립트를 다시 실행해 본다.
# cd /mnt/raid/user/natas
# ./backup
Connected [//main-nas.local//mnt/raid/share -> //backup-nas.local//mnt/raid/share]
위와 같은 메시지가 나오고 main server 및 backup server의 내용이 동일하면 확인 끝.

unison은 SSH를 사용해 원격 컴퓨터에 접속해 파일 해시를 가져와 두 컴퓨터 사이의 파일 해시를 비교한다. 서로 다르면 파일을 복사해서 맞춘다.

생성된 키는 /root/.ssh에 보존되는데, main server가 리부팅하면 사라진다. 이것을 방지하기 위해 생성된 키를 HDD의 /mnt/raid/natas/.ssh_root에 보관하고 main server가 부팅될 때 /root/.ssh로 다시 복사해 놓는다. 일단 root 계정에 생성된 .ssh 디렉토리를 통째로 natas 계정으로 복사.
# cp -r /root/.ssh /mnt/raid/user/natas/.ssh_root

그 다음 NAS의 web에서 부팅 후 실행할 명령으로 등록.

System->Command scripts->+
Command: cp -r /mnt/raid/user/gstech/.ssh_root /root/.ssh
Type: PostInit

cron job으로 등록


두 서버의 동기화를 자동으로 하려면 cron table에 backup 스크립트를 등록해 사용자가 지정한 시간마다 실행하도록 한다. FreeNAS의 cron은 각 사용자별 cron table을 제공하지 않는다. 오직 root만 가능한 것 같다.

커맨드 쉘에서 테스트하는 것과 web interface에서 cron 잡으로 실행하는 것은 차이가 좀 있다. 디렉토리 사용하는 거나 unison의 해시 파일 생성 디렉토리 등등이 달라 한번에 실행되지 않는다. 일단, 웹에서 Advanced->Command로 가서 /mnt/raid/user/gstech/backup 를 실행해 본다. 아래와 비슷한  메시지가 나타난다.
The archive file is missing on some hosts.
For safety, the remaining copies should be deleted.
  Archive arc5fcde3990570240836f07c4d9dd3a43 on host gstech-nas.local is MISSING
  Archive arc2bd324e34ece9d322c9e5b4e3e219f3 on host gstechlab-nas.local should be DELETED
Please delete archive files as appropriate and try again.
지시대로 main server 및 backup server의 해당 파일들을 지워준다.
# ssh 192.168.123.100
...
# rm -fr /mnt/raid/.unison/*
# exit
# rm -fr /mnt/raid/.unison/*
Advanced->Command로 가서 /mnt/raid/user/gstech/backup 를 실행하면 처음 unison을 실행할 때와 마찬가지의 메시지가 나타난다.  실행이 성공적으로 되면 cron 에 등록해 주러 간다. 아래 예는 매일 4:00am에 backup을 실행하는 것이다.

System->Advanced->Cron->Add(+)
Command: /mnt/raid/user/natas/backup
Who: root
Description: Unison Backup
Schedule time:
Minutes: 0
Hours: 4
Days: all
Months: all
Week days: all

여기까지 입력하고, Run now 버튼을 눌러 화면 상단에 'The cron job has been executed successfully' 이 나타나면 다 끝난 것이다.  save하고 reboot 한 다음 다시 실행해 보아서 잘 되면 다 끝난 것.

RAID 사고 대처


RAID1 미러링 중 HDD 하나가 fail나서 교체하는 경우에 어떻게 대처하나? 예: ad7이 fail 났을 경우.
mirroring중 하나의 HDD에 에러가 나더라도 다른 정상적인 HDD가 있으면 별 일 없이 잘 작동한다. 고장난 HDD를 교체할 수도 있고, 교체하지 않고 그대로 쓸 수도 있다. 일단 고장난 HDD를 제거해야 한다. 고장난 HDD가 있을 경우 RAID 상태 메뉴에는 DEGRADED라고 표시된다. Disks->Software RAID->RAID1->Tools 메뉴에서 information을 보면 어느 HDD가 맛이 갔는지 확인할 수 있다.

Disks->Software RAID->RAID1->Tools로 들어가서 Volume Name에 사용중인 raid를 선택하고 고장난 HDD를 지정한 다음,

Volume Name: raid
Disk: ad7

Command에서 'forget'을 선택하고 send command 버튼을 클릭한다. 다음에 Command에서 'remove'를 선택하고 send command 버튼을 클릭하면 고장난 HDD가 raid에서 제거된다. 그 다음 Disks->Management에서 고장난 HDD를 제거한다.

새로운 HDD를 추가하려면 NAS를 끄고 고장난 HDD를 빼고 새로운 HDD를 배선한 다음, 부팅하여

Disks->Management->Add(+)로 새 HDD를 추가하고, Preformatted file System: Software RAID로 지정한다. 다음, raid를 재구성하러 간다.
Disks->Software RAID->RAID1->Tools
Volume Name: raid
Disk: ad7
Command: 'insert' & send command
Command: 'rebuild' & send command

rebuild 과정은 매우 느리게 진행된다. Disks-Software RAID->RAID1->Information 에서 rebuild 과정이 진행되는 상황을 볼 수 있다. rebuild가 진행되는 와중에도 HDD 사용은 가능하다.

,