Linux backup and restore and Linux file permissions details

  • 2020-06-03 09:06:27
  • OfStack

Linux backup and restore and Linux file permissions details

An overview of the

A novice system administrator accidentally entered "ES7en-ES8en 777 /", which caused a huge tragedy and caused serious damage to the whole system. In daily administration, we have many tools for backing up file permissions, such as cp, rsync, etckeeper, and so on. If you use this backup tool, you really don't need to worry about changing file permissions.

But if you just want to temporarily back up the file permissions (not the files themselves), for example: temporarily remove all file write permissions in the directory to prevent the contents of 1 directory from being overwritten; Or you may need to use the chmod command on a file while you are in the process of eliminating file permissions. In these cases, we can backup the original file permissions before they are changed, and restore the original permissions when we need them. In many cases, a full backup of a file is not necessary if you just want permissions to back it up.

On Linux, it is actually easy to back up and restore file permissions using the Access Control List (ACL). ACL defines permissions for individual files on posix-compliant file systems based on different owners and groups of owners.

Install the ACL tool

On Debian, Ubuntu, Linux Mint


$ sudo apt-get install acl

On CentOS, Fedora, RHEL


$ sudo yum install acl

Permissions to back up all files in the current directory, including subdirectories


[xgj@entel2 shells]$ getfacl -R . > permissions.txt
[xgj@entel2 shells]$ 
[xgj@entel2 shells]$ ll
total 8
-rw-rw-r-- 1 xgj xgj 231 Jan 16 12:32 permissions.txt
-rwxrwxr-x 1 xgj xgj 420 Jan 16 12:14 sys_info.sh

This command writes all ACL information for all files to a file named ES43en.txt.

The following is some directory information in the generated ES47en.txt file


[xgj@entel2 shells]$ cat permissions.txt 
# file: .
# owner: xgj
# group: xgj
user::rwx
group::rwx
other::r-x

# file: sys_info.sh
# owner: xgj
# group: xgj
user::rwx
group::rwx
other::r-x

# file: permissions.txt
# owner: xgj
# group: xgj
user::rw-
group::rw-
other::r--

Modify a 1 file permission


[xgj@entel2 shells]$ ll
total 8
-rw-rw-r-- 1 xgj xgj 231 Jan 16 12:32 permissions.txt
-rwxrwxr-x 1 xgj xgj 420 Jan 16 12:14 sys_info.sh
[xgj@entel2 shells]$ chmod 777 sys_info.sh 
[xgj@entel2 shells]$ ll
total 8
-rw-rw-r-- 1 xgj xgj 231 Jan 16 12:32 permissions.txt
-rwxrwxrwx 1 xgj xgj 420 Jan 16 12:14 sys_info.sh

Restore original permission

cd to the directory where es57EN_info.sh was created Execute the following command:

[xgj@entel2 shells]$ setfacl --restore=permissions.txt
[xgj@entel2 shells]$ ll
total 8
-rw-rw-r-- 1 xgj xgj 231 Jan 16 12:32 permissions.txt
-rwxrwxr-x 1 xgj xgj 420 Jan 16 12:14 sys_info.sh
[xgj@entel2 shells]$ 

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: