Welcome to Tencent Cloud + community, get more Tencent mass technology practice dry goods oh ~
This article was published by Xin Jiang Yuan in cloud + community column
Novice when learning Linux system, it is inevitable to encounter command input errors, or system errors. So how to fix it quickly? This article will first give you a regret medicine, let you quickly backup and restore Linux system. This article will use Ubuntu as an example, before this, you need a server, you can use Tencent Cloud Developer lab for free to experiment.
The backup
Of course, the easiest and safest way to undo changes made on the server is to perform a regular routine backup of important files.
There are a lot of backup software on Linux systems. We’ll explore the differences between backup tools to find the one that best suits your needs. It’s even more important to verify your backups regularly to make sure they perform properly.
Backups provide an excellent way to recover from server damage. As long as the replicated critical data is kept on another server, it can be useful in the event of data corruption or deletion.
Backups include full backups (full backups of all data), differential backups (backups of every file that has changed since the last full backup), and incremental backups (backups of data changes in files since the last full or differential backup).
These levels of combination are often used in conjunction with each other to fully back up files, and often you can restore individual files without restoring the entire file system. If you accidentally delete or modify a file, you can use a backup to quickly restore it.
Version control
A similar strategy to backup is version control. While not an ideal solution for backing up an entire computer, if you just want to restore files to their previous state, version control may be just what you need.
Version control systems, such as Git and Mercurial, allow you to track changes to files. This means that if you put the configuration directory /etc under version control, you can easily undo changes in the event of file corruption due to changes.
In short, you can install Git on Ubuntu using the following command:
sudo apt-get update
sudo apt-get install git
Copy the code
After the installation is complete, you need to enter the following to set some configuration options:
git config --global user.name "your_name"
git config --global user.email "your_email"
Copy the code
When you’re done, switch to the directory where you want to track changes. We will use the directories in this example. Another good place to be under version control is in your home directory. We can initialize a Git repository by typing: /etc
cd /etc
sudo git init
Copy the code
You can then add all the files in this directory (and subdirectories) by typing:
sudo git add .
Copy the code
Commit changes by entering the following:
git commit -m "Initial commit"
Copy the code
Your files will now be version controlled. When you make changes to the files in this directory, you will need to re-run the last two commands (with different content than “Initial Commit”).
You can then restore the file to its previous state by looking up the hash value in the log:
git log
Copy the code
commit 7aca1cf3b5b19c6d37b4ddc6860945e8c644cd4f
Author: root
Date: Thu Jan 23 13:28:25 2014 -0500
again
commit 4be26a199fd9691dc567412a470d446507885966
Author: root
Date: Thu Jan 23 13:20:38 2014 -0500
initial commit
Copy the code
Then restore the file with the following command, for example:
git checkout commit_hash -- file_to_revert
Copy the code
Keep in mind that you should commit Git regularly only if you are ready to make changes.
Restore the changes using the package manager
Sometimes, you can make changes using the APT package manager. The package manager can help you restore packages to their default Settings. We discuss these cases below.
Use Apt to uninstall packages
Sometimes, you install a package only to find that it’s not something you want to keep. You can remove packages from APT by typing:
sudo apt-get remove package
Copy the code
However, the command above will leave the configuration file unchanged. But if you try to remove the package completely from your system, you can use the Purge command, as shown below:
sudo apt-get purge package
Copy the code
You can uninstall any auto-installed dependencies that are no longer needed using the autoremove apt command:
sudo apt-get autoremove --purge
Copy the code
Another problem that occurs with apt installation packages is that “meta-packages” are difficult to remove correctly.
A meta-package is a simple package that depends on a list. They don’t install anything themselves, but a list of other packages. It is very difficult to delete completely automatically.
One tool that can help is the Deborphan package:
sudo apt-get install deborphan
Copy the code
After you remove the meta-package, you can run the Orphaner command to look for isolated information left behind by the package uninstallation. This will help you find packages that have not been removed by normal methods.
Another way to find miscellaneous prose pieces is through the Mlocate package. Installation:
sudo apt-get install mlocate
Copy the code
After that, you can update the file index with the following command:
sudo updatedb
Copy the code
You can then search the package name to see if there are other locations of the file system (other than the APT index) that reference the package.
locate package_name
Copy the code
You can also check the files of the meta-package installation by checking the APT log:
sudo nano /var/lob/apt/history.log
Copy the code
Restore default files
Sometimes, during configuration, you need to change the configuration file and want to restore the default file.
To keep the current configuration file as a backup, you can copy it by typing:
sudo mv file file.bak
Copy the code
If you do not have write permission to the relevant directory, you must use sudo in the command above.
After deleting the file or removing it, you can reinstall the package and check if any configuration files are missing:
sudo apt-get -o Dpkg::Options="--force-confmiss" install --reinstall package_name
Copy the code
If you don’t know which software package is responsible for the configuration file that needs to be restored, you can use the DPKG program:
dpkg -S file_name
Copy the code
If you just want to perform some of the initial package configuration steps that occurred during installation to change some values, you can use the following command:
dpkg-reconfigure package_name
Copy the code
This will restart the configuration from the original setup.
Default permission to find files
Another common situation occurs when file permissions are changed. Sometimes, you change the permissions on a file for testing purposes but want to restore them later.
You can find out the default permissions for your distribution package file by looking up which package owns the file. You can do this by issuing the following command:
dpkg -S filename
Copy the code
This will tell you the package associated with the file. For example, if we wanted to find out the package owner of the /etc/deluser.conf file, we could type:
dpkg -S /etc/deluser.conf
Copy the code
adduser: /etc/deluser.conf
Copy the code
As you can see, it tells us that the addUser package is responsible for the file. We can then check the package’s.deb file by changing it to apt:
cd /var/cache/apt/archive
Copy the code
In this directory, you will find the.deb files for the packages installed on your system. If you cannot find a file that matches the package you are using, you need to re-download it from the repository using the following command:
sudo apt-get download package
Copy the code
If our adduser package doesn’t have a.deb, we can get one by typing:
sudo apt-get download adduser
Copy the code
Once the file is in the directory, we can query the default properties of the file it is installed on by typing:
dpkg -c file.deb
Copy the code
For the adduser program, it might look like this:
DPKG adduser_3 - c. 113 ubuntu2_all. DebCopy the code
drwxr-xr-x root/root 0 2011-10-19 18:01 ./
drwxr-xr-x root/root 0 2011-10-19 18:01 ./etc/
-rw-r--r-- root/root 604 2011-10-19 18:01 ./etc/deluser.conf
drwxr-xr-x root/root 0 2011-10-19 18:01 ./usr/
drwxr-xr-x root/root 0 2011-10-19 18:01 ./usr/sbin/
-rwxr-xr-x root/root 35120 2011-10-19 18:01 ./usr/sbin/adduser
-rwxr-xr-x root/root 16511 2011-10-19 18:01 ./usr/sbin/deluser
. . .
Copy the code
As you can see, we can verify that the default package sets read/write access for root and read access for all other users.
conclusion
Now you should have some strategies to reverse your mistakes and come up with a contingency plan for planning. All five scenarios mentioned above require backup in advance.
You can buy a new server to try it out, but I suggest you use the Tencent Cloud Developer Lab for free.
References: How To Use Backups, Git, and apt-Get To Undo Changes on a Linux VPS
Question and answer
Linux Real-time scheduling algorithm?
reading
5 Docker logging best practices
Is your Nginx access too slow? Add a module!
This section describes the changes in MySQL 8.0
Has been authorized by the author tencent cloud + community release, the original link: https://cloud.tencent.com/developer/article/1159250?fromSource=waitui
Welcome to Tencent Cloud + community or follow the wechat public account (QcloudCommunity), the first time to get more mass technology practice dry goods oh ~
Massive technical practice experience, all in the cloud plus community!