5

cmp(1) will of course tell me if the contents of two files are identical, but in order to test restoring from backups I also wanted to compare the relevant (!) file attributes.

So if I did something simple like

mv foo.bar foo.bar.save
deja-dup --restore foo.bar # or some other backup tool

how do I compare the attributes of foo.bar and foo.bar.save and test for sufficient equality in a shell script (or similar). I can do

stat foo.bar{,.save}

and manually inspect the output remembering to ignore inode, atime, and ctime (as well as link count, for some reason?), but this is error prone. Is there a cmp-with-attributes tool somewhere which include SELinux and other attributes? Must work on Fedora and ext4 file systems; ideally on "all" systems. Do I need to hack something up in perl?

(No point having backups if you don't test that they work right.)

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • 1
    `stat` has two options, `-c` or `--format`, and `--printf`, that you can use to make it print the data you are interested in, in the format you want, so you can later compare it with `diff` or another tool. – angus Aug 29 '12 at 09:03

1 Answers1

7
getallattr() {
  [ -L "$1" ] || lsattr -d -- "$1" | sed 's/ .*//;q'
  getfattr -hdm- -- "$1" | tail -n +2 | sort
  stat -c '%u %g %a %s %x %y' -- "$1"
}

To retrieve all the attributes (at least those that can easily be restored). It assumes the GNU implementation of stat or compatible, not the stat builtin of zsh. You could also include some form of digest/checksum of the contents (like with sha1sum/b2sum...) in there.

And then do

diff <(getallattr file1) <(getallattr file2)

(ksh, zsh, bash syntax).

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501