How to save and restore file's created/modified dates?

5

7

I've copied a bunch of files from one server to the other, and now the files' dates are reset to current.

How to backup files' dates on old server and restore the them on the new one (without re-transferring all files)?

Vi.

Posted 2012-12-02T19:39:48.113

Reputation: 13 705

If you version of cp (or scp) has the -p or the --preserve option, you should have used it! Don't forget it next time... – gniourf_gniourf – 2012-12-02T21:31:12.980

I used find ... | cpio ... | mcrypt ... | pv ... | nc -lp 1 – Vi. – 2012-12-03T00:16:07.613

Answers

7

Here are scripts to save and restore all {c,n,a}times of files and directories:

Save:

find / -mount -print0 | perl -ne 'INIT{ $/ = "\0"; use File::stat;} chomp; my $s = stat($_); next unless $s; print $s->ctime . "/" . $s->mtime . "/" . $s->atime ."/$_\0"; ' > dates.dat

Restore:

cat dates.dat |  perl -ne 'INIT{ $/ = "\0";} chomp; m!^([0-9]+)/([0-9]+)/([0-9]+)/(.*)!s or next; my ($ct, $mt, $at, $f) = ($1, $2, $3, $4); utime $at, $mt, $f;'

It does not set ctime (inote-change time) although.

Vi.

Posted 2012-12-02T19:39:48.113

Reputation: 13 705

2

You can use stat to get the dates on the source and touch to modify them on the target.

MaQleod

Posted 2012-12-02T19:39:48.113

Reputation: 12 560

How to do it automatically for a bunch of files (including ones with special names)? I don't want to stat and touch each file manually. – Vi. – 2012-12-02T21:04:50.943

You'd have to use a script. I'd do something with find, piped into a while statement, then parsing the output of stat for each file and applying with touch to the source. The actual implementation will depend on the particular file structure involved. – MaQleod – 2012-12-02T21:34:12.733

OK, implementing the script myself (I thought there should be a tool fo this or someone already having such script). – Vi. – 2012-12-03T00:12:46.720

2

If file names are not too weird, and I only need to restore mtime, I use this quick & dirty solution:

find . -type f -exec stat -c 'touch --no-create -d "%y" "%n"' {} \;

This creates a script on the source, and that script can be run on the destination to restore the mtime timestamps.

neingeist

Posted 2012-12-02T19:39:48.113

Reputation: 21

A probably faster alternative is find . -type f -printf 'touch --no-create -d "%t" "%p"\n' because it doesn't fork. But it still needs some improvement (stable time format) – Daniel Alder – 2014-07-09T19:51:32.400

I've been using find . -type f -exec stat -c 'touch --no-create -d @%Y "%n"' {} \; >restore-timestamps (timestamps instead of local date representation). – rdesgroppes – 2019-08-23T13:48:59.307

2

I have a Python script for doing this at https://github.com/robertknight/mandrawer/blob/master/save-file-attrs.py

On the original server run:

save-file-attrs.py save scp .saved-file-attrs <user>@<dest-server>:<path>

On the destination server run:

cd <path> save-file-attrs.py restore

This will restore the file attributes.

Robert Knight

Posted 2012-12-02T19:39:48.113

Reputation: 121