I had at one point copied a large number of files between drives and did not use the -p and thus the timestamps were all set to the date of the copy.
I did not catch this, and deleted the source. So I 'lived' with it and have since changed many files.
Well, yesterday I found a good backup of many of those files and I want to restore them to their proper dates.
cp -p -u is exactly the opposite of what I want. I want to copy only if the source files have an earlier date than the destination files.
The source files are just an old copy on another drive that I found when cleaning up things...
Robert Moskowitz spake the following on 8/6/2007 2:40 PM:
I had at one point copied a large number of files between drives and did not use the -p and thus the timestamps were all set to the date of the copy.
I did not catch this, and deleted the source. So I 'lived' with it and have since changed many files.
Well, yesterday I found a good backup of many of those files and I want to restore them to their proper dates.
cp -p -u is exactly the opposite of what I want. I want to copy only if the source files have an earlier date than the destination files.
The source files are just an old copy on another drive that I found when cleaning up things...
Can you restore the backups, and then cp -u from the existing directory over the restored copy?
Scott Silva wrote:
Robert Moskowitz spake the following on 8/6/2007 2:40 PM:
I had at one point copied a large number of files between drives and did not use the -p and thus the timestamps were all set to the date of the copy.
I did not catch this, and deleted the source. So I 'lived' with it and have since changed many files.
Well, yesterday I found a good backup of many of those files and I want to restore them to their proper dates.
cp -p -u is exactly the opposite of what I want. I want to copy only if the source files have an earlier date than the destination files.
The source files are just an old copy on another drive that I found when cleaning up things...
Can you restore the backups, and then cp -u from the existing directory over the restored copy?
No. Because all the files, changed or not since that date, are newer than what is on the backup. So it would overwrite everything.
Robert Moskowitz wrote:
Scott Silva wrote:
Robert Moskowitz spake the following on 8/6/2007 2:40 PM:
I had at one point copied a large number of files between drives and did not use the -p and thus the timestamps were all set to the date of the copy.
I did not catch this, and deleted the source. So I 'lived' with it and have since changed many files.
Well, yesterday I found a good backup of many of those files and I want to restore them to their proper dates.
cp -p -u is exactly the opposite of what I want. I want to copy only if the source files have an earlier date than the destination files.
The source files are just an old copy on another drive that I found when cleaning up things...
Can you restore the backups, and then cp -u from the existing directory over the restored copy?
No. Because all the files, changed or not since that date, are newer than what is on the backup. So it would overwrite everything.
Maybe you could iterate through the file directories on your good backup and do a "touch -r" on the current set using the timestamps from the good backup:
foreach file in backup_dir: if file exists in current_dir: touch -r backup_dir/file current_dir/file
or something like that...
-Greg
Robert Moskowitz spake the following on 8/6/2007 4:22 PM:
Scott Silva wrote:
Robert Moskowitz spake the following on 8/6/2007 2:40 PM:
I had at one point copied a large number of files between drives and did not use the -p and thus the timestamps were all set to the date of the copy.
I did not catch this, and deleted the source. So I 'lived' with it and have since changed many files.
Well, yesterday I found a good backup of many of those files and I want to restore them to their proper dates.
cp -p -u is exactly the opposite of what I want. I want to copy only if the source files have an earlier date than the destination files.
The source files are just an old copy on another drive that I found when cleaning up things...
Can you restore the backups, and then cp -u from the existing directory over the restored copy?
No. Because all the files, changed or not since that date, are newer than what is on the backup. So it would overwrite everything.
So all the files on the share that haven't changed are all roughly the same date/time? Maybe you could copy -p the files to a new location, and rm all the files that have that date/time with a "find /newlocation -mtime -(rough days to common time) -exec rm -f {} ;" and then merge the backup with this new directory.
Robert Moskowitz wrote:
I had at one point copied a large number of files between drives and did not use the -p and thus the timestamps were all set to the date of the copy.
I did not catch this, and deleted the source. So I 'lived' with it and have since changed many files.
Well, yesterday I found a good backup of many of those files and I want to restore them to their proper dates.
cp -p -u is exactly the opposite of what I want. I want to copy only if the source files have an earlier date than the destination files.
I don't think that's what you want to do. The files from the backup will also have an earlier date than the files you subsequently changed. I believe what you want is more like this:
cd /backup/directory for F in * do cmp "$F" "/new/directory/$F" && touch -r "$F" "/new/directory/$F" done
That will compare all the files and adjust the timestamp on the files that are still the same as those on the backup. Note that, despite the quoting, the script will break if any of the filenames have embedded whitespace because the "for F in *" will be parsed incorrectly.
On Mon, Aug 06, 2007 at 06:27:20PM -0500, Robert Nichols wrote:
for F in * do cmp "$F" "/new/directory/$F" && touch -r "$F" "/new/directory/$F" done
That will compare all the files and adjust the timestamp on the files that are still the same as those on the backup. Note that, despite the quoting, the script will break if any of the filenames have embedded whitespace because the "for F in *" will be parsed incorrectly.
Not in a sane shell. Heh, not even in bash :-)
bash-2.05b$ touch "a b" bash-2.05b$ touch c bash-2.05b$ touch d bash-2.05b$ ls a b c d bash-2.05b$ for a in *
do echo Result is: $a done
Result is: a b Result is: c Result is: d
Stephen Harris wrote:
On Mon, Aug 06, 2007 at 06:27:20PM -0500, Robert Nichols wrote:
for F in * do cmp "$F" "/new/directory/$F" && touch -r "$F" "/new/directory/$F" done
That will compare all the files and adjust the timestamp on the files that are still the same as those on the backup. Note that, despite the quoting, the script will break if any of the filenames have embedded whitespace because the "for F in *" will be parsed incorrectly.
Not in a sane shell. Heh, not even in bash :-)
bash-2.05b$ touch "a b" bash-2.05b$ touch c bash-2.05b$ touch d bash-2.05b$ ls a b c d bash-2.05b$ for a in *
do echo Result is: $a done
Result is: a b Result is: c Result is: d
OK. Color me paranoid, or maybe "once burned, twice shy."
On Mon, 2007-08-06 at 17:40 -0400, Robert Moskowitz wrote:
I had at one point copied a large number of files between drives and did not use the -p and thus the timestamps were all set to the date of the copy.
I did not catch this, and deleted the source. So I 'lived' with it and have since changed many files.
Well, yesterday I found a good backup of many of those files and I want to restore them to their proper dates.
cp -p -u is exactly the opposite of what I want. I want to copy only if the source files have an earlier date than the destination files.
I suggest "man find" and focus on the (new to me) versions of "newer". I think, IIUC, that one of those tests will meet your needs. When the test passes, an appropriate "exec ..." may do what you want.
The source files are just an old copy on another drive that I found when cleaning up things...
<snip>
HTH -- Bill
On Mon, 2007-08-06 at 17:40 -0400, Robert Moskowitz wrote:
<snip>
P.S.
I make (the possibly invalid assumption) that the original directory timestamp is a valid comparator to use in seeing if the file(s) have been modified. For you case, if this is valid, the negation of the test might be needed.
-- Bill
CentOS mailing list centos@centos.org writes:
I had at one point copied a large number of files between drives and did not use the -p and thus the timestamps were all set to the date of the copy.
I did not catch this, and deleted the source. So I 'lived' with it and have since changed many files.
Well, yesterday I found a good backup of many of those files and I want to restore them to their proper dates.
cp -p -u is exactly the opposite of what I want. I want to copy only if the source files have an earlier date than the destination files.
The source files are just an old copy on another drive that I found when cleaning up things...
SRC=/my/restored/files DST=/the/working/tree export SRC DST find "$SRC" -type f -print | while read A; do [ "${A}" -ot "${DST}${A##$SRC}" ] && /bin/touch -r ${A} "${DST}${A##$SRC}"; done
Note that using this syntax, the two variables cannot be set in-place on the same command line as "find ..."; it only seems to work for me if they're exported as environment variables.
A more expanded version of the same, with commentary:
cat > fix_timestamps.sh << __EOF__ #!/bin/sh ### Scans a source directory for files, setting the dates of same-named files in a target directory ### Adam Thompson athompson@sjsd.net 2007-Aug-07 # Source directory - no need for trailing slash SRC=/my/restored/files # Target directory - no need for trailing slash DST=/the/working/tree # Locate all _files_ only find "$SRC" -type f -print | ( # subshell not necessary, only here for readability # read each line of input from the pipe into $A while read A; do # if's can be shortened to && most times # Proceed only if source file is OlderThan target file if [ "${A}" -ot "${DST}${A##$SRC}" ]; then # Proceed only if target file exists # Probably being paranoid, since -ot should have already failed... if [ -f "${DST}${A##$SRC}" ]; then # Use "touch" to reset the timestamp on the file \ # instead of re-copying the data # "-r" == "--reference", uses the source file's \ # timestamp to set the destination file's timestamp /bin/touch -r ${A} "${DST}${A##$SRC}" fi fi done ) __EOF__
Obviously the one-liner above is going to be marginally faster, especially on slower CPUs, but the difference should be minimal - bash isn't that much of a CPU hog.
-Adam Thompson Divisional IT Department, St. James-Assiniboia School Division 150 Moray St., Winnipeg, MB, R3J 3A2 athompson@sjsd.net / tel: (204) 837-5886 x222 / fax: (204) 885-3178