OS/Linux

Folder backup script

아르비스 2019. 7. 5. 10:13

폴더 백업이 많이 발생해서 이를 자동화

1. 스크립트 생성

$ vi /backup.sh

내용을 아래와 같이 작성

#!/bin/bash

TIME=`date +%b-%d-%y` # This Command will read the date.

SRCDIR=nexfinance.com_bak # Source backup folder.

PARDIR=./backup #Parents folder

FILENAME=$SRCDIR-$TIME.tar.gz # The filename including the date.

DESDIR=backup # Destination of backup file.

tar -cpzf $DESDIR/$FILENAME $PARDIR/$SRCDIR

 

Tar command explained

  • tar = Tape archive
  • c = Create
  • v = Verbose mode will print all files that are archived.
  • p = Preserving files and directory permissions.
  • z = This will tell tar that to compress the files.
  • f = It allows tar to get the file name.

 

2.  자동 백업 설정 (Automation)

리눅스 크론잡(cron job)에 일정 등록

* The cron jobs line has 6 parts see below explanation:

Minutes Hours Day of Month Month Day of Week Command
0 to 59 0 to 23 1 to 31 1 to 12 0 to 6 Shell Command

         

Open crontab editor utility:

$ crontab -e

 

Note: the edit rules are similar with vi editor.

Paste the following text in the editor:

# M H DOM M DOW CMND 
00 04 * * * /bin/bash /backup.sh

매일  04:00:00.에 실행되는 스크립트

For example, if you want to run the script only twice a week:

# M H DOM M DOW CMND 
00 04 * * 1,5 /bin/bash /backup.sh

 

끝..