Rsync에서 update, delete 사용

폴더 동기화 하기

여기도 확인.

https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories

rsync 로 source 와 local folder 를 동기화 하고자 한다.

이런 상황을 destination 에 동기화해서 적용하는 것이다.

  1. --dry-run : 전송 전에 전송할 목록을 확인한다.

  2. --update : “”

목차

  1. 전체 동기화
  2. local 기준 동기화
  3. checksum 사용



1. 전체 동기화

  1. source 모든 파일
  2. source 새 파일 추가
  3. source 수정한 파일
  4. source 삭제된 파일

source 모든 파일

source 폴더는 다음 같다.

1
2
3
source:~/$ mkdir tst
source:~/$ cd test/
source:~/test$ touch a.txt b.txt
1
2
3
4
5
6
~$ rsync -avzh --dry-run /source/ /local/
receiving incremental file list
./
a.txt
b.txt

동기화를 실행한다.

1
2
3
4
5
6
7
8
~$(3.11.7)qkboo:~$ rsync -avzh --update --delete --e 'ssh -p 2020' qkboo@220.121.133.119:~/Q
kboo/test/ test/
receiving incremental file list
deleting c.txt
./

sent 51 bytes received 96 bytes 294.00 bytes/sec
total size is 0 speedup is 0.00

source 새 파일 추가

qkboo@homebook:~/Qkboo/test$ touch c.txt

source 파일 수정 상황

1
qkboo@homebook:~/Qkboo/test$ vi c.txt

source 폴더에 삭제 발생

1
qkboo@homebook:~/Qkboo/test$ rm c.txt

dry-run 을 실행해 확인하면 --update --delete 를 사용하면 로컬 파일이 삭제 된다.

1
2
3
4
5
6
7
~$ rsync -avzh --dry-run --update --delete --e 'ssh -p 2020' qkboo@220.121.133.119:~/Qkboo/test/ test/
receiving incremental file list
deleting c.txt
./

sent 51 bytes received 96 bytes 294.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)

동기화를 실행한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
~$(3.11.7)qkboo:~$ rsync -avzh --update --delete --e 'ssh -p 2020' qkboo@220.121.133.119:~/Q
kboo/test/ test/
receiving incremental file list
deleting c.txt
./

sent 51 bytes received 96 bytes 294.00 bytes/sec
total size is 0 speedup is 0.00

(3.11.7)qkboo:~$ ll test/
total 8
drwxrwxr-x 2 qkboo qkboo 4096 Jul 20 09:37 ./
drwxr-xr-x 39 qkboo qkboo 4096 Jul 20 09:34 ../
-rw-rw-r-- 1 qkboo qkboo 0 Jul 20 09:16 a.txt
-rw-rw-r-- 1 qkboo qkboo 0 Jul 20 09:16 b.txt

로컬에서 작업이 발생하는 경우

