2013. 12. 19. 15:22

[Linux/Ubuntu Backup][01] tar, gzip, bzip2 사용하기

전통적인 Linux의 백업은 압축 명령을 통해 자료를 압축하고, tape등에 저장하는 과정을 의미했다. 압축 도구와 압축 기법, 그리고 저장할 매체는 최근들어 많은 발전을 이뤄왔다. 이제 critical data를 위한 백업과 복원에 대해 알아본다.


tar를 이용하여 백업 하기


tar는 tape archiver의 약자로, unix 시스템으로 거슬러 올라간다.물론 현재 tape는 거의 사용되지 않으며 다른 다양한 매체가 사용된다.


많은 옵션이 사용되지만, 주로, 백업 파일 생성(-c), 백업 파일로 부터 파일 추출(-x), 백업 파일간 차이점 비교(-d), 백업 파일에 파일들의 업데이트(-u), 그리고 백업 파일의 내용 출력(-t) 등이 지원된다. 또한 -r 혹은 -a를 이용하여 파일을 추가할 수 있으며, -d를 통해 삭제할 수 있다.


tar 백업 파일을 생성할 때, 압축 옵션을 추가할 수 있다. 예를 들어, -j는 bzip2 format, -z는 gzip format(solaris는 지원되지 않을 수 있음)와 같다. 보통 .tar 끝에 bzip2로 압축하여 .tar.bz2 혹은 gzip으로 압축하여 .tar.gz가 사용된다.


tar는 소스 코드와 바이너리 배포에 유명한 포맷으로, linux 혹은 unix와 유사한 시스템에서 기본적으로 사용할 수 있기 때문이다.


전통적인 백업 파일 추출방법은 아래와 같다.

~$ tar c *.txt | gzip -c > archive.tar.gz

~$

위 예는 과거의 unix 시스템의 사용법을 예를 든 것이다. tar 명령은 현재 directory의 모든 .txt 파일들로 부터 백업 파일을 생성하도록 c 옵션을 사용하였다. 그 output은 gzip 멸영으로 pipe로 연결되었으며 그것은 -c 옵션을 사용하여 stdout으로 출력한다. 이는 redirect되어 .tar.gz 파일에 저장된다.


이제, 최신의 tar 사용법은 다음과 같다.

~$ tar czf archive.tar.gz *.txt

~$ tar czvf archive.tar.gz *.txt

AcrossTheUniverse.txt

AnnabelLee.txt

BlowingInTheWind.txt

StillFallsTheRain.txt

~$

백업 파일, 즉, archive.tar.gz는 반드시 f 옵션 뒤에 바로 와야한다. z 옵션은 gzip 압축을 의미한다. v는 보다 많은 정보를 출력해 준다.


이제 파일 시스템으로 파일을 분리하려면(unzipping, untarring) 다음과 같다.


우선, unzip(stdio)한뒤 untar를 실행하는 방법이다.

~$ gunzip -c archive.tar.gz | tar x

~$


혹은 다음과 같이 unzip(archive.tar)한뒤, untar를 실행한다.

~$ gunzip archive.tar.gz ; tar xf archive.tar

~$ ls -la

total 36

drwxr-xr-x  2 greenfish greenfish  4096 2013-12-19 16:17 .

drwxr-xr-x 23 greenfish greenfish  4096 2013-12-19 16:09 ..

-rw-r--r--  1 greenfish greenfish  1221 2013-12-19 15:50 AcrossTheUniverse.txt

-rw-r--r--  1 greenfish greenfish  1476 2013-12-19 15:45 AnnabelLee.txt

-rw-r--r--  1 greenfish greenfish 10240 2013-12-19 16:09 archive.tar

-rw-r--r--  1 greenfish greenfish   895 2013-12-19 15:51 BlowingInTheWind.txt

-rw-r--r--  1 greenfish greenfish  1506 2013-12-19 15:45 StillFallsTheRain.txt

~$

