Rsync Backup – Preserve Date Modified on Directories with Rsync

backuprsyncsynchronization

I am trying to backup a failing hard drive and rsync would be ideal due to the features it has such as progress indicator and ability to stop and resume. The one issue I am having is that while file date modified attribute is preserved the directories get new date attribute. This causes issues as I sort many files by date so I know what was added more recently. Is it possible to preserve directory date modified attribute with rsync:

sudo rsync -avhX --progress --info=progress2 /mnt/failing/ /mnt/new/

-t (included with -a) option preserves the file attributes but does not mention directories. Is there any special requirement for ownership / permissions of the /mnt/new partition to preserve certain attributes successfully?

Best Answer

The last modification time of directories is preserved by -a, but you can only see this when rsync finishes. It does not try to set the time on directories that are constantly being updated with new files.

You can test this yourself. Create a directory and set the date on it to yesterday, then copy it with rsync:

$ mkdir d1 d2
$ ls -ld d1
drwxr-xr-x 2  40 Nov  4 14:41 d1
$ touch -d 'yesterday' d1
$ ls -ld d1
drwxr-xr-x 2  40 Nov  3 14:41 d1
$ rsync -i -avR d1 d2
$ ls -ld d1 d2/d1/
drwxr-xr-x 2  40 Nov  3 14:41 d1
drwxr-xr-x 2  40 Nov  3 14:41 d2/d1/

The d2/d1 dir has yesterday's date. We can override it and see if rsync fixes things:

$ touch d2/d1
$ ls -ld d1 d2/d1/
drwxr-xr-x 2  40 Nov  3 14:41 d1
drwxr-xr-x 2  40 Nov  4 14:42 d2/d1/
$ rsync -i -avR d1 d2
.d..t...... d1/
$ ls -ld d1 d2/d1/
drwxr-xr-x 2  40 Nov  3 14:41 d1
drwxr-xr-x 2  40 Nov  3 14:41 d2/d1/

rsync -i shows the timestamp is wrong on d2/d1 and fixes it.

Related Question