0

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
Mmmax
  • 1
  • 1
  • 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. – mpez0 Jul 21 '21 at 14:20

1 Answers1

0

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

mpez0
  • 1,492
  • 9
  • 9
  • I do not understand how, because i have millions of files, 6TB What is new_base ? The nfs synology share? – Mmmax Jul 21 '21 at 08:18
  • I'll edit my post to provide more context. – mpez0 Jul 21 '21 at 12:32
  • I edited my post with the result of my try – Mmmax Jul 21 '21 at 13:53
  • Very good to do a test case first, Mmmax. – mpez0 Jul 21 '21 at 14:29
  • I added -nocheck-order to the comm command, i have now no any error, no return, but no any change on my test folder – Mmmax Jul 21 '21 at 14:49
  • @Mmmax. look at my (edited) post. try just the "comm -12 /tmp/old_list /tmp/new_list" and see if you get anything. – mpez0 Jul 21 '21 at 18:17
  • No result with this command (initial post edited) – Mmmax Jul 22 '21 at 13:21
  • @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. – mpez0 Jul 22 '21 at 15:31