IT bound has an old terrier, a database administrator of some forum complains that his boss has been maltreatment him, and the result is that under his anger he deleted the library and ran away……

So…

Beijing, Aug. 20 (Xinhua) — XU, a software engineer in Beijing, deleted the source code of the website he designed by using a backdoor file inserted after his resignation because his company failed to pay his salary on time. Reporters 20 from the Fengtai District People’s Court in Beijing, Xu was convicted of destroying computer information system, was sentenced to five years in prison.

  


I accidentally ran the rm -rf command…… during server maintenance Now the whole server was deleted by me how to do??

  


Okay, so let’s talk about RM.

Power of RM-RF

Rm is the Linux command to delete files. -r stands for delete everything under this, everything and everything. F indicates that the command is executed without user confirmation.

Usually this command is used to specify a folder, such as

rm -rf /home/test/

Delete everything under /home/test/.

But if the folder path is not added correctly,

Rm -rf/on the server means…

  


As the saying goes: often walk in the river, which can not wet shoes. So how can this tragedy be prevented?

How to avoid running again?

One solution is to redirect the RM command to graft as mv, which is equivalent to customizing a recycle bin for Linux.

The implementation is as follows:

Redefine the rm command ###

# define the recycle bin directory

trash_path=’~/.trash’

If the file defined by $trash_path exists, then $trash_path is created.

if [ ! -d $trash_path ]; then

mkdir -p $trash_path

fi

# Define alias: use rm to call trash

alias rm=trash

# call ‘ls ~/.trash’

# If you want to change the recycle bin directory, you need to change the recycle bin directory

alias rl=’ls ~/.trash’

Restorefile is called using unrm and needs to be executed in the parent directory of the deleted directory

alias unrm=restorefile

# claearteash is called using rmTrash

alias rmtrash=cleartrash

Function to restore files

restorefile()

{

mv -i ~/.trash/$@ ./

}

# Delete file function

trash()

{

mv $@ ~/.trash/

}

Empty the recycle bin function

cleartrash()

{

Read-p Are you sure you want to empty the recycle bin? [y/n] confirm

[ $confirm == ‘y’ ] || [ $confirm == ‘Y’ ] /bin/rm -rf ~/.trash/*

}

Finally, write the above script to /etc/bashrc and run the source /etc/bashrc command immediately.

  


use

This script defines several commands:

Rl: View files in the recycle bin

Unrm File name or directory: Restores to the current path

Rmtrash: Empty the recycle bin, but with friendly prompts. Executing rm does not actually delete, but instead uses MV to move to the recycle bin we specify.

If you really want to delete it, you can delete it by /bin/rm.

Also, note that some of the parameters of the previous RM instruction may no longer be used because rm is now mv.

# touch hoxistest

# touch 1

# mkdir haha

# ls

1 haha hoxistest

# rm 1

# ls

haha hoxistest

# View files in the recycle bin

# rl

1 myftp1

Restore deleted files

# unrm 1

[root@CESHI-CLM-10-254-4-48 test]# ls

1 haha hoxistest

# rm haha/

# rm hoxistest

# rl

haha hoxistest

# Situation recycle bin

# rmtrash

Are you sure you want to empty the recycle bin? [y/n]y

# rl

The effect seems to be ok.

  





Conclusion routine

It looks ok, but there are some problems, such as deleting files can not be the same name, if the same name will prompt you whether to overwrite. Then you need to do special processing, such as add a timestamp when deleting something, if you are interested in implementing it.