DEV Community

웹학교
웹학교

Posted on • Edited on

2 2

Linux에서 홈페이지 자료 백업 및 데이터베이스 백업 자동처리

대부분의 웹사이트가 리눅스 서버 기반에서 운영되고 있다.
아래는 전용 백업툴을 사용하지 않고 쉽게 사이트의 데이타 및 DB를 자동 백업하는 과정이다.

  1. nano 또는 vi편집기를 이용하여 backup.sh파일을 아래의 내용으로 만든다.

예:

  • 도메인이 abc.com
  • dbuser, dbpass, dbname
  • ftpuser, ftppass, ftphost(백업서버)
  • 3일이전 백업본은 삭제
#!/bin/sh
NOW=$(date +"%y-%m-%d") #오늘일자를 생성
DELDAY=$(date -d "-3 days" +%y-%m-%d)   #3일 전의 일자를 생성
####### 홈페이지 자료 백업 처리 및 데이터베이스 백업처리 #####
tar zcvfp /home/backup/abc-$NOW.tar.gz /home/abc/ #홈페이지폴더
mysqldump -udbuser -pdbpass dbname > /home/backup/abc-$NOW.sql
tar zcvfp /home/backup/abc-$NOW-sql.tar.gz /home/backup/abc-$NOW.sq*
rm -f /home/backup/abc-$NOW.sq*
B_DIR=/home/backup
REMOTE=ftphost
ID=ftpuser
PWD=ftppass
ftp -n -v $REMOTE << EOF
user $ID $PWD
lcd ${B_DIR}
put abc-${NOW}.tar.gz
put abc-${NOW}-sql.tar.gz
delete abc-$DELDAY.tar.gz
delete abc-$DELDAY-sql.tar.gz
#### 백업서버에 있는 파일중 3일 전의 파일은 삭제 처리 한다.
bye
EOF
### ftp접속을 종료 한다.
rm -f /home/backup/abc-$DELAY.tar.gz
rm -f /home/backup/abc-$DELAY-sql.tar.gz
Enter fullscreen mode Exit fullscreen mode
  1. cron에 위 파일을 등록한다.(끝)

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more