CentOS 리눅스에서의 tar 백업정책 - 증분백업 > 리눅스 Tip

본문 바로가기
 

CentOS 리눅스에서의 tar 백업정책 - 증분백업

페이지 정보

작성자 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 11,928회 작성일 08-05-13 19:17

본문

CentOS 리눅스에서의 tar 백업정책 - 증분백업

리눅스를 사용한다면 대부분 tar 명령어에 대하여 한번쯤 들어보았을 것이다.

그럼 지금부터 tar를 사용한 백업과 복구 방법에 대해 알아보도록 하겠다.
 


1. tar 압축하기

tar도 어김없이 여타 리눅스 명령어와 같이 수많은 옵션들을 가지고 있다.

이옵션들중 자주 사용되는 옵션에 대해서만 언급하도록 한다.

파일이나 디렉토리를 압축하고자 할때에는 -c 옵션(compress)을 사용한다.

예를 들어 설명하도록 한다.

먼저 /home/multi/test1 디렉토리의 내용을 모두 백업하려면 어떻게 할까?
 

tar -czf [백업파일명] -C [기준으로 삼을 디렉토리명] [백업할 디렉토리명]

-c 옵션은 압축
-z 는 gzip 압축
-f 는 파일이름

 

[multi@localhost ~]$ uname -a
Linux localhost.localdomain 2.6.18-53.1.13.el5 #1 SMP Tue Feb 12 13:01:45 EST 2008 i686 i686 i386 GNU/Linux
[multi@localhost ~]$ pwd //현재 디렉토리 확인
/home/multi
[multi@localhost ~]$ mkdir test1 //백업할 test1 디렉토리 생성
[multi@localhost ~]$ cd test1
[multi@localhost test1]$ pwd
/home/multi/test1
[multi@localhost test1]$ echo "test1" > test1.txt //test1.txt 파일 만들기
[multi@localhost test1]$ echo "test2" > test2.txt //test2.txt 파일 만들기
[multi@localhost test1]$ echo "test3" > test3.txt //test3.txt 파일 만들기
[multi@localhost test1]$ ls -l // 파일이 생성되었는지 확인
합계 12
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test1.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test2.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test3.txt
[multi@localhost test1]$


/home/multi/test1 디렉토리 아래에 test1.txt, test2.txt, test3.txt이라는 3개의 파일을 만들어 두었다.
이 3개의 파일을 묶어서 압축해 보도록 하자. 묶음과 동시에 압축을 하기 위해서는

tar -czf [압축파일명] -C [압축할 파일들의 디렉토리경로] 형식으로 명령을 입력한다.
 

[multi@localhost test1]$ tar -czf test1.tgz -C /home/multi ./test1
[multi@localhost test1]$ ls -l
합계 16
-rw-rw-r-- 1 multi multi 206  2월 21 02:03 test1.tgz
-rw-rw-r-- 1 multi multi   6  2월 21 02:00 test1.txt
-rw-rw-r-- 1 multi multi   6  2월 21 02:00 test2.txt
-rw-rw-r-- 1 multi multi   6  2월 21 02:00 test3.txt
[multi@localhost test1]$


위와 같이 test1.tgz로 압축이 된 것을 확인할 수 있다. 그리고 -C 옵션은 기본 디렉토리의 경로를 지정할때 사용한다.

위와 같이 입력하면 /home/multi 디렉토리를 기준으로 그 아래의 test1 디렉토리를 지정한다는 의미다. 즉, /home/multi/test1 디렉토리하의 모든 파일을 현재 디렉토리에 test1.tgz 파일로 압축하겠다는 뜻이다.

압축을 한 다음에 압축한 파일이 정상적으로 압축이 되어 있는지 알아보도록 하자.
압축파일안의 파일리스트를 보기 위해서는 -t 옵션을 사용하면 된다.
 

[multi@localhost test1]$ tar -tf test1.tgz
./test1/
./test1/test1.tgz
./test1/test1.txt
./test1/test2.txt
./test1/test3.txt
[multi@localhost test1]$


위와 같이 표시된다면 이것은 압축시 기준 디렉토리의 경로를 /home/multi로 지정했기때문에 파일들의 경로는 test1 디렉토리가 최상위 디렉토리가 되어 압축이 된 것이다.

이 압축파일을 현재의 디렉토리에서 압축해제 하였을때는 아래와 같이 압축이 해제된다. 압축을 해제할때에는 -x옵션을 사용하여 아래와 같은 형식을 사용한다.
 

tar -xzf [압축된 파일명]

 

[multi@localhost test1]$ tar -xzf test1.tgz
[multi@localhost test1]$ ls -l
합계 20
drwxrwxr-x 2 multi multi 4096  2월 21 02:03 test1
-rw-rw-r-- 1 multi multi  206  2월 21 02:03 test1.tgz
-rw-rw-r-- 1 multi multi    6  2월 21 02:00 test1.txt
-rw-rw-r-- 1 multi multi    6  2월 21 02:00 test2.txt
-rw-rw-r-- 1 multi multi    6  2월 21 02:00 test3.txt
[multi@localhost test1]$


위에서 보는것과 같이 압축을 해제하면 현재디렉토리 아래에 test1이라는 디렉토리가 만들어지고 그 아래에 각 파일들이 압축해제된 것을 볼 수 있다.
 

[multi@localhost test1]$ cd test1
[multi@localhost test1]$ pwd
/home/multi/test1/test1
[multi@localhost test1]$ ls -l
합계 12
-rw-rw-r-- 1 multi multi 0  2월 21 02:03 test1.tgz
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test1.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test2.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test3.txt
[multi@localhost test1]$


그런데 압축해제된 파일 리스트를 보면 test1.tgz 파일이 보인다.
이것은 압축시 압축할 디렉토리안에 압축한 파일을 저장했기 때문이다.
이와 같이 압축할 파일이 있는 디렉토리안에 압축한 파일을 저장하지 말기 바라며, 압축한 파일의 저장위치는 왠만하면 다른 디렉토리아래로 하기 바란다.

다른 디렉토리아래에 압축한 파일을 저장 하기 위해서는 압축시 명령을 아래와 같은 형식으로 하면 되겠다. 즉 압축한 파일의 위치는 다른디렉토리로 전체경로를 적어주도록 한다.
 

tar -czf /home/multi/test1.tgz -C /home/multi ./test1

 

[multi@localhost test1]$ pwd
/home/multi/test1/test1
[multi@localhost test1]$ cd ..
[multi@localhost test1]$ pwd
/home/multi/test1
[multi@localhost test1]$ ls -l
합계 20
drwxrwxr-x 2 multi multi 4096  2월 21 02:03 test1
-rw-rw-r-- 1 multi multi  206  2월 21 02:03 test1.tgz
-rw-rw-r-- 1 multi multi    6  2월 21 02:00 test1.txt
-rw-rw-r-- 1 multi multi    6  2월 21 02:00 test2.txt
-rw-rw-r-- 1 multi multi    6  2월 21 02:00 test3.txt
[multi@localhost test1]$ rm -rf test1 test1.tgz
[multi@localhost test1]$ ls -l
합계 12
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test1.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test2.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test3.txt
[multi@localhost test1]$ tar -czf /home/multi/test1.tgz -C /home/multi ./test1
[multi@localhost test1]$ cd ..
[multi@localhost ~]$ ls -l test1.tgz
-rw-rw-r-- 1 multi multi 196  2월 21 02:09 test1.tgz
[multi@localhost ~]$ tar -tf test1.tgz
./test1/
./test1/test1.txt
./test1/test2.txt
./test1/test3.txt
[multi@localhost ~]$


위와 같이 하지 않고 기준 디렉토리를 최상위 / 루트디렉토리로 지정하여 압축하면 압축해제시에는 압축되어 있는 디렉토리 경로에 맞게 압축이 풀리게 된다.

/ 루트 디렉토리아래의 전체경로를 사용하여 압축하려면 다음과 같이 입력한다.
 

tar -czf /home/multi/test1.tgz -C / ./home/multi/test1

 

[multi@localhost ~]$ tar -czf /home/multi/test1.tgz -C / ./home/multi/test1
[multi@localhost ~]$ tar -tf test1.tgz
./home/multi/test1/
./home/multi/test1/test1.txt
./home/multi/test1/test2.txt
./home/multi/test1/test3.txt
[multi@localhost ~]$


2. tar 압축해제

이번에는 tar로 압축한 파일들을 압축해제해 보도록 하겠다.
압축 해제는 아래와 같은 옵션으로 쉽게 압축해제가 가능하다.
 

tar -xzf [압축된 파일명]


앞서 압축한 파일에 대해 압축을 해제해 보도록 하자.
 

[multi@localhost ~]$ tar -czf /home/multi/test1.tgz -C /home/multi ./test1
[multi@localhost ~]$ tar -tf test1.tgz
./test1/
./test1/test1.txt
./test1/test2.txt
./test1/test3.txt
[multi@localhost ~]$ rm -rf ./test1
[multi@localhost ~]$ ls -l ./test1
ls: ./test1: 그런 파일이나 디렉토리가 없음
[multi@localhost ~]$ tar -xzf test1.tgz
[multi@localhost ~]$ ls -l ./test1
합계 12
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test1.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test2.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test3.txt
[multi@localhost ~]$


tar help 내용
 

