728x90

DB 서버

1. DB 백업폴더 구조 만들기 

 

2. sql 저장 위치

 

3. DB서버의 crontab

crontab -e

# DB backup AM 01:00
0 1 * * * /home/backup.sh

 

4. DB 서버의 backup.sh

#!/bin/sh
DATE=`/bin/date +%Y%m%d`
PREV_DATE=`/bin/date --date '+20 days ago' +%Y%m%d`

#################  db backup make cron #####################
/usr/bin/mysqldump -uroot -pMaria_db_pw AA_Database > /home/backup/AA_backup/mysql_db_bak_${DATE}.sql
chown root.root /home/backup/AA_backup/mysql_db_bak_${DATE}.sql
chmod 755 /home/backup/AA_backup/mysql_db_bak_${DATE}.sql


/usr/bin/mysqldump -uroot -pMaria_db_pw  BB_Database > /home/backup/BB_backup/mysql_db_bak_${DATE}.sql
chown root.root /home/backup/BB_backup/mysql_db_bak_${DATE}.sql
chmod 755 /home/backup/BB_backup/mysql_db_bak_${DATE}.sql

#################  db backup delete cron   #####################

rm -Rf /home/backup/AA_backup/mysql_db_bak_${PREV_DATE}.sql
rm -Rf /home/backup/BB_backup/mysql_db_bak_${PREV_DATE}.sql

 

3. DB 서버의 cat /etc/rsyncd.conf (centos os7)

[root@test-db /]# 

# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area

[ test DB ] 
path=/home/backup/
comment=test_db
uid=root
git=root
use chroot=yes
read only=yes
hosts allow= work_server_ip
max connections=3
timeout=600 

 

4. 백업한 sql 파일이 저장될 서버의 crontab

crontab -e

# DB backup RSYnC AM 02:00
15 10 * * * /usr/bin/rsync -avrz --delete DB_server_ip::test_db/home/backup/

 

rsync 부가 설명

rsync를 사용하여 원격으로 데이터 백업 하기
rsync는 873번 포트를 사용하며, 두 서버간의 데이터 백업을 위해서 주로 사용된다.
master서버에서 rsync데몬을 띄우고, backup 서버에서 master서버에 접근하여 허용된 디렉토리의 데이터를 백업하는 방식과,
ssh를 이용하여 데이터를 미러링 하는 방법 두가지가 있다.

테스트 환경 
master서버 : 10.30.100.60
backup서버 : 10.30.100.61
1. rsync데몬(873포트)을 이용하는 방법 

1.1. /etc/rsyncd.conf 파일 생성
( 수정이 완료되면 재시작 : systemctl start rsyncd.service )
( 시스템 재부팅 시에도 적용하려면 아래와 같은 명령을 실행 systemctl enable rsyncd.service )


/etc/rsyncd.conf 파일은 기본적으로 생성되어 있지 않기 때문에 아래와 같은 형식으로 생성 한다.
==============================================================================
[root@master ~]# cat /etc/rsyncd.conf
[add4s]
comment =add4s home directory
path = /home/add4s/public_html
uid = root
gid = root
use chroot = yes
read only = yes
hosts allow = 10.30.100.61
max connection = 2
timeout = 300
[root@master ~]#
==============================================================================

[add4s] --> 서비스명
comment = add4s home directory --> 서비스에 대한 코멘트
path = /home/add4s/public_html  --> 서비스 디렉토리(백업 디렉토리)
uid = root --> 접속 user 권한
gid = root --> 접속 group 권한
use chroot = yes  --> 서비스 디렉토리를 최상위 디렉토리로 사용
read only = yes --> 읽기 전용으로 설정
hosts allow = 10.30.100.61  --> 접속을 허용할 호스트(백업서버의 ip) 여러 호스트를 지정하는경우 , 로 구분
max connection = 2  --> 최대 접속 가능 수
timeout = 300  --> 클라이언트에서 접근시 타임아웃시간
1.2. master서버(DB 서버) 에서 rsync데몬 실행 

/etc/xinetd.d/rsync 파일에서 disable = no 로 변경하고 xinetd 데몬을 실행한다.
데몬이 실행되면, 873포트가 open된 것을 확인 할 수 있다.

ubunt계열의 Linux에서는
/etc/default/rsync 파일에서 RSYNC_ENABLE=true 로 변경하고
rsync 데몬을 실행한다.
==============================================================================
[root@master ~]# /etc/init.d/xinetd start
Starting xinetd: [  OK  ]
[root@master ~]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1242/sshd           
tcp        0      0 :::873                      :::*                        LISTEN      7022/xinetd         
tcp        0      0 :::22                       :::*                        LISTEN      1242/sshd        
[root@master ~]#
==============================================================================
1.3. iptables 873 포트 접속 허용 

