DEV Community

Cover image for rsync - 10 examples in 11 days (Day 04)
m4r4v
m4r4v

Posted on

rsync - 10 examples in 11 days (Day 04)

Day 04

10 examples in 11 days

Backups

So far, we have been using rsync to sync files between folders. Today, I will talk about the -b option. The b stands for --backup. This option allows us to do backups of files at a different path than the destination folder.

But if we are copying/synching all files/directories, what are the backups for?

Long time ago, I was working in a big company and we needed to sync files across multiple machines and this was a bullet point on our to do list when synching between machines, since then I do this whenever I need to backup synched files.

The --backup=DIR specifies rsync to create a new folder and duplicates all files/directories into DIR, this directory will be created if it doesn't exist already. Somehow is like synching twice our files and/or folders, but when you want to have backups, is just awesome. Very useful when using synching to remote machines.

iamgroot@laptop:~$ rsync -ba --backup-dir=BACKUPS ~/Users/ ~/Sync
iamgroot@laptop:~$ ls -a ~/Sync/ && ls -a ~/Sync/BACKUPS/
.  ..  BACKUPS  file1.txt
.  ..  file1.txt
Enter fullscreen mode Exit fullscreen mode

The BACKUPS folder was created at destination and inside of it you have the same file as in ~/Sync. Obviosuly right now, we are just using the same destination folder, but it comes very useful to put the backups inside the backup disk or different directory. In order to do this, we must give rsync full paths, i.e.: /home/iamgroot/. Let's put the following scenario: we have our /home/iamgroot/Sync folder as destination, but we want backups to be at /home/iamgroot/backups/ then we can use this folder to store all our backups. Let's check it out

iamgroot@laptop:~$ rsync -bavh --backup-dir='/home/iamgroot/backups/'  ~/Users/  ~/Sync
sending incremental file list
./
file1.txt

sent 145 bytes  received 38 bytes  366.00 bytes/sec
total size is 17  speedup is 0.09

iamgroot@laptop:~$ ls backups/
file1.txt
Enter fullscreen mode Exit fullscreen mode

Let me point out something. If we use ~/backups/ instead of /home/iamgroot/backups/ it will create a folder inside our destination and it will be called ~/backups. this means it takes the argument literally.

Getting back to what we did, you can see it worked perfectly, but we can make it even better by using the --suffix=SUFFIX option to add something at the end of our files, something like file1.txt.backup.

iamgroot@laptop:~$ rsync -bavh --backup-dir='/home/iamgroot/backups/' --suffix='.bak' ~/Users/  ~/Sync
sending incremental file list
./
file1.txt

sent 157 bytes  received 38 bytes  390.00 bytes/sec
total size is 29  speedup is 0.15

iamgroot@laptop:~$ ls -a ~/backups
 .   ..   file1.txt  file1.txt.bak
Enter fullscreen mode Exit fullscreen mode

As you can see we have both files the one we backup earlier ended in .txt and the one we used with a suffix .backup

Cool right!!


Ok, that'll be for today's example, thanks for reading!!!

Follow, ❤ or 🦄

Top comments (0)