Backup rsync

1. Initial source folder structure:

me@MacBook:~/tmp$ find source
source
source/.DS_Store
source/a
source/b
source/dev
source/dev/.DS_Store
source/dev/x
source/dev/y.orig.vhd
source/dev/y.vhd

2. Lets backup source folder to destination folder using rsync

me@MacBook:~/tmp$ rsync -abvP  --backup-dir=backup_`date +%Y-%m-%d--%H-%M-%S` --include=*.orig.vhd --exclude={/backup_*,.DS_Store,*.vhd} --delete source/ destination/
building file list ... 
6 files to consider
created directory destination
./
a
           1 100%    0.00kB/s    0:00:00 (xfer#1, to-check=4/6)
b
           1 100%    0.98kB/s    0:00:00 (xfer#2, to-check=3/6)
dev/
dev/x
           1 100%    0.98kB/s    0:00:00 (xfer#3, to-check=1/6)
dev/y.orig.vhd
           1 100%    0.98kB/s    0:00:00 (xfer#4, to-check=0/6)

sent 343 bytes  received 120 bytes  926.00 bytes/sec
total size is 4  speedup is 0.01

explanation:

--include=*.orig.vhd

do not exclude files *.orig.vhd for any source (sub)directories (since include is before exclude then inclusion will first match rather then exclusion from *.vhd)

--exclude={/backup_*,.DS_Store,*.vhd}

copy all files excluding ~/tmp/backup_* files, excluding all .DS_Store and *.vhd files in any (sub)directories

3. The destination directory content:

me@MacBook:~/tmp$ find destination
destination
destination/a
destination/b
destination/dev
destination/dev/x
destination/dev/y.orig.vhd

4. Lets modify one file and delete another and then run the same rsync command:

me@MacBook:~/tmp$ echo "2" > source/a 
me@MacBook:~/tmp$ rm source/dev/x
me@MacBook:~/tmp$ rsync -abvP  --backup-dir=backup_`date +%Y-%m-%d--%H-%M-%S` --include=*.orig.vhd --exclude={/backup_*,.DS_Store,*.vhd} --delete source/ destination/
building file list ... 
5 files to consider
deleting dev/x
./
a
           2 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/5)
dev/

sent 195 bytes  received 54 bytes  498.00 bytes/sec
total size is 4  speedup is 0.02
--backup-dir=backup_`date +%Y-%m-%d--%H-%M-%S`

creates a backup folder in destination dir with previous versions of modified or deleted files

--delete

deletes the file in destination folder if they were deleted in the source dir

5. Then the destination folder will contain backup_2016-10-29–12-26-06 with previous version of a and dev/x before deletion:

me@MacBook:~/tmp$ find destination
destination
destination/a
destination/b
destination/backup_2016-10-29--12-26-06
destination/backup_2016-10-29--12-26-06/a
destination/backup_2016-10-29--12-26-06/dev
destination/backup_2016-10-29--12-26-06/dev/x
destination/dev
destination/dev/y.orig.vhd
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s