iptables 방화벽에 873 포트 접속이 가능하도록 포트를 허용한다. 
================================================================================
[root@master ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:http 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:https 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:rsync 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@master ~]#
==============================================================================
1.4. backup할 서버(Work 서버)에서 master서버(DB 서버)의 rsync 서비스와 comment 확인하기

backup서버에서 아래와 같은 방법으로 master서버에서 허용된 서비스와 comment를 확인할 수 있다.
==============================================================================
[root@backup ~]# rsync 10.30.100.60::
add4s            add4s home directory
[root@backup ~]#
==============================================================================

1.5. 백업 테스트 
- master서버에서 파일 생성
==============================================================================
[root@master ~]# cd /home/add4s/public_html
[root@master public_html]# touch testfile1 testfile2 testfile3
[root@master public_html]# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 24 01:14 testfile1
-rw-r--r-- 1 root root 0 Apr 24 01:14 testfile2
-rw-r--r-- 1 root root 0 Apr 24 01:14 testfile3
[root@master public_html]#
==============================================================================

- backup서버에서 rsync명령으로 업데이트(master서버에서 생성된 파일이 백업된 것을 확인할 수 있다.)
  아래는 master서버에서 허용된 서비스의 디렉토리와 backup서버의 /backup/add4s 디렉토리를 동기화 시키는 작업이다.
==============================================================================
[root@backup ~]# rsync -avzr --delete 10.30.100.60::add4s /backup/add4s/
receiving incremental file list
./
testfile1
testfile2
testfile3

sent 86 bytes  received 200 bytes  52.00 bytes/sec
total size is 0  speedup is 0.00
[root@backup ~]# cd /backup/add4s
[root@backup add4s]# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 24  2009 testfile1
-rw-r--r-- 1 root root 0 Apr 24  2009 testfile2
-rw-r--r-- 1 root root 0 Apr 24  2009 testfile3
[root@backup add4s]#
==============================================================================

- master서버에서 file 삭제
==============================================================================
[root@master public_html]# rm -rf testfile2 testfile3
[root@master public_html]# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 24 01:14 testfile1
[root@master public_html]#
==============================================================================

- backup서버에서 rsync명령으로 업데이트(master서버에서 삭제한 파일이 같이 삭제 된것을 확인할 수 있다.)
==============================================================================
[root@backup add4s]# rsync -avzr --delete 10.30.100.60::add4s /backup/add4s/
receiving incremental file list
deleting testfile3
deleting testfile2
./

sent 29 bytes  received 82 bytes  20.18 bytes/sec
total size is 0  speedup is 0.00
[root@backup add4s]# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 24  2009 testfile1
[root@backup add4s]#
==============================================================================


1.6. cron에 등록하여 주기적으로 백업

아래와 같은 방법으로 backup서버의 cron에 등록을 하면 원하는 시간에 주기적으로 데이터를 미러링 하게 된다.
==============================================================================
[root@backup add4s]# crontab -l
00 02 * * * /usr/bin/rsync -avzr --delete 10.30.100.60::add4s /backup/add4s/
==============================================================================


2. ssh를 이용하여 rsync사용하기

ssh를 이용하는 경우는 아래와 같이 -e ssh 옵션(생략가능)을 추가해서 rsync 명령을 사용하면 된다. 그리고, master서버와 backup서버 둘다 명령을 수행 할 수 있다.
마찬가지로 아래의 명령을 스크립트로 만들어서 사용하거나, cron에 등록해서 주기적인 데이터 미러링 작업이 가능하다.


2.1. master서버에서 rsync 명령으로 백업
==============================================================================
[root@master ~]# rsync -avzr --delete -e ssh /home/add4s/public_html/ root@10.30.100.61:/backup/add4s/
The authenticity of host '10.30.100.61 (10.30.100.61)' can't be established.
RSA key fingerprint is 4a:42:46:c9:55:48:63:08:a5:84:0a:69:d8:a6:47:0e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.30.100.61' (RSA) to the list of known hosts.
root@10.30.100.61's password:
sending incremental file list

sent 67 bytes  received 12 bytes  10.53 bytes/sec
total size is 0  speedup is 0.00
[root@master ~]#
==============================================================================


2.2. slave서버에서 rsync 명령으로 백업
==============================================================================
[root@backup ~]# rsync -avzr --delete -e ssh root@10.30.100.60:/home/add4s/public_html/ /backup/add4s/
root@10.30.100.60's password:
receiving incremental file list

sent 11 bytes  received 68 bytes  8.32 bytes/sec
total size is 0  speedup is 0.00
[root@backup ~]#
==============================================================================


 


 

728x90

+ Recent posts