즉, 위와 같은 경우, 명령 수행후 archive.tar.gz 파일은 없어지고, archive.tar 파일이 새로 생성됨이 확인되니 유의 바란다.


~$ tar xzvf archive.tar.gz 

AcrossTheUniverse.txt

AnnabelLee.txt

BlowingInTheWind.txt

StillFallsTheRain.txt

~$

위 예는, 최근의 일반적인 사용법으로, x는 백업 파일을 분리하겠다는 뜻이고, z는 압축 해제(unzip), v는 자세한 정보, 그리고 f는 바로뒤에 따라오는 백업 파일의 이름을 의미한다.


bzip2 사용하기


파일을 백업하는데에 압축은 중요한 작업중 하나이다. 압축이 저장 공간과 전송 시간을 줄여주지만, CPU 사용량은 올라가기 마련이다. bzip2 같은 경우, gzip 보다 일반적으로 느리고 압축률은 높다. 이러한 bzip2를 사용하려면 -j 옵션을 사용하며, 그 예는 다음과 같다. 


~$ tar cjvf archive.tar.bz2 *.txt

AcrossTheUniverse.txt

AnnabelLee.txt

BlowingInTheWind.txt

StillFallsTheRain.txt

~$


백업 파일을 풀려면, 다음과 같다.

~$ tar xjvf archive.tar.bz2

AcrossTheUniverse.txt

AnnabelLee.txt

BlowingInTheWind.txt

StillFallsTheRain.txt

~$


gzip으로 압축하기


다음은 gzip으로 압축하는 예이다.

~$ ls -la

total 24

drwxr-xr-x  2 greenfish greenfish 4096 2013-12-19 16:39 .

drwxr-xr-x 23 greenfish greenfish 4096 2013-12-19 16:38 ..

-rw-r--r--  1 greenfish greenfish 1221 2013-12-19 15:50 AcrossTheUniverse.txt

-rw-r--r--  1 greenfish greenfish 1476 2013-12-19 16:38 AnnabelLee.txt

-rw-r--r--  1 greenfish greenfish  895 2013-12-19 15:51 BlowingInTheWind.txt

-rw-r--r--  1 greenfish greenfish 1506 2013-12-19 15:45 StillFallsTheRain.txt

~$ gzip AnnabelLee.txt

~$ ls -la

total 24

drwxr-xr-x  2 greenfish greenfish 4096 2013-12-19 16:39 .

drwxr-xr-x 23 greenfish greenfish 4096 2013-12-19 16:38 ..

-rw-r--r--  1 greenfish greenfish 1221 2013-12-19 15:50 AcrossTheUniverse.txt

-rw-r--r--  1 greenfish greenfish  693 2013-12-19 16:38 AnnabelLee.txt.gz

-rw-r--r--  1 greenfish greenfish  895 2013-12-19 15:51 BlowingInTheWind.txt

-rw-r--r--  1 greenfish greenfish 1506 2013-12-19 15:45 StillFallsTheRain.txt

~$

즉, AnnabelLee.txt가 AnnabelLee.txt.gz으로 변환되었다.


다음은 보다 자세한 정보를 확인할 때 필요한 과정이다.

~$ gzip -v AcrossTheUniverse.txt

AcrossTheUniverse.txt: 67.9% -- replaced with AcrossTheUniverse.txt.gz

~$ gzip -tv AcrossTheUniverse.txt.gz

AcrossTheUniverse.txt.gz: OK

~$ gzip -lv AcrossTheUniverse.txt.gz

method  crc     date  time           compressed        uncompressed  ratio uncompressed_name

defla ce3a44a5 Dec 19 15:50                 432                1221  67.9% AcrossTheUniverse.txt

~$

-v 는 보다 자세한 내용을 표시해주며, -t는 압축된 파일의 무결성 검사를 수행한다.-l은 압축된 파일의 자세한 내용을 표기한다.


directory 단위의 작업은 다음과 같다.

