카이도스의 Tech Blog

AWS MongoDB backup - S3 본문

AWS

AWS MongoDB backup - S3

카이도스 2023. 4. 4. 13:58
728x90
반응형

AWS MongoDB backup - S3

s3 생성하여 ec2-mongodb를 백업해보겠다.

모든환경은 우분투 20.04

참고 url - https://medium.com/@codematrix/back-up-and-restore-mongo-db-hosted-on-aws-ec2-with-a-shell-script-cron-and-aws-s3-238731946d7c


사전작업(s3 마운트 및 S3FullAccess 권한 필요)

## s3 마운트  
s3fs backup.XXX.XX /home/ubuntu/s3-drive -o url=https://s3-ap-northeast-2.amazonaws.com -o use_path_request_style -o iam_role=s3_job_role

# awscli 설치 - job 서버에서 진행
sudo apt install awscli
aws --version
aws-cli/1.18.69 Python/3.6.9 Linux/5.4.0-1066-aws botocore/1.17.44

aws s3 ls | grep backup.XXX.XXX
2023-02-22 04:17:23 backup.XXX.XXX

# hosts 추가
sudo vi /etc/hosts
IP      호스트네임

# s3_backup_db.sh 수정
sudo vi /home/ubuntu/s3_backup_db.sh
---------------------------------------
#!/bin/sh
find /home/ubuntu/s3-drive/back/* -mtime +3 -ls -exec rm -rf {} \;
mongodump --host 호스트네임 --authenticationDatabase admin -u admin -p'패스워드' --db 디비명 --out /home/ubuntu/s3-drive/back/`date +"%m-%d-%y"`
---------------------------------------

# mongodump 재설치 mongdump 버전이슈로 덤프 안되서 업그레이드 진행
sudo apt autoremove mongo*
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2004-x86_64-100.6.1.tgz
tar -zvxf mongodb-database-tools-ubuntu2004-x86_64-100.6.1.tgz
cd /home/ubuntu/mongodb-database-tools-ubuntu2004-x86_64-100.6.1/bin
sudo mv * /usr/bin/

# 커넥션 테스트
mongostat --host 호스트네임  -authenticationDatabase admin -u admin -p'패스워드'
2023-02-22T08:44:01.742+0000	WARNING: On some systems, a password provided directly using --password may be visible to system status programs such as `ps` that may be invoked by other users. Consider omitting the password to provide it via stdin, or using the --config option to specify a configuration file with the password.
insert query update delete getmore command dirty  used flushes vsize   res qrw arw net_in net_out conn   set repl                time
    10    77     12     *0      60    98|0  0.1% 78.5%       0 10.0G 6.37G 0|0 1|0   128k   1.09m  617 repl0  SLV Feb 22 08:44:02.770
     5   812      9     *0      29   203|0  0.1% 78.6%       0 10.0G 6.37G 0|0 2|0   406k   1.74m  645 repl0  SLV Feb 22 08:44:03.770
.....

# crontab 주석 해제
sudo vi /etc/crontab
30 20 * * *   ubuntu sh /home/ubuntu/s3_backup_db.sh >> /home/ubuntu/log/s3_backup_`date +\%Y-\%m-\%d`.log 2>&1

#백업테스트 및 확인
sh /home/ubuntu/s3_backup_db.sh >> /home/ubuntu/log/s3_backup_`date +\%Y-\%m-\%d`.log 2>&1
tree -d /home/ubuntu/s3-drive/back/
/home/ubuntu/s3-drive/back/
├── 02-20-23
│   └── db이름
└── 02-22-23
    └── db이름
728x90
반응형

'AWS' 카테고리의 다른 글

AWS 비용 최적화(예약 인스턴스, savings plans)  (0) 2023.05.10
ALB- S3 로그 연동  (0) 2023.04.04
EC2-cloudwatch 로그 연동  (0) 2023.03.29
2주차 - 컴퓨팅 서비스  (0) 2023.03.07
1주차 - AWS & 컴퓨트 서비스  (0) 2023.03.03
Comments