Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use rsync for backup without overwrite

I know about --ignore-existing option and wonder if there's something slightly different.

My situation is: I have a webserver with large amount of user-uploaded pictures. It's about 2TB now. Usually these files are not modified at all. I use rsync and backup them to other server.

But what if some file becomes broken? Suppose, a half of a file disappeared. It will copied to backup and overwrites previous correct file, so backup becomes broken as well.

I may use --ignore-existing. But what if some file was still changed (that's very rarely but possible). With this option backup will not have this new file.

I would like to have all versions of a file on backup if it changes. Is it possible? That is: if rsync defines that file already exists, it moves existing old file to some ../previous-TIMESTAMP/oldFileHere and after that puts the new file on the place of an old one.

Is there a way to achieve this?

like image 293
Kasheftin Avatar asked Jul 01 '16 13:07

Kasheftin


People also ask

Does rsync overwrite existing files?

Files that do not exist on the remote-host are copied. Files that have been updated will be synced, rsync will copy only the changed parts of files to the remote host. File that is exactly the same are not copied to the remote host at all.

Does rsync overwrite files with same name?

It doesn't care which file is newer, if it is different, it gets overwritten. You can pass the '--update' flag to rsync which will cause it to skip files on the destination if they are newer than the file on the source, but only so long as they are the same type of file.

Why is rsync not a good backup solution?

If your system is being actively accessed by some other systems on the network, it is not a better solution. Because, the contents of the systems might be constantly updated every minute, and some files may change during the rsync process.

Is rsync good for backups?

The rsync command-line tool is the most preferred backup tool in Linux systems for multiple reasons. It allows you to make incremental backups including the entire directory tree, both locally and on a remote server. Better yet, you can automate the backups using shell scripts and cron jobs.


1 Answers

--backup --suffix=

If you don't want to use rsnapshot you can use destination files with suffixes like this:

rsync --backup --suffix=`date +'.%F_%H-%M'` ~/life_story.txt /media/myusername/backupdrive/life_story.txt

Note:

  • The copies will be "smart" - if the source and the destination are the same, no new timestamped backup will be generated (which is good)
  • There's an option to use a subdirectory instead of a file suffix (--backup-dir) but I haven't tried that myself.
like image 146
Sridhar Sarnobat Avatar answered Oct 03 '22 20:10

Sridhar Sarnobat