Use getfacl and setfacl. Will work even if you dont run your filesystem with “acl” mode. “acl” mount option (Available by installing acl program) gives more permissions than your standard u/g/o r/w/x. But if your using simple posix permissions getfacl and setfacl will still work in that simple mode.

Ext filesystems support acl, but its mount option. Some newer filesystems like btrfs automatically have acl enabled and they dont even mention it in mount options. You can see that acl is filesystem option in those filesystems if you read their wikis.

getfacl shows all permissions on the file in text format
setfacl can set permissions, and load backed up permissions that you backed up with getfacl

 

***To Backup Permissions:***

Go to your location that you want to backup the permissions to

cd /mnt/backup

Now restore the permissions of your folder (myweb in this case has a million other folders and files in a big tree – so getfacl recursively goes down)

getfacl -R /var/www/myweb > permissions.acl

NOTE: save the file anywhere other than /var/www/myweb

 

***To Restore Permissions:***

First get into the folder where you will restore the permissions

cd /var/www/myweb

Then point setfacl at the file, the file can be anywhere, and it will restore permissions into current folder:

setfacl --restore=/mnt/backup/permissions.acl

NOTE: you need to cd into /var/www/myweb because getfacl doesnt remember the fullpath, just the relative path from where you started
So a file like /var/www/myweb/folder1/folder2/myfile.txt will get saved into getfacl as folder1/folder2/myfile.txt. Hence why you need to cd /var/www/myweb before restoring permissions with setfacl

 

****Using GZIP to compress them:***

With a million files that permissions.acl can get huge. So lets compress on the go.

To backup with gzip:

getfacl -R /var/www/myweb | gzip > /mnt/backup/permissions.acl.gz

To restore with zip:

cd /mnt/backup
gzip -d permissions.acl.gz

That will remove permissions.acl.gz and make permissions.acl which will be alot bigger
Now run:

cd /var/www/myweb
setfacl --restore=/mnt/backup/permissions.acl

NOTE: If you dont want to lose your oginal permissions.acl.gz, then instead of running “gzip -d permissions.acl.gz” run “gzip -d permissions.acl.gz -c > permissions.acl

Leave a Reply

Your email address will not be published. Required fields are marked *