파일 소유권을 유지하면서 폴더를 압축하는 방법

파일 소유권을 유지하면서 폴더를 압축하는 방법

백업하고 싶은 도커 서버가 있습니다. 내 배포 폴더에는 내 컨테이너를 지원하기 위한 매우 구체적인 소유권과 권한이 있습니다.

$ ls -lhaF /opt/docker
total 32K
drwxr-xr-x  7 devops devops 4.0K Aug 23 02:34 ./
drwxr-xr-x  6 root   root   4.0K Aug 23 04:20 ../
drwxrwxr-x  2 devops devops 4.0K Aug 21 00:00 .certs/
drwxrwxr-x  2 devops devops 4.0K Aug 23 03:53 .scripts/
-rw-rw-r--  1 devops devops 1.2K Aug 21 01:52 docker-compose.yml
drwxrwxr-x  4   1000   1000 4.0K Aug 21 02:05 minecraft/
drwxrwxr-x  4 devops devops 4.0K Aug 20 23:38 pihole/
drwx------ 19     70     70 4.0K Aug 19 01:31 postgres/

그런 다음 다음 명령을 사용하여 이 폴더를 압축해 봅니다. 내가 읽은 내용에 따르면 파일 권한/소유권을 유지하려면 tar루트/sudo로 실행해야 합니다.

$ sudo tar -czpf "/tmp/server-backup.tar.gz" --directory="/opt/docker" .

이 명령은 폴더를 압축하지만 .tar.gz를 검사하면 모든 폴더의 소유권이 이제 루트임을 알 수 있습니다.

$ tar -tvf /tmp/server-backup.tar.gz
drwxr-xr-x root/root         0 2021-08-23 12:10 ./
drwxr-xr-x root/root         0 2021-08-23 12:10 ./minecraft/
-rw-r--r-- root/root         2 2021-08-23 12:10 ./minecraft/whitelist.json
-rw-r--r-- root/root       111 2021-08-23 12:10 ./minecraft/usercache.json
-rw-r--r-- root/root      1204 2021-08-23 12:10 ./minecraft/server.properties
-rw-r--r-- root/root  43626592 2021-08-23 12:10 ./minecraft/minecraft_server.1.17.1.jar
-rw-r--r-- root/root        68 2021-08-23 12:10 ./minecraft/eula.txt
-rw-r--r-- root/root         2 2021-08-23 12:10 ./minecraft/banned-players.json
-rw-r--r-- root/root         2 2021-08-23 12:10 ./minecraft/banned-ips.json
# -- Other minecraft files
drwxr-xr-x root/root         0 2021-08-23 12:10 ./.certs/
# -- Certs folder contents redacted
drwx------ root/root         0 2021-08-23 12:10 ./postgres/
drwx------ root/root         0 2021-08-23 12:10 ./postgres/pg_subtrans/
-rw------- root/root      8192 2021-08-23 12:10 ./postgres/pg_subtrans/0000
drwx------ root/root         0 2021-08-23 12:10 ./postgres/pg_multixact/
drwx------ root/root         0 2021-08-23 12:10 ./postgres/pg_multixact/offsets/
-rw------- root/root      8192 2021-08-23 12:10 ./postgres/pg_multixact/offsets/0000
drwx------ root/root         0 2021-08-23 12:10 ./postgres/pg_multixact/members/
-rw------- root/root      8192 2021-08-23 12:10 ./postgres/pg_multixact/members/0000
drwx------ root/root         0 2021-08-23 12:10 ./postgres/pg_xact/
-rw------- root/root      8192 2021-08-23 12:10 ./postgres/pg_xact/0000
# -- Other postgres files
drwxr-xr-x root/root         0 2021-08-23 12:10 ./pihole/
drwxr-xr-x root/root         0 2021-08-23 12:10 ./pihole/etc-pihole/
-rw-r--r-- root/root         0 2021-08-23 12:10 ./pihole/etc-pihole/custom.list
-rw-r--r-- root/root   5201920 2021-08-23 12:10 ./pihole/etc-pihole/gravity.db
-rw-r--r-- root/root       485 2021-08-23 12:10 ./pihole/etc-pihole/setupVars.conf
-rw-r--r-- root/root         0 2021-08-23 12:10 ./pihole/etc-pihole/setupVars.conf.update.bak
-rw-r--r-- root/root   1812161 2021-08-23 12:10 ./pihole/etc-pihole/list.1.raw.githubusercontent.com.domains
-rw-r--r-- root/root     73728 2021-08-23 12:10 ./pihole/etc-pihole/pihole-FTL.db
-rw-r--r-- root/root         0 2021-08-23 12:10 ./pihole/etc-pihole/pihole-FTL.conf
-rw-r--r-- root/root        37 2021-08-23 12:10 ./pihole/etc-pihole/local.list
-rw-r--r-- root/root        95 2021-08-23 12:10 ./pihole/etc-pihole/list.1.raw.githubusercontent.com.domains.sha1
-rw-r--r-- root/root        20 2021-08-23 12:10 ./pihole/etc-pihole/localbranches
drwxr-xr-x root/root         0 2021-08-23 12:10 ./pihole/etc-pihole/migration_backup/
-rw-r--r-- root/root        65 2021-08-23 12:10 ./pihole/etc-pihole/migration_backup/adlists.list
-rw-r--r-- root/root       618 2021-08-23 12:10 ./pihole/etc-pihole/dns-servers.conf
-rw-r--r-- root/root        20 2021-08-23 12:10 ./pihole/etc-pihole/GitHubVersions
-rw-r--r-- root/root        44 2021-08-23 12:10 ./pihole/etc-pihole/localversions
drwxr-xr-x root/root         0 2021-08-23 12:10 ./pihole/etc-dnsmasq.d/
-rw-r--r-- root/root      1475 2021-08-23 12:10 ./pihole/etc-dnsmasq.d/01-pihole.conf
drwxr-xr-x root/root         0 2021-08-23 12:10 ./.scripts/
-rwxr-xr-x root/root      1638 2021-08-23 12:10 ./.scripts/create-backup.sh
-rwxr-xr-x root/root       511 2021-08-23 12:10 ./.scripts/new-cert-pihole.sh
-rwxr-xr-x root/root       345 2021-08-23 12:10 ./.scripts/fix-permissions.sh
-rw-r--r-- root/root      1170 2021-08-23 12:10 ./docker-compose.yml

