Related to this question
Short description of the problem:
When source tree has a mounted point inside it, then time stamps on files inside that mounted point when copied to target tree are not preserved even when using -a option
Detailed description:
Assume this is the source tree:
/home/ /home/
| |
me/ BACKUP/
| |
+----+----------+ +----+-------+
| | | | | |
data/ foo.txt boo.txt data/ foo.txt boo.txt
| |
a.txt a.txt
where data/ above is mounted external USB disk. Everything is ext4 file system. Everything in source is owned my me.
BACKUP also happened to be a mount point, the backup USB disk.
After issuing this command rsync -av --delete /home/me/ /home/BACKUP/, I found that /home/BACKUP/data/ and everything below it has the current time stamp, as if these files were created now, and not the time stamp on the files in /home/me/data/. Other files and folders outside data did have the time stamp preserved OK.
Question is: How to use rsync in the above setting to tell it to preserve time stamps on all files and folders even on files and folders on a mounted point?
I am using:
>uname -a Linux 3.5.0-17-generic #28-Ubuntu SMP x86_64 x86_64 x86_64 GNU/Linux >rsync -v rsync version 3.0.9 protocol version 30
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
from man rsync:
-t, --times preserve modification times
Since you are copying files from one filesystem to another and wanting to preserve c-time. Most people understand c-time to mean “create time” which is incorrect on most UNIX/Linux systems (Windows filesystems track “creation” or “birth” times).
For the most part, in UNIX and Linux, c-time is the timestamp used to record the last inode ‘C‘hange. An inode changes if any of its attributes are updated:
- creation (OP’s case)
- mode (permissions)
- owner/group
- hard link count
- etc. (stat() system call)
OP cannot preserve the c-time of their file’s when they are brought onto a new filesystem. The creation of these files in the new filesystems is one of the conditions listed above (creation of inode/file).
Method 2
as hr3miller already said, -a (or –archive) is equal to -rlptgoD and already includes syncing time.
However when rsync copies data to, eg. an NFS/FAT32/NTFS mount where preserving user and owner fails, rsync won’t try to set the time. Rsync will warn with something like
rsync: chown "/mnt/backup/postgres/hourly.0/primary/var" failed: Operation not permitted (1)
Therefore leave out preserving user and group by using
-rlptD
instead of
-rlptgoD
Only use this when not preserving the owner and group is an option for you.
Note that preserving symlinks and other features could trigger that behavior, too. You will have to go through the man page for every rsync feature (-r -l -p -t -g -o -D) you want to backup.
Method 3
You can use the touch tool for that. The -r argument will copy the timestamps from one file / folder to another.
cd srcParentDir
find srcDir -exec touch -r {} /target/dir/{} ;
Or for your example:
cd /home/me
find . -exec touch -r {} /home/BACKUP/{} ;
which will execute the following statements:
touch -r ./data/a.txt /home/BACKUP/./data/a.txt touch -r ./foo.txt /home/BACKUP/./foo.txt touch -r ./boo.txt /home/BACKUP/./boo.txt
And thus basically copy the old timestamps to the new filesystem.
If you have spaces in the destination path you have to escape them /home/my spaced path/. I think {} will do the escaping for you, though I am not 100% sure.
Method 4
I use rsync -az and sure thing it preserves me the modification time. I double-checked it right now.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0