65

I have an rsync backup script I run, which also restores files back where they came from when I ask. But if the files at the destination are newer than those in the backup when I try to restore, it will not replace them. I really want to replace the newer files with those in the backup but I don't see a way to make rsync do this.

tldr: is there a way to force rsync to overwrite files at the destination?

edit: I've been running rsync -avhp When I want to restore a backup, I use the same command with the "to" and "from" swapped. So it tries to copy files from the backup drive to the place on my computer they belong.

5
  • 23
    rsync will always overwrite files at the destination unless you tell it not to do this (option -u, --update). Check your command line arguments.
    – scai
    Jun 17, 2013 at 12:12
  • Can you share your actual rsync command? Rsync will overwrite older files with newer ones, so you're most likely comparing files by just timestamp, and the dates are slightly out of whack between the 2 computers.
    – slm
    Jun 17, 2013 at 12:51
  • I've tried using touch to make the files I want to replace "older" than my backup, but it still doesn't replace the existing ones in my home folder.
    – jedipixel
    Jun 17, 2013 at 15:34
  • Maybe '-I --modify-window=999999999' works for you?
    – jelle foks
    Jun 19, 2013 at 23:43
  • FYI -a contains the -p. Manual says: --archive, -a archive mode is -rlptgoD (no -A,-X,-U,-N,-H)
    – Ray
    Jun 12 at 15:16

1 Answer 1

68

The manual says:

-I, --ignore-times   don't skip files that match size and time

With more details:

-I, --ignore-times

Normally rsync will skip any files that are already the same size and have the same modification timestamp. This option turns off this "quick check" behavior, causing all files to be updated.

That could be also useful (thanks Attila to pointing this out):

--checksum, -c   skip based on checksum, not mod-time & size

This changes the way rsync checks if the files have been changed and are in need of a transfer. Without this option, rsync uses a "quick check" that (by default) checks if each file's size and time of last modification match between the sender and receiver. This option changes this to compare a 128-bit checksum for each file that has a matching size. Generating the checksums means that both sides will expend a lot of disk I/O reading all the data in the files in the transfer, so this can slow things down significantly (and this is prior to any reading that will be done to transfer changed files)

The sending side generates its checksums while it is doing the filesystem scan that builds the list of the available files. The receiver generates its checksums when it is scanning for changed files, and will checksum any file that has the same size as the corresponding sender's file: files with either a changed size or a changed checksum are selected for transfer.

Note that rsync always verifies that each transferred file was correctly reconstructed on the receiving side by checking a whole-file checksum that is generated as the file is transferred, but that automatic after-the-transfer verification has nothing to do with this option's before-the-transfer "Does this file need to be updated?" check.

The checksum used is auto-negotiated between the client and the server, but can be overridden using either the --checksum-choice (--cc) option or an environment variable that is discussed in that option's section.

Worth to mention the --delete flag if you wanted to remove the destination files which are doesn't exists in the source directory.

Another useful option is the -n which just mimics what would rsync do without any actual copying or deleting. Only listing. Very useful when you not 100% sure what you do.

3
  • 1
    what about just size? skip files that match exactly, but replace those if there is a diff ... regardless if the target one is newer
    – mjs
    Nov 21, 2019 at 10:45
  • 4
    @mmm That sounds like the --size-only option. From the rsync man: "This modifies rsync's lqquick checkrq algorithm for finding files that need to be transferred, changing it from the default of transferring files with either a changed size or a changed lastmodified time to just looking for files that have changed in size. This is useful when starting to use rsync after using another mirroring system which may not preserve timestamps exactly." Jul 6, 2020 at 16:00
  • 2
    Also check out the --checksum option: "This changes the way rsync checks if the files have been changed and are in need of transfer." Be aware of I/O performance overhead. Apr 23, 2021 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.