It's been said that there's really no proper fix for this, but it's almost doable given a rescue environment.
As with all recovery operations, you should do what you can to make sure you have a backup copy, or make one now out of all the data you can access, in case you accidentally do further damage.
First, if you have a recent backup, you might be better off restoring from the backup. Even if you don't want to restore the files (maybe some have been updated since), doing a permission-only restore is a feasible option and reasonably safe - if you haven't installed anything new or changed any configs since, you'd have a clean restore + data changes since.
To do this from a backup, you'd want to first restore the backup to a temp directory, and then copy the permissions over.
The other option is to set up a simple base system (maybe temporarily spin up a new VPS), back up the permissions from there, and restore those permissions to the existing server. However, this will obviously not include any changes from packages you've installed since, nor any config changes you've made. This will probably get you back into a bootable system, but you should not continue using it - plan a full rebuild ASAP.
The general method for backing up, restoring, or copying permissions is to use the getfacl
and setfacl
commands.
First, cd
into the root of the affected system (either /
on a running one, or wherever you've mounted the drive on a rescue system, or the temp directory you want to copy permissions from), and then:
Backup with
getfacl -R . >permissions.facl
and restore with
setfacl --restore=permissions.facl