22

Is there a way to back up and restore file ownership and permissions (the things that can be changed with chown and chmod)?

You can do this in Windows using icacls.

What about access control lists?

4
  • It would help if you stated which distro you are using, as different distros use different package managers. Mar 9, 2015 at 19:59
  • @garethTheRed, does it also depend on the fs being used, or just the distro?
    – leeand00
    Mar 9, 2015 at 20:21
  • 1
    I doubt it would depend on the filesystem. Mar 9, 2015 at 21:33
  • Till now, there's no perfect answer.
    – kittygirl
    Jan 11, 2019 at 9:31

3 Answers 3

38

You can do this with the commands from the acl package (which should be available on all mainstream distributions, but might not be part of the base installation). They back up and restore ACL when ACL are present, but they also work for basic permissions even on systems that don't support ACL.

To back up permissions in the current directory and its subdirectories recursively:

getfacl -R . >permissions.facl

To restore permissions:

setfacl --restore=permissions.facl
15
4

I'm not aware of anything "off the shelf" that would do this. Here's a starter script for you, though, that will handle basic permissions. It does not handle ACLs of any description - but your Question explicitly excludes those. (It will also fail on pathological filenames - those starting with whitespace, or containing non-printable characters.)

Save the permissions

find * -depth -exec stat --format '%a %u %g %n' {} + >/tmp/save-the-list

Restore the permissions

while read PERMS OWNER GROUP FILE
do
    chmod "$PERMS" "$FILE"
    chown "${OWNER}:${GROUP}" "$FILE"
done </tmp/save-the-list
3
  • do you mean ACL will also fail on pathological filenames?
    – kittygirl
    Oct 2, 2018 at 16:07
  • @kittygirl I didn't include any processing of ACLs in the scriptlet because the OP had explicitly excluded them from the requirements. You can add what you like, bearing in mind that the code isn't particularly robust (see the comment describing pathological filenames). Oct 2, 2018 at 17:29
  • I found a problem:cannot find .htaccess,gitignore...
    – kittygirl
    Jan 10, 2019 at 14:20
0
#!/bin/bash
# prepare files
home="/home/exchange"
cd $home
>acl
echo "#!/bin/bash">recovery_acl.sh
echo "cd $home">>recovery_acl.sh
f='./'
# create acl file sorted by dir_level
for i in `seq 0 15`;do
  find . -mindepth $i -maxdepth $i -type d -exec getfacl {} +|grep -E '*UTS|file:'>>acl
done
sed -i 's/default\:user/\-dm\ u/g' acl
sed -i 's/default\:group/\-dm\ g/g' acl
sed -i 's/user/\-m\ u/g' acl
sed -i 's/group/\-m\ g/g' acl
sed -i 's/\#\ file\:\ /\.\//g' acl
sed -i 's,\\,\\\\,g' acl

while IFS='' read -r line ; do
  # grep dir name
  if echo "$line" | grep -q "$f" ; then
    dir="$line"
    continue
  fi
  echo setfacl $line '"'$dir'"'>>recovery_acl.sh
  # grep non def acl (for files)
  if echo "$line" | grep -q '\-m' ; then
    echo setfacl $line '"'$dir'"'/*>>recovery_acl.sh
  fi
done < "acl"

sed -i "s/\\\134/\\\\\\\134/g" recovery_acl.sh
sed -i "s/\\\040/\\\\ /g" recovery_acl.sh

This bash script get acl only dirs (in my case files acls = dir(parent) acl ) After execution of script, will creating another "recovery_acl.sh".

While recovering Errors like "No such file or directory" means that dir is empty or dirname has two/more spaces together.

You must log in to answer this question.

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