Score:0

Rsync : Preserve date after copying

cn flag

I have an issue with files attributes since I changed my company's file system from an old ubuntu 12 to a Synology NAS.

I initially mounted NFS shares on my old file system, then with cp I copied everything, everything was ok But i forgot to preserve the attributes, and we lost especially the date, the dates became the copy date, and not the real date.

I found the rsync command rsync -vrt --size-only /src /dest But some files have been modified since the copy, and the command overwrite them...

For example i tried to create a file on the old file system, copying it on the new synology then modifiyng the file on synology. When i use rsync, the file is erased and replaced by an old version.

I am looking for a command, to copy timestamps for files only when the file have exactly the same size (haven't been modified)

No mater if it takes long time

Edit : I tried the proposed solution with a test directory, no error but no any changes:

old_base=/home/samba/shares/projects/test      
new_base=/mnt/NAS  
cp $old_base $new_base -rv
        `/home/samba/shares/projects/test/1201.txt' -> `/mnt/NAS/test/1201.txt'
        `/home/samba/shares/projects/test/test.txt' -> `/mnt/NAS/test/test.txt'
        `/home/samba/shares/projects/test/1200.txt' -> `/mnt/NAS/test/1200.txt'
        `/home/samba/shares/projects/test/1202/1202.txt' -> `/mnt/NAS/test/1202/1202.txt'

Now my new base is the same with the old, but no timestamp like the reality I modify one file, adding content to simulate a file edited since i copied everything Then i apply your solution :

    root@xx:/mnt/NAS/test# cd $old_base; find . -type f | xargs ls -s | sort -k 2,2 >/tmp/old
    root@xx:/home/samba/shares/projects/test# cd $new_base; find . -type f | xargs ls -s | sort -k 2,2 >/tmp/new
    root@xx:/mnt/NAS# comm -12 /tmp/old /tmp/new | while read size filename; do touch ${new_base}/${filename} -r ${old_base}/${filename}; done
    comm: file 2 is not in sorted order
    comm: file 1 is not in sorted order

I have no error, but no any changes (i am not sure what do you mean by ${filename} but i also tried to replace it by ${new_base}/*

Edit : 22/07

I have no any result when i use

comm -12 --nocheck-order /tmp/old /tmp/new
cn flag
comm isn't working since it's looking for the files to be edited by the first field. You *may* be able to just use the --nocheck-order flag to comm. Or see additional verbiage in my answer.
Score:0
cn flag

Outline of how I'd proceed: List the two hierarchies into a file with size. Sort, and compare the size files with "comm -12" to give a list of file names that have the exact same size. You can then either generate a list of timestamps and compare, or just feed that list into a "touch -r" command to duplicate the timestamps. Please don't blindly cut'n'paste these commands -- make sure you understand what they're trying to do.

Unless you've got a huge number of files, this shouldn't take that long.

Some simple (untested) commands that should help:

old_base = base directory of hierarchy with old files, desired timestamps

new_base = base directory of hierarchy with new files, improper timestamps

cd $old_base; find . -type f | xargs ls -s | while read size filename; do echo "${filename} ${size}"; done | sort >/tmp/old_list

cd $new_base; find . -type f | xargs ls -s | while read size filename; do echo "${filename} ${size}"; done | sort >/tmp/new_list

Try just doing comm -12 /tmp/old_list /tmp/new_list and looking at the output. If it's not filenames that need timestamps changed, we need to figure out why not.

comm -12 /tmp/old_list /tmp/new_list | while read size filename; do touch ${new_base}/${filename} -r ${old_base}/${filename}; done

Mmmax avatar
cn flag
I do not understand how, because i have millions of files, 6TB What is new_base ? The nfs synology share?
cn flag
I'll edit my post to provide more context.
Mmmax avatar
cn flag
I edited my post with the result of my try
cn flag
Very good to do a test case first, Mmmax.
Mmmax avatar
cn flag
I added -nocheck-order to the comm command, i have now no any error, no return, but no any change on my test folder
cn flag
@Mmmax. look at my (edited) post. try just the "comm -12 /tmp/old_list /tmp/new_list" and see if you get anything.
Mmmax avatar
cn flag
No result with this command (initial post edited)
cn flag
@Mmmax Please try to look at what my suggestions mean and are trying to do. Are the two lists coming out correctly? Does comm report any errors? You posed an interesting problem, and I'd like to help, but you have to give me more to work with to help you.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.