~$ gzip -rv ./

.//BlowingInTheWind.txt: 62.5% -- replaced with .//BlowingInTheWind.txt.gz

.//StillFallsTheRain.txt: 50.7% -- replaced with .//StillFallsTheRain.txt.gz

.//AcrossTheUniverse.txt: 67.9% -- replaced with .//AcrossTheUniverse.txt.gz

.//AnnabelLee.txt: 55.3% -- replaced with .//AnnabelLee.txt.gz

~$

즉, -r 옵션을 이용하여 전달된 directory의 모든 파일에 대해 압축을 시도한다. ./ 대신 * 도 가능하다.


다음은 압축 효율로, 1이 가장 빠르나 압축율이 낮다.

~$ gzip -1 AcrossTheUniverse.txt

~$ gzip -9 StillFallsTheRain.txt

~$


압축된 파일을 해제하기 위해서, gunzip을 실행한다.

~$ gunzip -v StillFallsTheRain.txt.gz

StillFallsTheRain.txt.gz: 50.7% -- replaced with StillFallsTheRain.txt

~$

역시 .gz 파일은 gunzip 호출이후에 삭제된다.


bzip2로 압축하기


~$ bzip2 AnnabelLee.txt

~$ ls -la

total 24

drwxr-xr-x  2 greenfish greenfish 4096 2013-12-19 17:06 .

drwxr-xr-x 23 greenfish greenfish 4096 2013-12-19 16:38 ..

-rw-r--r--  1 greenfish greenfish 1221 2013-12-19 15:50 AcrossTheUniverse.txt

-rw-r--r--  1 greenfish greenfish  685 2013-12-19 16:38 AnnabelLee.txt.bz2

-rw-r--r--  1 greenfish greenfish  895 2013-12-19 15:51 BlowingInTheWind.txt

-rw-r--r--  1 greenfish greenfish 1506 2013-12-19 15:45 StillFallsTheRain.txt

~$ bzip2 -v AcrossTheUniverse.txt 

  AcrossTheUniverse.txt:  2.528:1,  3.165 bits/byte, 60.44% saved, 1221 in, 483 out.

~$ bunzip2 AnnabelLee.txt.bz2 

~$ ls -la

total 24

drwxr-xr-x  2 greenfish greenfish 4096 2013-12-19 17:07 .

drwxr-xr-x 23 greenfish greenfish 4096 2013-12-19 16:38 ..

-rw-r--r--  1 greenfish greenfish  483 2013-12-19 15:50 AcrossTheUniverse.txt.bz2

-rw-r--r--  1 greenfish greenfish 1476 2013-12-19 16:38 AnnabelLee.txt

-rw-r--r--  1 greenfish greenfish  895 2013-12-19 15:51 BlowingInTheWind.txt

-rw-r--r--  1 greenfish greenfish 1506 2013-12-19 15:45 StillFallsTheRain.txt

~$


tar 백업 파일에 파일 열거, 병합, 그리고 추가하기


다음과 같이 tar 파일 내부의 파일들을 열거할 수 있다.

~$ tar cvf Archive.tar *.*

AcrossTheUniverse.txt

AnnabelLee.txt

BlowingInTheWind.txt

StillFallsTheRain.txt

~$ tar tvf Archive.tar

-rw-r--r-- greenfish/greenfish 1221 2013-12-19 17:10 AcrossTheUniverse.txt

-rw-r--r-- greenfish/greenfish 1476 2013-12-19 16:38 AnnabelLee.txt

-rw-r--r-- greenfish/greenfish  895 2013-12-19 15:51 BlowingInTheWind.txt

-rw-r--r-- greenfish/greenfish 1506 2013-12-19 15:45 StillFallsTheRain.txt

~$ tar czvf Archive.tar.gz *.*

AcrossTheUniverse.txt

AnnabelLee.txt

Archive.tar

BlowingInTheWind.txt

StillFallsTheRain.txt

~$ tar tvf Archive.tar.gz