rsync 시 source/ 와 source/* 의 결과 차이가 발생한다.

로컬에 새 파일이 생기는 경우

source 와 destination 폴더 동기화 된 상태에서 destination 폴더에 새 파일이 추가된다.

1
2
3
4
5
6
7
(3.11.7)qkboo:~$ touch test/local.txt; ll test/
total 8
drwxrwxr-x 2 qkboo qkboo 4096 Jul 20 09:59 ./
drwxr-xr-x 39 qkboo qkboo 4096 Jul 20 09:34 ../
-rw-rw-r-- 1 qkboo qkboo 0 Jul 20 09:16 a.txt
-rw-rw-r-- 1 qkboo qkboo 0 Jul 20 09:16 b.txt
-rw-r--r-- 1 qkboo qkboo 0 Jul 20 09:59 local.txt

source 폴더에 / 구분자만 사용해서 확인해 본다.

dry-run 을 실행해 확인하면 --update --delete 를 사용하면 로컬 파일이 삭제 된다.

1
2
3
4
5
6
7
8
(3.11.7)qkboo:~$ rsync -avzh --update --delete --dry-run --e 'ssh -p 2020' qkboo@220.121.1
33.119:~/Qkboo/test/ test/
receiving incremental file list
deleting local.txt
./

sent 51 bytes received 96 bytes 98.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)

--ignore-existing 도 삭제가 발생한다.

1
2
3
4
5
6
7
~$ rsync -avzh --update --delete --ignore-existing --dry-run --e 'ssh -p 2020' qkboo@220.121.133.119:~/Qkboo/test/ test/
receiving incremental file list
deleting local.txt
./

sent 51 bytes received 96 bytes 98.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)

--update 만 사용하면 로컬에 변경있는 파일은 무시된다.

1
2
3
4
5
6
7
~$ rsync -avzh --update --dry-run --e 'ssh -p 2020' qkboo@220.121.133.119:~/
Qkboo/test/ test/
receiving incremental file list
./

sent 51 bytes received 96 bytes 294.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)

source/* 로 사용하면 --update --delete 를 사용해도 로컬 파일이 무시된다.

1
2
3
4
5
6
7
8
(3.11.7)qkboo:~$ rsync -avzh --update --delete --dry-run --e 'ssh -p 2020' qkboo@220.121.1
33.119:~/Qkboo/test/ test/
receiving incremental file list
deleting local.txt
./

sent 51 bytes received 96 bytes 98.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)

source 와 local 모두 새 파일 생기면

1
source/ $ touch d.txt

source/* 로 사용하면 --update --delete 로 새 파일을 받는다.

1
2
3
4
5
6
(3.11.7)qkboo:~$ rsync -avzh --update --delete --dry-run --e 'ssh -p 2020' qkboo@220.121.133.119:~/Qkboo/test/* test/
receiving incremental file list
d.txt

sent 51 bytes received 96 bytes 294.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)

local 에도 새 파일이 생기면

1
local/ $ touch test/local2.txt

source/* 로 사용하고 --update --delete 로 새 파일을 받고 로컬은 유지된다.

1
2
3
4
5
6
local:~/ $ rsync -avzh --update --delete --dry-run --e 'ssh -p 2020' qkboo@220.121.133.119:~/Qkboo/test/* test/
receiving incremental file list
d.txt

sent 51 bytes received 96 bytes 98.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
ll test/
1
2
3
4
5
6
7
8
total 8
drwxrwxr-x 2 qkboo qkboo 4096 Jul 20 10:46 ./
drwxr-xr-x 39 qkboo qkboo 4096 Jul 20 09:34 ../
-rw-rw-r-- 1 qkboo qkboo 0 Jul 20 09:16 a.txt
-rw-rw-r-- 1 qkboo qkboo 0 Jul 20 09:16 b.txt
-rw-rw-r-- 1 qkboo qkboo 0 Jul 20 10:40 d.txt
-rw-r--r-- 1 qkboo qkboo 0 Jul 20 09:59 local.txt
-rw-r--r-- 1 qkboo qkboo 0 Jul 20 10:43 local2.txt

정리

전체 동기화 후 source,local 모두 변경이 발생

  1. local 기준으로 동기화 하려면 srouce/* 를 꼭 사용한다.
  2. 혹은 local 기준으로 동기화시 --update 만 사용한다.

source 기준으로 동기화 하려면

  1. source 기준으로 동기화 하려면 srouce/ 로 사용한다.


---

[참고] source 와 local 의 같은 파일 동시 수정되면?

양쪽 모두 파일 내용의 갱신이 발생하는 이런 경우는 가능하면 VCS 도구를 사용하자!!!

다음 같이 동기화 한 상태에서 a.txt 를 source 와 local 에서 모두 수정한다면?

ll test/
1
2
3
4
5
6
7
8
total 8
drwxrwxr-x 2 qkboo qkboo 4096 Jul 20 10:46 ./
drwxr-xr-x 39 qkboo qkboo 4096 Jul 20 09:34 ../
-rw-rw-r-- 1 qkboo qkboo 0 Jul 20 09:16 a.txt
-rw-rw-r-- 1 qkboo qkboo 0 Jul 20 09:16 b.txt
-rw-rw-r-- 1 qkboo qkboo 0 Jul 20 10:40 d.txt
-rw-r--r-- 1 qkboo qkboo 0 Jul 20 09:59 local.txt
-rw-r--r-- 1 qkboo qkboo 0 Jul 20 10:43 local2.txt

source/a.txt 에는 ‘AAA’ 문자열이 추가했고, local/a.txt 는 ‘local’ 단어로 변경했다.

단, 다음 같이 비슷한 시간대에 수정한 양쪽의 파일에는 변화가 없다.

source 의 a.txt 파일

1
2
3
$ ls -l a.txt
total 4
-rw-rw-r-- 1 qkboo qkboo 4 Jul 20 10:49 a.txt

local 의 a.txt 파일

1
2
$ ls -l a.txt
-rw-rw-r-- 1 qkboo qkboo 6 Jul 20 10:49 a.txt

변화가 없다.

1
2
3
4
5
~$ rsync -avzh --update --delete --dry-run --e 'ssh -p 2020' qkboo@220.121.133.119:~/Qkboo/test/* test/
receiving incremental file list

sent 44 bytes received 98 bytes 94.67 bytes/sec
total size is 4 speedup is 0.03 (DRY RUN)

시간과 Byte 변화가 있으면 다음 같이 갱신을 수행한다.

1
2
3
4
5
6
7
~$
local:~$ rsync -avzh --update --delete --dry-run source/test/* local/test/
receiving incremental file list
a.txt

sent 51 bytes received 100 bytes 302.00 bytes/sec
total size is 228 speedup is 1.51 (DRY RUN)

source/a.txt 에 Byte 변화가 좀 커지면 동기화가 수행된다.

qkboo@homebook:~/Qkboo/test$ ls -l a.txt
-rw-rw-r– 1 qkboo qkboo 16 Jul 20 10:56 a.txt

source/a.txt 가 local에 동기화가 되고 로컬은 source/a.txt로 교체된다.

1
2
3
4
5
6
7
~$
(3.11.7)qkboo:~$ rsync -avzh --update --delete --dry-run --e 'ssh -p 2020' qkboo@220.121.133.119:~/Qkboo/test/* test/
receiving incremental file list
a.txt

sent 51 bytes received 100 bytes 100.67 bytes/sec
total size is 16 speedup is 0.11 (DRY RUN)

양쪽 모두 갱신이 발생하는 이런 경우는 가능하면 VCS 도구를 사용하자!!!

3. checksum 사용

1
2
3
~$
local:~$ rsync -avzh --update --delete --checksum --dry-run source/test/* local/test/
receiving incremental file list
Author

Gangtai Goh

Posted on

2024-06-13

Updated on

2024-11-08

Licensed under