3

I made a very simple backup script using rsync that backs up some folders to an USB stick. This is just for quick daily backups.

The command is

rsync -av --delete

followed by --exclude options and the paths.

But now it occurred to me that this is possibly not safe:
What happens if my hard drive gets corrupted, and some files can not be read anymore? Will rsync delete the files on the stick if it can't read the source?
Or more generally: Is there any scenario other than "me deleting the files on my hard drive" that will lead to deletion of the files on the USB stick?

alain
  • 215

2 Answers2

3

First of all, --delete option can lead to some unwanted destructive results if you are unaware of the background. For example, if the source becomes empty then all the existing files at the destination will be removed or if some files are removed from source then those files will be removed from destination too.

What happens if my hard drive gets corrupted, and some files can not be read anymore?

If rsync can not read the source, it would show a permission denied message and won't do anything further. So your existing files at the destination will be safe.

Is there any scenario other than "me deleting the files on my hard drive" that will lead to deletion of the files on the USB stick?

Unless your USB itself gets corrupted, no, there is no scenario where (you don't remove files from source, the hard drive and) the existing files on the USB will get deleted by rsync with --delete option.

Test:

% ls -1 source dest
dest:

source:
foo

% rsync -av --delete source/ dest
sending incremental file list
./
foo

sent 143 bytes  received 38 bytes  362.00 bytes/sec
total size is 16  speedup is 0.09

% ls -1 source dest              
dest:
foo

source:
foo

% chmod 000 source 

% rsync -av --delete source/ dest
sending incremental file list
rsync: change_dir "/source" failed: Permission denied (13)

sent 20 bytes  received 12 bytes  64.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1183) [sender=3.1.0]

% ls -1 source dest              
dest:
foo
ls: cannot open directory source: Permission denied
heemayl
  • 93,925
1

An easy solution to corruption and deletion concerns is to employ a proper backup procedure. Rotate a series of three (or more) USB thumbdrives, using the eldest 'backup' as the plugged in stick when you run this command once every week. This way if you catch the corruption before you cycle through your USB drives, you can recover from it. This is known as FIFO (First In, First Out).

There are other, more complicated schemes out there, all of which are compatible with your backup method. A primer on some of these is available on Wikipedia at the following link:

https://en.wikipedia.org/wiki/Backup_rotation_scheme
Aren
  • 483