1. script
#!/bin/sh dir=/home/user/backup/homepage/db/test time=`date +%Y%m%d` mysqldump -uroot -p12345 test > "test_"$time".sql" tar czf $dir"/test_"$time".tar.gz" "test_"$time".sql" rm -rf "test_"$time".sql" #find $dir -atime +2 -print -exec rm -rf {} ;
dir=/home/user/backup/homepage/db/test
; backup 데이터가 저장될 디렉토리
time=`date +%Y%m%d`
; 파일명에 날짜 정보를 넣기 위한 변수
mysqldump -uroot -p12345 test > “test_”$time”.sql”
; test 데이터베이스를 test_######.sql 이름으로 저장. ######는 날짜 정보
tar czf $dir”/test_”$time”.tar.gz” “test_”$time”.sql”
; sql 파일을 tar.gz 으로 압축
rm -rf “test_”$time”.sql”
; sql 파일 삭제
find $dir -atime +2 -print -exec rm -rf {} \;
; 백업한지 3일 지난 데이터를 삭제, 위 소스에서는 주석처리 됨
2. crontab
리눅스는 기본적으로 crontab 이라는 툴이 설치되어 있음
/etc/crontab 을 수정하여 주기적으로 동작시킬 명령을 생성할 수 있음
크론탭은 총 6개의 필드로 구성되어 있다. 앞의 5개의 필드가 시간 지정을 위한 필드이고, 마지막 필드에 스케쥴링할 명령어를 지정한다. 아래와 같이 사용한다.
# min hours day month day command 34 2 * * * sh /root/backup.sh
위 명령은 매일 2시 34분에 /root/backup.sh를 실행한다는 의미이다.
시간은 아래와 같이 5개의 단위로 분류된다.
분 | 0-59 | |
시 | 0-23 | |
일 | 1-31 | |
월 | 1-12 | |
주/일 | 0-6 | 일요일이 0이다. |
별표 (*)는 all을 의미한다.
매주 일요일 새벽 4시에 어떤 명령을 실행하려면 다음과 같이 쓴다.
# min hours day month day command 00 4 * * 0 sh /root/backup.sh
실행
$ /etc/init.d/cron restart