-rw-r--r-- greenfish/greenfish 1221 2013-12-19 17:10 AcrossTheUniverse.txt

-rw-r--r-- greenfish/greenfish 1476 2013-12-19 16:38 AnnabelLee.txt

-rw-r--r-- greenfish/greenfish 10240 2013-12-19 17:11 Archive.tar

-rw-r--r-- greenfish/greenfish   895 2013-12-19 15:51 BlowingInTheWind.txt

-rw-r--r-- greenfish/greenfish  1506 2013-12-19 15:45 StillFallsTheRain.txt

~$ tar tvzf Archive.tar.gz

-rw-r--r-- greenfish/greenfish 1221 2013-12-19 17:10 AcrossTheUniverse.txt

-rw-r--r-- greenfish/greenfish 1476 2013-12-19 16:38 AnnabelLee.txt

-rw-r--r-- greenfish/greenfish 10240 2013-12-19 17:11 Archive.tar

-rw-r--r-- greenfish/greenfish   895 2013-12-19 15:51 BlowingInTheWind.txt

-rw-r--r-- greenfish/greenfish  1506 2013-12-19 15:45 StillFallsTheRain.txt

~$


하나의 tar 파일을 다른 tar에 결합하기 위해 -A 옵션을 사용한다.

~$ tar cvf StillFallsTheRain.tar StillFallsTheRain.txt

StillFallsTheRain.txt

~$ tar cvf AcrossTheUniverse.tar AcrossTheUniverse.txt

AcrossTheUniverse.txt

~$ tar -Af AcrossTheUniverse.tar StillFallsTheRain.tar 

~$ tar tvf AcrossTheUniverse.tar 

-rw-r--r-- greenfish/greenfish 1221 2013-12-19 17:10 AcrossTheUniverse.txt

-rw-r--r-- greenfish/greenfish 1506 2013-12-19 15:45 StillFallsTheRain.txt

~$ tar tvf StillFallsTheRain.tar 

-rw-r--r-- greenfish/greenfish 1506 2013-12-19 15:45 StillFallsTheRain.txt

~$


-r 옵션을 사용하여 기존의 tar에 하나 이상의 파일을 추가한다.

~$ tar tvf AcrossTheUniverse.tar 

-rw-r--r-- greenfish/greenfish 1221 2013-12-19 17:10 AcrossTheUniverse.txt

-rw-r--r-- greenfish/greenfish 1506 2013-12-19 15:45 StillFallsTheRain.txt

~$ tar rvf AcrossTheUniverse.tar BlowingInTheWind.txt

BlowingInTheWind.txt

~$ tar tvf AcrossTheUniverse.tar 

-rw-r--r-- greenfish/greenfish 1221 2013-12-19 17:10 AcrossTheUniverse.txt

-rw-r--r-- greenfish/greenfish 1506 2013-12-19 15:45 StillFallsTheRain.txt

-rw-r--r-- greenfish/greenfish  895 2013-12-19 15:51 BlowingInTheWind.txt

~$


--delete를 통해 tar에 있는 임의의 파일을 삭제할 수 있다. tape 매체는 지원되지 않는다.

~$ tar tvf AcrossTheUniverse.tar 

-rw-r--r-- greenfish/greenfish 1221 2013-12-19 17:10 AcrossTheUniverse.txt

-rw-r--r-- greenfish/greenfish 1506 2013-12-19 15:45 StillFallsTheRain.txt

-rw-r--r-- greenfish/greenfish  895 2013-12-19 15:51 BlowingInTheWind.txt

~$ tar --delete StillFallsTheRain.txt -f AcrossTheUniverse.tar

~$ tar tvf AcrossTheUniverse.tar 

-rw-r--r-- greenfish/greenfish 1221 2013-12-19 17:10 AcrossTheUniverse.txt

-rw-r--r-- greenfish/greenfish  895 2013-12-19 15:51 BlowingInTheWind.txt

~$