소유자와 권한을 동일하게 유지하면서 폴더를 복사하는 방법은 무엇입니까?

소유자와 권한을 동일하게 유지하면서 폴더를 복사하는 방법은 무엇입니까?

그래서 다음과 같이 홈 폴더를 외부 드라이브에 복사하여 백업할 계획입니다.

sudo cp -r /home/my_home /media/backup/my_home

결과적으로 외장 드라이브의 모든 폴더가 이제 소유됩니다 . 원래 소유권과 권한을 유지하려면 root:root어떻게 해야 합니까 ?cp

답변1

sudo cp -rp /home/my_home /media/backup/my_home

cp 맨페이지에서:

 -p     same as --preserve=mode,ownership,timestamps

 --preserve[=ATTR_LIST]
          preserve the specified attributes (default: mode,ownership,timestamps),
          if possible additional attributes: context, links, xattr, all

답변2

을 사용할 수도 있습니다 rsync.

sudo rsync -a /home/my_home/ /media/backup/my_home/

rsync맨페이지 에서 :

 -a, --archive
              This  is  equivalent  to  -rlptgoD.  It  is a quick way of saying you want
              recursion and want to preserve almost everything (with -H being a  notable
              omission).    The   only  exception  to  the  above  equivalence  is  when
              --files-from is specified, in which case -r is not implied.

              Note that -a does not preserve hardlinks, because finding  multiply-linked
              files is expensive.  You must separately specify -H.

cp다음을 비교하려면 이 질문을 참조하세요 rsync.https://stackoverflow.com/q/6339287/406686

뒤에 오는 슬래시에 주의하세요(자세한 내용은 맨페이지 참조).

답변3

cp -a

Where -a의 약어 --archive— 기본적으로 디렉터리를 있는 그대로 복사합니다. 파일은 모든 속성을 유지하며 기호 링크는 역참조되지 않습니다( -d).

에서 man cp:

   -a, --archive
          same as -dR --preserve=all

답변4

다음을 수행할 수 있습니다.

tar cf - my_home | (cd /media/backup; sudo tar xf - )

tar권한, 소유권, 디렉터리 구조는 그대로 유지하되 모든 것을 바이트 스트림으로 변환하세요. 디렉토리를 변경한 다음 tar변환을 되돌리는 "하위 쉘"(괄호 안의 명령)을 실행합니다. 바이트 문자열은 올바른 소유권과 권한을 가진 디렉터리와 파일이 됩니다.

관련 정보