[multi@localhost ~]$ tar --help
Usage: tar [OPTION...] [FILE]...
GNU `tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.

Examples:
  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
  tar -tvf archive.tar         # List all files in archive.tar verbosely.
  tar -xf archive.tar          # Extract all files from archive.tar.

 Main operation mode:

  -A, --catenate, --concatenate   append tar files to an archive
  -c, --create               create a new archive
  -d, --diff, --compare      find differences between archive and file system
      --delete               delete from the archive (not on mag tapes!)
  -r, --append               append files to the end of an archive
  -t, --list                 list the contents of an archive
  -u, --update               only append files newer than copy in archive
  -x, --extract, --get       extract files from an archive

 Operation modifiers:

  -g, --listed-incremental=FILE   handle new GNU-format incremental backup
  -G, --incremental          handle old GNU-format incremental backup
      --ignore-failed-read   do not exit with nonzero on unreadable files
  -k, --keep-old-files       don't replace existing files when extracting
      --keep-newer-files     don't replace existing files that are newer than
                             their archive copies
      --no-overwrite-dir     preserve metadata of existing directories
  -n, --seek                 Archive is seekable
      --occurrence[=NUMBER]  process only the NUMth occurrence of each file in
                             the archive. This option is valid only in
                             conjunction with one of the subcommands --delete,
                             --diff, --extract or --list and when a list of
                             files is given either on the command line or via
                             -T option. NUMBER defaults to 1.
      --overwrite            overwrite existing files when extracting
  -O, --to-stdout            extract files to standard output
      --recursive-unlink     empty hierarchies prior to extracting directory
      --remove-files         remove files after adding them to the archive
  -S, --sparse               handle sparse files efficiently
  -U, --unlink-first         remove each file prior to extracting over it
  -W, --verify               attempt to verify the archive after writing it

 Handling of file attributes:

      --acls                 Save the ACLs to the archive
      --atime-preserve       don't change access times on dumped files
      --group=NAME           force NAME as group for added files
      --mode=CHANGES         force (symbolic) mode CHANGES for added files
  -m, --touch                don't extract file modified time
      --no-acls              Don't extract the ACLs from the archive
      --no-same-owner        extract files as yourself
      --no-same-permissions  do not extract permissions information
      --no-selinux           Don't extract the SELinux context from the archive

      --no-xattrs            Don't extract the user/root xattrs from the
                             archive
      --numeric-owner        always use numbers for user/group names
      --owner=NAME           force NAME as owner for added files
  -p, --preserve-permissions, --same-permissions
                             extract permissions information
      --preserve             same as both -p and -s
      --same-owner           try extracting files with the same ownership
  -s, --preserve-order, --same-order
                             sort names to extract to match archive
      --selinux              Save the SELinux context to the archive
      --xattrs               Save the user/root xattrs to the archive

 Device selection and switching:

  -f, --file=ARCHIVE         use archive file or device ARCHIVE
      --force-local          archive file is local even if has a colon
  -F, --info-script=NAME, --new-volume-script=NAME
                             run script at end of each tape (implies -M)
  -L, --tape-length=NUMBER   change tape after writing NUMBER x 1024 bytes
  -M, --multi-volume         create/list/extract multi-volume archive
      --rmt-command=COMMAND  use given rmt COMMAND instead of rmt
      --rsh-command=COMMAND  use remote COMMAND instead of rsh
      --volno-file=FILE      use/update the volume number in FILE

 Device blocking:

  -b, --blocking-factor=BLOCKS   BLOCKS x 512 bytes per record
  -B, --read-full-records    reblock as we read (for 4.2BSD pipes)
  -i, --ignore-zeros         ignore zeroed blocks in archive (means EOF)
      --record-size=NUMBER   SIZE bytes per record, multiple of 512

 Archive format selection:

  -H, --format=FORMAT        create archive of the given format.

 FORMAT is one of the following:

    gnu                      GNU tar 1.13.x format
    oldgnu                   GNU format as per tar <= 1.12
    pax                      POSIX 1003.1-2001 (pax) format
    posix                    Same as pax
    ustar                    POSIX 1003.1-1988 (ustar) format
    v7                       old V7 tar format

  -j, --bzip2                filter the archive through bzip2
      --old-archive, --portability
                             same as --format=v7
      --pax-option=keyword[[:]=value][,keyword[[:]=value], ...]
                             control pax keywords
      --posix                same as --format=posix
      --use-compress-program=PROG
                             filter through PROG (must accept -d)
  -V, --label=TEXT           create archive with volume name NAME. At
                             list/extract time, use TEXT as a globbing pattern
  -z, --gzip, --gunzip, --ungzip   filter the archive through gzip
  -Z, --compress, --uncompress   filter the archive through compress

 Local file selection:

      --after-date=DATE      same as -N
      --anchored             exclude patterns match file name start
      --backup[=CONTROL]     backup before removal, choose version CONTROL
  -C, --directory=DIR        change to directory DIR
      --exclude=PATTERN      exclude files, given as a PATTERN
      --exclude-caches       exclude directories containing a cache tag
  -h, --dereference          dump instead the files symlinks point to
      --ignore-case          exclusion ignores case
  -K, --starting-file=MEMBER-NAME
                             begin at member MEMBER-NAME in the archive
      --newer-mtime=DATE     compare date and time when data changed only
      --no-anchored          exclude patterns match after any / (default)
      --no-ignore-case       exclusion is case sensitive (default)
      --no-recursion         avoid descending automatically in directories
      --no-wildcards         exclude patterns are plain strings
      --no-wildcards-match-slash   exclude pattern wildcards do not match '/'
      --null                 -T reads null-terminated names, disable -C
  -N, --newer=DATE-OR-FILE   only store files newer than DATE-OR-FILE
      --one-file-system      stay in local file system when creating archive
  -P, --absolute-names       don't strip leading `/'s from file names
      --recursion            recurse into directories (default)
      --strip-components=NUMBER   strip NUMBER leading components from file
                             names
      --suffix=STRING        backup before removal, override usual suffix ('~'
                             unless overridden by environment variable
                             SIMPLE_BACKUP_SUFFIX
  -T, --files-from=FILE-OF-NAMES   get names to extract or create from file
                             NAME
      --wildcards            exclude patterns use wildcards (default)
      --wildcards-match-slash   exclude pattern wildcards match '/' (default)
  -X, --exclude-from=FILE    exclude patterns listed in FILE

 Informative output:

      --checkpoint           display progress messages every 10th record
  -v, --verbose              verbosely list files processed

      --check-links          print a message if not all links are dumped
      --index-file=FILE      send verbose output to FILE
  -R, --block-number         show block number within archive with each
                             message
      --show-defaults        Show tar defaults
      --show-omitted-dirs    When listing or extracting, list each directory
                             that does not match search criteria
      --totals               print total bytes written while creating archive
      --utc                  print file modification dates in UTC
  -w, --interactive, --confirmation
                             ask for confirmation for every action

 Compatibility options:

      --allow-name-mangling  when creating, allow GNUTYPE_NAMES mangling --
                             considered dangerous
  -o                         when creating, same as --old-archive. When
                             extracting, same as --no-same-owner

 Other options:

  -?, --help                 Give this help list
      --license              Print license and exit
      --usage                Give a short usage message
      --version              Print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control may be set with --backup or VERSION_CONTROL, values are:

  t, numbered     make numbered backups
  nil, existing   numbered if numbered backups exist, simple otherwise
  never, simple   always make simple backups

*This* tar defaults to:
--format=gnu -f- -b20 --rmt-command=/sbin/rmt

Report bugs to <bug-tar@gnu.org>.
[multi@localhost ~]$


3. tar를 사용한 풀백업과 증분백업

3-1. 증분백업하기

앞서 tar 명령을 사용하여 압축하는 방법에 대해서 알아보았다.

이번에는 tar 명령을 사용하여 증분백업을 하는 방법에 대해 알아보겠다.

일반적으로 tar를 사용해서 전체적인 풀백업을 하곤 한다. 하지만 풀백업을 하면 시간이 많이 걸릴뿐만 아니라 컴퓨터의 각종 자원낭비가 커질 수 있겠다. 그래서 보통 증분백업 방법을 많이 사용한다.

이 증분백업에는 -g snapshot-file 옵션을 사용하는데 지정한 스냅샷 파일에는 압축한 파일에 대한 정보를 숫자로 표시한다. 이 정보를 사용해서 이미 압축된 파일과 비교해서 증분백업을 수행하게 되는것이다.

위에서 만든 /home/multi/test1 디렉토리를 증분백업으로 백업해 보도록 하자

먼저 증분백업을 저장해둘 디렉토리로 /home/multi/backup 디렉토리를 생성한다.
 

[multi@localhost ~]$ mkdir backup
[multi@localhost ~]$ ls -al|grep backup
drwxrwxr-x  2 multi multi 4096  2월 21 02:16 backup
[multi@localhost ~]$


그리고 방금 만든 백업 디렉토리에 test1.tgz 파일로 압축한다. 그리고 스냅샷 이름을 snap-test1 이라고 지정한다.
 

[multi@localhost ~]$ tar -g ~/backup/snap-test1 -czf /home/multi/backup/test1.tgz -C /home/multi ./test1
[multi@localhost ~]$ ls -l ./backup/
합계 8
-rw-rw-r-- 1 multi multi  11  2월 21 02:52 snap-test1
-rw-rw-r-- 1 multi multi 235  2월 21 02:52 test1.tgz
[multi@localhost ~]$


백업의 원본 파일이 만들어 졌으니, test1 디렉토리아래에 linux.txt 파일을 만들어 넣고, 다시 백업해 보도록 하자.
스냅샷 파일을 이용하여 증분 백업시에는 원본 백업파일에 추가적인 파일들만 백업하므로 백업파일명을 다른이름으로 지정하도록 한다.

여기서는 증분백업파일명을 test1-ymdHMS.tgz (날짜와 시간)형식으로 지정했다.

이렇게 증분백업 파일명에 자동으로 초단위 시간값이 입력되도록 하여 중복되지 않고 구분이 쉽도록 하였다.
 

tar -g ~/backup/snap-test1 -czf /home/multi/backup/test1-`date '+%y%m%d%H%M%S'`.tgz -C /home/multi ./test1

 

[multi@localhost ~]$ echo "linux" > ./test1/linux.txt
[multi@localhost ~]$ ls -l ./test1/
합계 16
-rw-rw-r-- 1 multi multi 6  2월 21 02:53 linux.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test1.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test2.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test3.txt
[multi@localhost ~]$ tar -g ~/backup/snap-test1 -czf /home/multi/backup/test1-`date '+%y%m%d%H%M%S'`.tgz -C /home/multi ./test1
[multi@localhost ~]$ ls -l ./backup/
합계 12
-rw-rw-r-- 1 multi multi  11  2월 21 02:54 snap-test1
-rw-rw-r-- 1 multi multi 194  2월 21 02:54 test1-080221025404.tgz
-rw-rw-r-- 1 multi multi 235  2월 21 02:52 test1.tgz
[multi@localhost ~]$
 


증분백업시 위의 내용을 보면 test1-080221025404.tgz 파일이 추가적으로 만들어 졌는데 그 내용을 보면 아래와 같다.
 

[multi@localhost ~]$ tar -tf ./backup/test1.tgz
./test1/
./test1/test1.txt
./test1/test2.txt
./test1/test3.txt
[multi@localhost ~]$ tar -tf ./backup/test1-080221025404.tgz
./test1/
./test1/linux.txt
[multi@localhost ~]$ cat ./backup/snap-test1
1203530045
[multi@localhost ~]$


위에서 보는것과 같이 초기 백업파일을 생성한 다음, 추가로 생성한 linux.txt 파일만 test1-080221025404.tgz파일에 포함되어 있는 것을 볼 수 있다.

이와 같이 새로운 백업파일을 생성할때 초기에 생성한 파일의 스냅샷 파일을 참고하여 추가로 생성된 파일만 새로운 압축파일로 생성 하도록 하는것이다. 이것이 바로 증분백업 방식이다.

그러면 다시 centos.txt 파일을 추가하고 증분백업을 해보자.
 

[multi@localhost ~]$ echo "centos" > ./test1/centos.txt
[multi@localhost ~]$ tar -g ~/backup/snap-test1 -czf /home/multi/backup/test1-`date '+%y%m%d%H%M%S'`.tgz -C /home/multi ./test1
[multi@localhost ~]$ ls -l ./backup/
합계 16
-rw-rw-r-- 1 multi multi  11  2월 21 02:57 snap-test1
-rw-rw-r-- 1 multi multi 194  2월 21 02:54 test1-080221025404.tgz
-rw-rw-r-- 1 multi multi 200  2월 21 02:57 test1-080221025704.tgz
-rw-rw-r-- 1 multi multi 235  2월 21 02:52 test1.tgz
[multi@localhost ~]$ cat ./backup/snap-test1
1203530224
[multi@localhost ~]$ tar -tf ./backup/test1.tgz
./test1/
./test1/test1.txt
./test1/test2.txt
./test1/test3.txt
[multi@localhost ~]$ tar -tf ./backup/test1-080221025404.tgz
./test1/
./test1/linux.txt
[multi@localhost ~]$ tar -tf ./backup/test1-080221025704.tgz
./test1/
./test1/centos.txt
[multi@localhost ~]$


3-2. 증분백업 복구하기

이번에는 앞서 증분백업한 내용을 복구해 보도록 하자.

백업파일들은 아래와 같다.
 

[multi@localhost ~]$ pwd
/home/multi
[multi@localhost ~]$ cd backup/
[multi@localhost backup]$ ls -l
합계 16
-rw-rw-r-- 1 multi multi  11  2월 21 02:57 snap-test1
-rw-rw-r-- 1 multi multi 194  2월 21 02:54 test1-080221025404.tgz
-rw-rw-r-- 1 multi multi 200  2월 21 02:57 test1-080221025704.tgz
-rw-rw-r-- 1 multi multi 235  2월 21 02:52 test1.tgz
[multi@localhost backup]$


백업을 복구하기 위해서는 아래와 같이 복구하도록 한다.
먼저 스냅샷 파일을 만들기위해 최초로 만든 백업파일인 test1.tgz파일을 압축해제하고, 다음으로 두개의 압축파일을 해제하면 원본 디렉토리의 파일들과 같게 된다.
 

[multi@localhost backup]$ pwd
/home/multi/backup
[multi@localhost backup]$ tar xzf test1.tgz
[multi@localhost backup]$ ls -l
합계 20
-rw-rw-r-- 1 multi multi   11  2월 21 02:57 snap-test1
drwxrwxr-x 2 multi multi 4096  2월 21 02:52 test1
-rw-rw-r-- 1 multi multi  194  2월 21 02:54 test1-080221025404.tgz
-rw-rw-r-- 1 multi multi  200  2월 21 02:57 test1-080221025704.tgz
-rw-rw-r-- 1 multi multi  235  2월 21 02:52 test1.tgz
[multi@localhost backup]$ tar -xzf test1-080221025404.tgz
[multi@localhost backup]$ ls -l ./test1
합계 16
-rw-rw-r-- 1 multi multi 6  2월 21 02:53 linux.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test1.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test2.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test3.txt
[multi@localhost backup]$ tar -xzf test1-080221025704.tgz
[multi@localhost backup]$ ls -l ./test1
합계 20
-rw-rw-r-- 1 multi multi 7  2월 21 02:56 centos.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:53 linux.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test1.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test2.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test3.txt
[multi@localhost backup]$


원본파일 내용 (/home/multi/test1 디렉토리 내용)
 

[multi@localhost backup]$ ls -l /home/multi/test1
합계 20
-rw-rw-r-- 1 multi multi 7  2월 21 02:56 centos.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:53 linux.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test1.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test2.txt
-rw-rw-r-- 1 multi multi 6  2월 21 02:00 test3.txt
[multi@localhost backup]$


원본 파일과 비교했을때 파일의 정보는 모두 동일함을 알 수 있다.

3-3. tar 증분백업의 자동화

증분 백업의 자동화는 cron을 이용하면 되겠다.

먼저 일주일 동안 증분백업을 한다고 정하였다면, 가장 먼저 주말 새벽에 스냅샷 파일을 사용하여 초기 백업파일을 생성해 두고, /etc/cron.daily 디렉토리안에 아래와 같은 명령어를 실행하도록 스크립트 파일을 작성해 두면 매일 지정된 시간에 자동으로 증분 백업을 받을수 있다.
 

vi /etc/cron.daily/backup.sh

#/bin/sh
tar -g /home/multi/backup/snap-test1 -czf /home/multi/backup/test1-`date '+%y%m%d%H%M%S'`.tgz -C /home/multi ./test1


tar 증분백업시에는 파일이 많아지면 나중에 복구할때 모든 압축파일을 해제해야 하므로 귀찮아 질 수 있다.

그래서 매주 한번씩 특정시간에 백업디렉토리의 모든 파일을 삭제하고, 스냅샷 파일을 이용해서 새로운 초기 백업파일을 만들도록 해 두고, 일주일동안 원본을 제외한 6개의 파일만 생성되도록 하는 것이다.

백업파일 재생성 스크립트는 아래와 같이 하면 되겠다.

test1.tgz 파일은 다시 백업하면 덮어쓰기 때문에 지워줄 필요가 없으며, 스냅샷 파일과 증분백업파일들만 삭제하고 새로 증분백업을 수행하도록 스크립트를 작성한다.
 

vi /etc/cron.weekly/backup_init.sh

#/bin/sh
rm -rf /home/multi/backup/snap-test1
rm -rf /home/multi/backup/test1-*
tar -g /home/multi/backup/snap-test1 -czf /home/multi/backup/test1.tgz -C /home/multi ./test1


초기 백업파일인 test1.tgz 파일은 다시 백업하면 다시 덮어쓰기 때문에 지워줄 필요가 없겠다.

[참고 사항]
리눅스상에서 스크립트를 작성시에는 실행시 실행권한이 있어야 하므로 .sh형태로 만든 스크립트 파일들은 항상 chmod u+x [스크립트 파일] 명령을 실행해 주도록 한다.


-끝-

댓글목록

등록된 댓글이 없습니다.

Total 138건 8 페이지
리눅스 Tip 목록
번호 제목 글쓴이 조회 날짜
12 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 15937 05-19
11 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 17505 05-15
10 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 15827 05-15
열람중 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 11929 05-13
8 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 15744 05-13
7 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18087 05-13
6 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 17859 05-13
5 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 16960 05-13
4 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 15777 05-13
3 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 16855 05-13
2 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 18765 05-13
1 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 15132 05-13
게시물 검색