12

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?

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
Kasheftin
  • 7,509
  • 11
  • 41
  • 68

2 Answers2

24

--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.
Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
  • 3
    great answer! I modified to `rsync --backup-dir=backup_\`date +%F_%H-%M-%S\` src dst`. This syncs `src` to `dst`, and creates a dated backup directory with copies only of the files that are about to be overwritten in `dst`. The backup directory preserves the subdirectory structure – cxrodgers Nov 01 '21 at 15:57
  • 1
    When using a time-based `--suffix` with a fixed `--backup-dir`, all the backed up versions of a particular file will be in a single directory, which is convenient for finding them. The suffix is added *after* any file extension, so depending on how those files are going to be accessed they might need to be renamed when they are retrieved from backup. (I'm thinking about setting this up for someone who does not use CLI, hence the consideration.) – crantok Aug 29 '23 at 18:57
  • thanks for the info. Feel free to edit my answer. Oh and `date -I` is a lot easier than that custom date format I have above – Sridhar Sarnobat Aug 30 '23 at 19:15
7

sorry this is the wrong answer, see my other answer

I think you want to use rsnapshot rather than rsync. It's built specifically for recovering older versions of files. It provides versioning of files at an hourly, weekly and monthly basis in the directory ~/.snapshots.

If your files are small you might even consider versioning them on a modification-basis rather than time-basis by putting them in a git repo. That's what I do with my databases, dot files and personal notes.

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106