LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Red Hat (https://www.linuxquestions.org/questions/red-hat-31/)
-   -   How to preserve all timestamps while copying files (https://www.linuxquestions.org/questions/red-hat-31/how-to-preserve-all-timestamps-while-copying-files-742757/)

megalosaurus 07-25-2009 01:22 PM

How to preserve all timestamps while copying files
 
I need to copy a directory from one place to another. I want all the files, both in the source and the destination, to have the Access, Modify, and Change timestamps they had before I began the copy. I've found that cp, rsync, and tar each offer options that preserve some of the timestamps, but I can't find a way to preserve all of them. Doing a disk image copy is not an option. The directory has 3 million files spread out over tens of thousands of subdirectories going 20 levels deep, so anything that handles files individually is not practical. Does anyone know of a solution?

jhwilliams 07-25-2009 01:34 PM

Quote:

Originally Posted by megalosaurus (Post 3620072)
Doing a disk image copy is not an option. The directory has 3 million files spread out over tens of thousands of subdirectories going 20 levels deep, so anything that handles files individually is not practical.

So this is a separate issue. Your post seems more to be about just how to mirror data or something right? So, there are only a few layers in the storage stack to begin with, and you need to chose where you'll copy: block level? volume? filesystem? by file?

rsync -a sounds to do what you want.

If that looks like it'll be too slow, then I'd say just dd the source device to the target device. why is that not an option?

megalosaurus 07-25-2009 02:47 PM

Both directories are about 200 GB within a multi-terabyte filesystem, so dd won't work.

I've tried rsync -a. It leaves the timestamps on the source files unchanged, but updates the Access and Change timestamps on the destination.

colucix 07-25-2009 02:48 PM

You can preserve the access and modify time, but not the change time, since whenever you copy a file a new inode is created.

megalosaurus 07-26-2009 05:09 PM

Quote:

Originally Posted by colucix (Post 3620142)
You can preserve the access and modify time, but not the change time, since whenever you copy a file a new inode is created.

OK - I guess I could live with that if that's the way it has to be. But it also changes the access timestamp at the destination. If I use cp -p, it won't change the access timestamp at the destination, but it changes it at the source. Seems like you just can't win.

colucix 07-27-2009 12:24 AM

Quote:

Originally Posted by megalosaurus (Post 3621056)
But it also changes the access timestamp at the destination. If I use cp -p, it won't change the access timestamp at the destination, but it changes it at the source. Seems like you just can't win.

It looks like cpio does the trick:
Code:

<commands> | cpio -p --reset-access-time --preserve-modification-time /path/to/dest/dir
the list of files to copy has to be created and passed as standard input.

jschiwal 07-27-2009 01:47 AM

Using tar to replicate preserves the mtime and atime:
Code:

> tar -C work/ -cf - home.png | ssh hpmedia 'tar -C rsynctest -xvf - home.png'
home.png
ssh hpmedia stat rsynctest/home.png
  File: `rsynctest/home.png'
  Size: 4624            Blocks: 24        IO Block: 4096  regular file
Device: 831h/2097d      Inode: 7676168    Links: 1
Access: (0640/-rw-r-----)  Uid: ( 1000/jschiwal)  Gid: ( 1000/jschiwal)
Access: 2009-07-27 01:44:21.000000000 -0500
Modify: 2009-07-27 01:41:42.000000000 -0500
Change: 2009-07-27 01:44:21.000000000 -0500
jschiwal@qosmio:~> stat work/home.png
  File: `work/home.png'
  Size: 4624            Blocks: 16        IO Block: 4096  regular file
Device: 807h/2055d      Inode: 1114126    Links: 1
Access: (0640/-rw-r-----)  Uid: ( 1000/jschiwal)  Gid: ( 1000/jschiwal)
Access: 2009-07-27 01:44:21.000000000 -0500
Modify: 2009-07-27 01:41:42.000000000 -0500
Change: 2009-07-27 01:41:42.000000000 -0500


megalosaurus 07-31-2009 10:34 AM

Quote:

Originally Posted by jschiwal (Post 3621328)
Using tar to replicate preserves the mtime and atime:

It's copying the atime from the source to the destination, but it's still updating the atime at the source before it begins.

jschiwal 07-31-2009 10:36 AM

You are right, I should have included the --atime-preserve option.

megalosaurus 07-31-2009 10:56 AM

Quote:

Originally Posted by colucix (Post 3621270)
It looks like cpio does the trick:
Code:

<commands> | cpio -p --reset-access-time --preserve-modification-time /path/to/dest/dir
the list of files to copy has to be created and passed as standard input.

cpio changes the ctime at the source and destination.

colucix 07-31-2009 11:11 AM

Just out of curiosity, why are you so concerned about change time? It is related to the inode and it is transparent to many commands, which on the contrary refer to the modification and/or the access time to work properly.

Doctor_Pi 07-11-2015 03:57 PM

Dump and Restore?
 
I believe Dump and Restore will preserve all timestamps.

Ivan.

voleg 07-13-2015 05:19 AM

"rsync -a" too.


All times are GMT -5. The time now is 07:18 AM.