-1

I have performed a backup and on restoring it, unfortunately, all file access times were set to the timestamp of the DateTime of this restoring process. How now to set all timestamps from the backup (say /mount/backup/user) with the same folder directory structure into my current system (/home/user), but only for the files I have not changed after the backup? (at the 11/January/2021)

Somethin with touch's reference option?

$ man touch
-r, --reference=FILE
              use this file's times instead of the current time
8
  • You'll need to walk the directory hierarchy (perhaps with find) for older files (perhaps with the -mtime directive) and touch the corresponding backup file (with something like -exec sh -c 'for file; do bkp=${file/#\/home/\/mount\/backup}; touch ...; done' _ ") Commented Feb 20, 2021 at 22:43
  • What backup software are you using? How did you restore the backup? Are the modification timestamps even stored in the backup? What Unix are you using?
    – Kusalananda
    Commented Mar 13, 2021 at 12:52
  • see my answer, where I explained that I was using Backup using Tar as it is explained on the ubuntu website (see my answer below). when restoring individual files or folders (by extracting them), the file times are lost. This is not the case when restoring complete backup
    – camel
    Commented Mar 13, 2021 at 13:03
  • IMHO, essential information like that should be in the question, which would make it possible to actually answer the question. For that we know, from reading the question, you're using a proper backup tool such as restic or borgbackup, not an tape archiving tool or file transfer utility.
    – Kusalananda
    Commented Mar 13, 2021 at 20:52
  • If you're using tar to extract individual files from a tar archive, it's unclear what you do to reset the mtime timestamp (you don't show what command you use). GNU tar extracts the mtime stored in the tar archive and won't reset it unless you use -m.
    – Kusalananda
    Commented Mar 13, 2021 at 21:02

1 Answer 1

0

A complex statement like this seems to work

find . -mtime +225 -exec sh -c 'echo "\n" && echo File = "{}"; bkf="/mount/backup/{}"; echo Backupfile = \""${bkf}"\" . ; test -f "$bkf" && echo do the touch && echo okok ; echo end' \;

where 225 is the days between now and the backup (somehow rounded, reference)

The find's exec is well described here

Edit: If you have your Backup archived using tar/tar.gz, you do not need to extract all files, but can simply cd to the backup and issue following command (reference: list tar contents) to get metadata of all files in the backup:

for f in *.tar.gz ; do echo $f; tar -v -t -f $f > "/tmp/contents_backup-20200401/$f.txt"; done

(dont know why the -z option was not needed even though the archive was compressed using gzip)

This data is in ASCII table format with space delimied. Unfortunately cut cannot operate with variable spacing, but awk can.

The follwing procedure works smoothly (except some ugly file names with dollar or apostroph signs or brackets). This can be partly fixed by using fgrep instead of grep but actually it is also find's output by {} that is stripped when filename contains for instance a dollar sign or apostrophe.

navigate to the folder of the live system, then:

find  *  -type f  -mtime +244   -exec sh -c  'echo "\n" && echo File = "{}"; lastmod=$(stat -c %y "{}" |cut -c-16); echo "LastMod TimeStamp = \""$lastmod"\"" ; dt=$(echo "$lastmod" | cut -c-10) && [ "$dt" = "2020-07-10" ] && ( echo "old file detected" ; timestamp=$(fgrep "{}" /tmp/contents_backup-20200401/backup-musik.tar.gz.txt  | head -n1 | awk -F" " '"'"'{ print $4 " " $5 }'"'"' ); echo "Backup TimeStamp  = \""${timestamp}"\"" . ; test -n "$timestamp" && echo "do the touch" && touch -d "$timestamp" "{}"; echo "ret=$?" && echo okok ) ; echo end' \;

whereby:

  • 244 is the days from the backup till now (as a first filter)
  • 2020-07-10 is the hardcoded date , only files with this date will be affected
  • /tmp/contents_backup-20200401/backup-musik.tar.gz.txt is the file generated by the above mentioned command

What this command does:

  • loop through files in live system
  • check if they are affected by a wrong date issue (it is old and was this not altered after the backup, then you dont want to reset its timestamp)
  • lookup in the backup listing and find the corresponding entry (this does not work for folders* unfortunately since many files would be found which are in the folder)
  • using awk get the timestamp of the backuped file
  • report about success

*) folder datetimes can be set using this command based on the latest file in that folder (actually this needs to be applied to all folders recursively)

stat -c %y . && touch -r "$(find -mindepth 1 -maxdepth 1 -printf '%T+=%p\n' | sort |tail -n 1 | cut -d= -f2-)" . && stat -c %y .

Edit2: escaping (single apostrophe) using correct syntax is sometimes difficult.

Edit3: This command can be executed in any subfolder which might lead to non-unique matches in the backup lookup, so this will be reported as well:

find  *  -type f  -mtime +244   -exec sh -c  'echo "\n" && echo File = "{}"; lastmod=$(stat -c %y "{}" |cut -c-16); echo "LastMod TimeStamp = \""$lastmod"\"" ; dt=$(echo "$lastmod" | cut -c-10) && [ "$dt" = "2020-07-10" ] && ( echo "old file detected" ; filelist=$(fgrep "{}" /tmp/contents_backup-20200401/backup-dokumente.tar.gz.txt); filecount=$(echo "$filelist"|wc -l); echo "file count = $filecount"; [ "$filecount" -gt "1" ] && echo "multiple files found" ; timestamp=$(echo "$filelist" | head -n1 | awk -F" " '"'"'{ print $4 " " $5 }'"'"' ); echo "Backup TimeStamp  = \""${timestamp}"\"" . ; [ $(echo -n "$timestamp" | wc -m) = 16 ] && echo "do the touch" && touch -d "$timestamp" "{}"; echo "ret=$?" && echo okok ) ; echo end' \;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .