更安全的rm命令,保護重要資料

駿馬金龍發表於2020-05-05

更安全的rm命令,保護重要資料

網上流傳的安全的rm,幾乎都是提供一個rm的"垃圾"回收站,在伺服器環境上來說,這實非良方。

我想,提供一個安全的rm去保護一些重要的檔案或目錄不被刪除,避免出現重要資料誤刪的悲劇,或許才是更佳方案。

我寫了一個指令碼:https://github.com/malongshuai/rm_is_safe ,原始碼和用法本文後面已經提供了,不過各位願意捧場的可以去github上點個star。

工作方式

rm_is_safe會建立一個名為/bin/rm的shell指令碼,同時會備份原生的/bin/rm為/bin/rm.bak。所以,原來如何使用rm,現在也以一樣的方式使用rm,沒有任何區別。

為了區分原生rm和偽裝後的安全的rm,下面將偽裝的rm命令稱為rm_is_safe

rm_is_safe會自動檢查rm被呼叫時傳遞的引數,如果引數中包含了重要檔案,可能意味著這是一次危險的rm操作,rm_is_safe會直接忽略本次rm。至於哪些屬於重要檔案,由你自己來決定。

rm_is_safe對所有使用者都有效,包括目前已存在的使用者和未來新建立的使用者。

哪些是重要檔案?

  1. 根目錄/以及根目錄下的子目錄、子檔案總是自動被保護的

  2. 你可以在/etc/security/rm_fileignore中定義你自己覺得重要的檔案,每行定義一個被保護的檔案路徑。例如:

    /home/junmajinlong
    /home/junmajinlong/apps
    

現在,該檔案中定義的兩個檔案都被保護起來了,它們是安全的,不會被rm刪除。

注意事項:

  1. 顯然,被保護的目錄是不會進行遞迴的,所以'/bin'是安全的,而'/bin/aaa'是不安全的,除非你將它加入/etc/security/rm_fileignore檔案中
  2. 根目錄/以及根目錄下的子目錄是自動被保護的,不用手動將它們新增到/etc/security/rm_fileignore中
  3. /etc/security/rm_fileignore檔案中定義的路徑可以包含任意斜線,rm_is_safe會自動處理。所以,'/home/junmajinlong'和'/home///junmajinlong/////'都是有效路徑
  4. /etc/security/rm_fileignore中定義的路徑中不要使用萬用字元,例如/home/*是無效的

Usage

1.執行本文後面提供的Shell指令碼:

$ sudo bash rm_is_safe.sh

執行完成後,你的rm命令就變成了安全的rm了。

2.如果確實想要刪除被保護的檔案,比如你明確知道/data是可以刪除的,那麼你可以使用原生的rm命令,即/bin/rm.bak來刪除。

$ rm.bak /path/to/file

3.如果你想要解除安裝rm_is_safe,執行函式uninstall_rm_is_safe即可:

# 如果找不到該函式,則先exec bash,再執行即可
$ uninstall_rm_is_safe

解除安裝完成後,/bin/rm就變回原生的rm命令了。

指令碼:rm_is_safe.sh

指令碼如下,假設其檔名為rm_is_safe.sh

#!/bin/bash

###############################
# Author: www.junmajinlong.com
###############################

# generate /bin/rm
#   1.create file: /etc/security/rm_fileignore
#   2.backup /bin/rm to /bin/rm.bak
function rm_is_safe(){
  [ -f /etc/security/rm_fileignore ] || touch /etc/security/rm_fileignore
  if [ ! -f /bin/rm.bak ];then
    file /bin/rm | grep -q ELF && /bin/cp -f /bin/rm /bin/rm.bak
  fi

  cat >/bin/rm<<'eof'
#!/bin/bash
args=$(echo "$*" | tr -s '/' | tr -d "\042\047" )
safe_files=$(find / -maxdepth 1 | tr '\n' '|')\
           $(cat /etc/security/rm_fileignore | tr '\n' '|')
echo "$args" | grep -qP "(?:${safe_files%|})(?:/?(?=\s|$))"
if [ $? -eq 0 ];then
  echo -e "'\e[1;5;33mrm $args\e[0m' is not allowed,Exit..."
  exit 1
fi
/bin/rm.bak "$@"
eof

  chmod +x /bin/rm
}

# for uninstall rm_is_safe
# function `uninstall_rm_safe` used for uninstall
function un_rm(){
  # make efforts for all user
  if [ ! -f /etc/profile.d/rm_is_safe.sh ];then
    shopt -s nullglob
    for uh in /home/* /root /etc/skel;do
      shopt -u nullglob

cat >>$uh/.bashrc<<'eof'
# for rm_is_safe:
[ -f /etc/profile.d/rm_is_safe.sh ] && source /etc/profile.d/rm_is_safe.sh
eof
    done
  fi

cat >/etc/profile.d/rm_is_safe.sh<<'eof'
function uninstall_rm_is_safe(){
  unset uninstall_rm_is_safe
  /bin/unlink /etc/security/rm_fileignore
  /bin/cp -f /bin/rm.bak /bin/rm
  /bin/unlink /etc/profile.d/rm_is_safe.sh
  shopt -s nullglob
  for uh in /home/* /root /etc/skel;do
    shopt -u nullglob
    sed -ri '\%# for rm_is_safe%,\%/etc/profile.d/rm_is_safe.sh%d' $uh/.bashrc
  done
}
export -f uninstall_rm_is_safe
eof
}

rm_is_safe
un_rm

相關文章