Linux system prevents accidental deletion of files

Source: Internet
Author: User
Tags rsync

1.Preface

Once Upon a while, a rm-rf/useless/* command took out the files we needed, and the entire system was killed ,

This is not with tears.

such as the use of the RM command mistakenly delete the file, we will notice, but we can not guarantee that it will not happen, after all, there is always an accident, then for this situation we are not bound without policy, no! We have a corresponding solution, that is, the use of trash-cli.

1.1 Trash-cliIntroduction

TRASH-CLI provides a set of command-line Recycle Bin tools. In the GNOME environment, this command-line recycle bin is unified with the Gnome Recycle Bin, and the deleted files will go to the Recycle Bin, both at the command line and in the graphical environment.

1.2DownloadTrash-cliTool Pack

trash-cli zip package download link available on GitHub: HTTPS://GITHUB.COM/ANDREAFRANCIA/TRASH-CLI

1.3installationTrash-cliTools

# Installation Environment

[[email protected] ~]# uname-r2.6.32-504.el6.x86_64[[email protected] ~]# uname-mx86_64[[email protected] ~]# cat/etc/r Edhat-releasecentos Release 6.6 (Final)

# Unzip the installation TRASH-CLI

[Email protected] tools]# lstrash-cli-master.zip[[email protected] tools]# Unziptrash-cli-master.zip[[email Protected] tools]# CD Trash-cli-master[[email protected] trash-cli-master]# python Setup.pyinstall
1.4 Trash-cliManagement Command Description

#trash-cli

[[email protected] trash-cli-master]# ls -l/usr/bin/|grep trash-rwxr-xr-x     1 root root        123 Mar 2  19:34 trash-rwxr-xr-x    1 root root         125 Mar 2 19:34 trash-empty-rwxr-xr-x    1 root  Root        124 mar 2 19:34 trash-list-rwxr-xr-x     1 root root        123 mar  2 19:34 trash-put-rwxr-xr-x    1 root root         127 Mar 2 19:34 trash-restore-rwxr-xr-x     1 root root        122 mar 2 19:34  trash-rm

Function Description:

Trash-put// moving files or directories into the recycle bin

Trash-empty days// emptying the recycle bin, Trash-empty The 10-day old file

Trash-list// list files in the Recycle bin

Trash-restore// Restore files in the Recycle bin

TRASH-RM// Delete a single file in the back station

# Special Tip:trash-put The command will move the files we want to delete to ~/.local/share/trash/files When we use the trash-restore can not be recovered from ~/.local/share/trash/files files or directories that will need to be recovered in the directory CP to the appropriate path, the relevant information is recorded in the ~/.local/share/trash/info the.

1.5withTrash-cliReplaceRmCommand

# for The sake of system security we replace the RM command with Trash-put. Prevent accidental deletion of operations.

[[email protected] ~]# echo ' aliasrm= "Trash-put" ' >>/etc/profile[[email protected] ~]# tail-1/etc/profilealias RM = "Trash-put" [[email protected] ~]#. /etc/profile
1.6 Trash-cliTool Usage Examples

Example 1: Delete the/etc/issue file and restore it.

[Email protected] ~]# Cat/etc/issuecentos release 6.6 (Final) Kernel \ r on an \m[[email protected] ~]# Rm/etc/issue[[ema Il protected] ~]# cat/etc/issuecat:/etc/issue:no such file Ordirectory[[email protected] ~]# Cp~/.local/share/trash/fil Es/issue/etc/[[email protected] ~]# Cat/etc/issuecentos release 6.6 (Final) Kernel \ r on an \m

Example 2: RM-RF/* recovery performed by mistake

# When we execute the rm-rf/* operation even if the CTRL + C Terminate operation immediately deletes part of the content

[[Email protected] ~]# rm -rf /*trash-put: cannot trash directory '/ Boot ' ^ctraceback  (most recent call last): file  "/usr/bin/trash-put",  line  5, in <module>   sys.exit (Main ())  File  "/usr/lib/python2.6/ site-packages/trashcli/put.py ", line17, in main   ). Run (SYS.ARGV)  File   "/usr/lib/python2.6/site-packages/trashcli/put.py", line46, in run    Self.trash_all (args)  File  "/usr/lib/python2.6/site-packages/trashcli/put.py",  line52, in  trash_all   self.trashcan.trash (ARG)  File  "/usr/lib/python2.6/site-packages/ Trashcli/put.py ",  line248, in trash   trashed_file = trash_dir.trash ( File)  File  "/usr/lib/python2.6/site-packages/trashcli/put.py",  line372, in trash    self.move (path, wHere_to_store_trashed_file)  File  "/usr/lib/python2.6/site-packages/trashcli/fs.py",  line47,  in move   return shutil.move (Path, str (dest))  File  "/usr/lib64/ Python2.6/shutil.py ",  line 257, in move   copytree (SRC,&NBSP;REAL_DST,  symlinks=true)  File  "/usr/lib64/python2.6/shutil.py", line 156, in  Copytree   copy2 (srcname, dstname)  File  "/usr/lib64/python2.6/shutil.py",  Line 95, in copy2   copyfile (SRC,&NBSP;DST)  File  "/usr/lib64/python2.6 /shutil.py ",  line 52, in copyfile
Copyfileobj (FSRC, FDST) File "/usr/lib64/python2.6/shutil.py", line +, in Copyfileobj fdst.write (BUF) Keyboardinterrupt

# After the deletion, we find that LS,CP and other commands have not been executed.

[Email protected] ~]# Ls-bash:/bin/ls:no such file or Directory[[email protected] ~]# Cp-bash:/bin/cp:no such file or Directory

# you won't be able to view the contents of the Recycle Bin by switching to the folder.

[[email protected] ~]# cd~/.local/share/trash/files/[[email protected] files]# Ls-bash:/bin/ls:no such file ordirectory

# Use the rsync command to recover files that were mistakenly deleted to / directory

[[email protected] ~]# cd~/.local/share/trash/files/[[ Email protected] files]# rsync -avzp ./* /sending incremental file  listbin/bin/alsaunmute        123 100%     0.00kB/s    0:00:00  (xfer#1, to-check=151/155) bin/arch       27776 100%    2.65MB/s    0:00:00  (Xfer#2,  to-check=150/155) bin/awk -> gawkbin/basename      26264  100%    2.09mb/s    0:00:00  (xfer#3, to-check=148/155) bin/ bash     940416 100%    7.60mb/s     0:00:00  (xfer#4, to-check=147/155) 

# After the recovery is completed , the LS view command is restored, followed by the directory page

[[email protected] files]# lsbin boot dev[[email protected] files]# CD/[[email protected]/]# lsbin Dev Home lib64 Media opt root selinux srv tmp varboot etc lib lost+found mnt proc sbin server sys usr
1.7emptying files in the Recycle bin

# Clear All

[Email protected] ~]# Trash-empty

# emptied 7 days ago.

[Email protected] ~]# Trash-empty 7


This article is from the Linux OPS blog, so be sure to keep this source http://enzhi.blog.51cto.com/11193298/1746894

Linux system prevents accidental deletion of files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.