.tar.gz 파일을 추출하려고 하면 모든 소유권이 손실되었음을 확인할 수 있습니다.

$ sudo mkdir /tmp/server-backup
$ sudo tar -xzpf /tmp/server-backup.tar.gz --directory=/tmp/server-backup
$ ls -lhaF /tmp/server-backup
total 32K
drwxr-xr-x  7 root root 4.0K Aug 23 12:10 ./
drwxrwxrwt 13 root root 4.0K Aug 23 12:16 ../
drwxr-xr-x  2 root root 4.0K Aug 23 12:10 .certs/
drwxr-xr-x  2 root root 4.0K Aug 23 12:10 .scripts/
-rw-r--r--  1 root root 1.2K Aug 23 12:10 docker-compose.yml
drwxr-xr-x  4 root root 4.0K Aug 23 12:10 minecraft/
drwxr-xr-x  4 root root 4.0K Aug 23 12:10 pihole/
drwx------ 19 root root 4.0K Aug 23 12:10 postgres/

내가 찾은 바에 따르면 소유권과 권한을 유지하는 것이 tar. 여기에 제가 놓친 것이 있나요? 나는 내가 실행하는 일부 도커 컨테이너에 필요한 섬세한 파일 소유권을 손상시키지 않고 내 서버의 상태를 백업할 수 있기를 원합니다.

시스템 정보:

$ lsb_release -d
Description:    Ubuntu 20.04.3 LTS
$ uname -a
Linux rpi-1 5.4.0-1042-raspi #46-Ubuntu SMP PREEMPT Fri Jul 30 00:35:40 UTC 2021 aarch64 aarch64 aarch64 GNU/Linux

답변1

아무래도 tar범인은 여기가 아닌 것 같습니다. 이는 배포 폴더를 백업하기 위한 더 큰 스크립트의 일부였습니다. 컨테이너가 계속 실행되는 동안 읽기 불일치를 피하기 위해 압축하기 전에 폴더를 복사하고 있었습니다. 스크립트에는 다음 내용이 포함되어 있습니다.

cp -R "/opt/docker" "/tmp/server-backup"
tar -czf "/tmp/server-backup.tar.gz" --directory="/tmp/server-backup" .

폴더 복사 중에 파일 소유권이 손실되었습니다. -p폴더 복사본에 대한 권한/소유권을 유지하려면 플래그가 필요합니다 . 올바른 명령은 다음과 같습니다.

cp -Rp "/opt/docker" "/tmp/server-backup"
tar -czf "/tmp/server-backup.tar.gz" --directory="/tmp/server-backup" .

관련 정보