Friday, November 13, 2015

Folders and files synchronisation using rsync in Linux


# Creat source folder and files and blank destination folder
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ mkdir src dst
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ mkdir src/a 
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ echo "hell a" > src/a/file1.txt
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
└── src
    └── a
        └── file1.txt

# rsync to test folder and file synch. Both 'a' and 'file1' appeared under 'dst'.
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   └── a
│       └── file1.txt
└── src
    └── a
        └── file1.txt

# Test newly added folder 'b' being synched
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ mkdir src/b
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   ├── a
│   │   └── file1.txt
│   └── b
└── src
    ├── a
    │   └── file1.txt
    └── b

# Test newly added file 'file2.txt' being synched
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ echo "hell b" > src/b/file2.txt
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   ├── a
│   │   └── file1.txt
│   └── b
│       └── file2.txt
└── src
    ├── a
    │   └── file1.txt
    └── b
        └── file2.txt

# Test newly delete folder 'a' and 'file1.txt' inside being synched.
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rm -rf src/a/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   └── b
│       └── file2.txt
└── src
    └── b
        └── file2.txt

# Test newly delete file 'file2.txt' being synched.
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rm src/b/file2.txt 
rm: remove regular file ‘src/b/file2.txt’? y
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   └── b
└── src
    └── b

# Add back 'file2.txt' and check
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ echo "hell b" > src/b/file2.txt
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   └── b
│       └── file2.txt
└── src
    └── b
        └── file2.txt

# modify 'file2.txt' and check synch
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ cat dst/b/file2.txt
hell b
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ vi src/b/file2.txt
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ cat src/b/file2.txt
Modified ...hello b
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ cat dst/b/file2.txt
Modified ...hello b
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   └── b
│       └── file2.txt
└── src
    └── b
        └── file2.txt


Using cp command line for multiple files copy
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   └── b
│       └── file2.txt
└── src
    ├── a
    │   └── file1.txt
    └── b
        ├── file2.txt
        └── file3.txt

5 directories, 4 files
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ cp {src/a/file1.txt,src/b/file{2..3}.txt} dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   ├── b
│   │   └── file2.txt
│   ├── file1.txt
│   ├── file2.txt
│   └── file3.txt
└── src
    ├── a
    │   └── file1.txt
    └── b
        ├── file2.txt
        └── file3.txt

No comments: