Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of deleting database

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to solve the problem of database deletion". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

A preliminary study of the crime scene

Let's take a look at how the junk code written by Kobayashi triggered this deletion.

Since the phenomenon of rm-fr / * has occurred, it is inevitable that the variable new_lic_dir is empty.

So when the statement rm-fr $new_lic_dir/* is executed, it becomes a rm-fr / * delete statement. All right, we got the murder weapon.

Then why is the new_lic_dir empty?

The careful little friend must have noticed because the new_lic_dir variable was assigned in backquotation marks.

Yes, that's the reason for the backquotation marks.

Backquotes have a special meaning on the Linux Shell command line: the contents between backquotes are executed first by Shell. After its output is put into the main command, the main command is executed.

In other words, the value of new_lic_dir is the result of the execution of the command ${lic_path} / new_license. The problem is that this is not a command, so it must return a null value to the new_lic_dir variable.

Kobayashi wrote such a gentle code, unexpectedly turned into a ferocious deletion code.

The reason for this is that the backquotation marks should be changed to double quotation marks.

Kobayashi, you really suck, such a simple assignment command is wrong.

Ha, it's really bad, like I said, it's almost Labour Day holiday, and Kobayashi wrote this code floating.

So habitually start the first kung fu of programmers' content: crtl+c and crtl+v.

Copy and paste the first assignment lic_path= `pwd` statement, and then only change the variable name, not paying attention to the backquotation mark to be changed to double quotation marks, resulting in the tragedy of deleting the library.

Preserve the crime scene

Now that the deletion has happened, do not restart the server or close the session of the ssh connection, but keep the crime scene and find out what is left.

Kobayashi, isn't that a cannon? How can I find out if the ls is gone?

Fortunately, this time is lucky, because in the execution of the script, the first time found something wrong, immediately cut off the script is still running, so not all Linux files have been deleted.

As long as I pinch fast, rm-fr / * can't kill me.

Although ls has been deleted, it is fortunate to find that the cd command still works.

As long as cd is used well, it can also use the ls effect. Quite simply, all the files in the specified directory will appear automatically with the cd + Tab key.

With the cd + Tab key, we can view the files in each directory, so we can confirm which system files have been deleted step by step.

After some confirmation and comparison, it is found that the four main directories that have been deleted are

The three directories / bin, / boot and / dev have been deleted.

The dynamic library section in the / lib directory is deleted

Let's review what is stored in the above four directories:

/ bin stores common system commands. Ls, cp, rm, chmod and other common commands are all in this directory.

/ boot system boot directory to save files related to system startup, such as kernel files and boot loader

/ dev device file save location

/ lib stores the dynamic library and static library files needed by the program

/ boot has been deleted, but fortunately Kobayashi did not restart the server. If you restart the server, it will be over and the system will definitely not work.

The cd command is in the / sin directory, and / sin is still sound, so cd can be used normally.

Fortunately, the important database information and files have not been deleted, so Kobayashi's primary goal is to restore / bin, / boot, / dev, / lib.

Restore a file

Due to the deletion of the / bin directory and some dynamic files of / lib, the common methods of transferring files, such as ftp, scp, mount, etc., cannot be used.

Kobayashi fumbled for a long time and found that wget can be used. The wget command is in the / usr/bin directory, but fortunately / usr/bin is still sound.

So, using a trick, start with another normal server, put the / bin directory in the Web directory of the Web server, and then download it through wget.

If there is a chance, I see the dawn of success.

But a new problem arises. The command file I downloaded does not have the permission to execute it.

The chmod command is in the / bin directory, which is also deleted and cannot be used to grant file permissions.

Also, I found a great command perl on the Internet, which can be used to give file permissions:

Perl-e "chmod 777, 'ls'"

That's a magical order.

Well, now the assignment permission problem has also been solved, and success is in sight.

Wget cannot download the / bin directory directly, only one file can be downloaded.

But Kobayashi, I can't download it one by one to recover. It will take years and months to complete.

Kobayashi thought of a way:

First download the tar command through wget, and give the tar command permission through perl

Then another server packages the / bin directory into a compressed file, and then downloads the zip file of the bin directory through wget

Finally, the bin package is extracted by tar command.

/ bin is restored in this way, and the remaining directories are restored through the same operation.

Xiao Lin's smile gradually came back, ha.

When you encounter the rm-fr / * deletion incident, you must calm down and keep a steady mind.

The reason why Kobayashi was lucky to recover from this deletion incident is that there are two key points:

Kobayashi found that the execution of the script was not normal and decisively cut it off immediately, without causing important database information to be deleted. If it had been cut off a little later, it might have been gone.

When Kobayashi found that the common commands could not be used, he did not restart the server, otherwise the server would not be able to get up and the ssh session would not be closed, otherwise he would not be able to reconnect to the ssh session and would not be able to operate.

If the above two points are not done well, it will be much more difficult to recover the server, and what is more serious is that it will not be over on May Day.

Prevention of misexecution of rm-fr / *

Since rm-fr / * is a cruel murder weapon, it is necessary to prevent it. Next, we will discuss several ways to prevent it.

Option 1: rm-rf should judge the directory when deleting the directory

#! / bin/bash

Work_path= `pwd`

# delete only if the directory is not empty

If [${work_path}! = "]; then

Rm-fr ${work_path} / *

Fi

Before performing the delete directory operation, determine whether the directory to be deleted is empty or not before performing the delete operation.

Option 2: Shell script specifies set-u

When executing the script, Bash ignores it by default if it encounters a variable that does not exist.

#! / bin/bash

Echo $a

Echo hello

In the above code, $an is a non-existent variable, and the execution result is as follows.

$bash test.sh

Hello

You can see that echo $a prints a blank line, and Bash ignores the $a that doesn't exist, and then continues to execute echo hello.

It is best to encounter that the variable does not exist and the script should report an error rather than proceed quietly.

Set-u is used to change this behavior, add it to the script, encounter a non-existent variable will report an error, and stop execution.

#! / bin/bash

Set-u

Rm-fr $a gamble *

Echo hello

The running results are as follows:

$bash test.sh

Test.sh: line 4: a: unbound variable

As you can see, because an is an undefined variable, the footer is wrong and the following statements are no longer executed.

Option 3: safe-rm replaces rm

Safe-rm is an open source software tool, the name sounds secure, so it is used to replace the less secure rm.

It can configure path blacklists in / etc/safe-rm.conf to define which ones cannot be deleted by safe-rm.

We can rename safe-rm to rm. If / etc/ is defined and cannot be deleted, an error will be reported when deleting / etc:

$rm-rf / etc/

Safe-rm: skipping / etc/

Plan 4: establish the recycle bin mechanism

Windows has a Recycle Bin. Even if it is deleted by mistake, it can be restored in the Recycle Bin.

Therefore, we can also implement the mechanism of the Recycle Bin in Linux.

The idea of realization is:

When deleting a file, it does not actually delete the file, but moves the file to a specific directory, and you can set the time to clear the Recycle Bin. Or delete when the file size in the Recycle Bin reaches a certain capacity (or take time to judge) to make room.

You can write a Shell script to replace the rm command, or use the mv command to move the file to the Recycle Bin when you need to delete it.

① creates a Recycle Bin directory

Mkdir / home/.trash

② writes a remove.sh script as follows

③ modifies ~ / .bashrc and replaces the rm command with our self-built remove.sh

Alias rm= "sh / home/remove.sh"

④ sets crontab to empty trash bins regularly, such as 0: 00 a. M. every day:

0 * rm-rf / home/.trash/*

Finally, ⑤, execute the following command to make it effective

Source / .bashrc

Scheme 5: the root file is mounted as read-only

In the / etc/fstab file, mount the / file system as read-only.

Among them, remount,ro means mount read-only.

After the mount is read-only, the deletion operation cannot be successful:

This is the end of the content of "how to solve the problem of deletion". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report