서버 작업하면 가끔 콘솔 2개 열고 작업하다 엉뚱한데 날리는 경우가 있습니다.
특히나 꾸벅 꾸벅 졸면서 새벽작업할때.. 캄캄합니다. IDC까지 그새벽에 가야하고..
쉘스크립트 입니다.
cat newrm.sh
#!/bin/sh
# newrm - a replacement for the existing rm command that allows a
# rudimentary unremove capability through utilizing a newly created
# directory in the user's home directory. It can handle directories
# of content as well as individual files, and if the user specifies
# the -f flag, files are NOT archived, but removed.
# rewrm 에서 -f를 붙이면 휴지통으로 버리지않고 완전 제거한다는...
# Big Important Warning: you'll want a cron job or similar to keep the
# individual trash directories tamed, otherwise nothing will ever
# actually be deleted on the system and you'll run out of disk space!
mydir="$HOME/.deleted-files" # 지워진 파일이 저장되는 곳
realrm="/bin/rm " # rm파일위치
copy="/bin/cp -R" # 복사(휴지통으로 넣기)위한 cp 명령 경로
if [ $# -eq 0 ] ; then # let 'rm' ouptut the usage error
exec $realrm # our shell dies and is replaced by /bin/rm
fi
# parse all options looking for '-f'
flags=""
while getopts "dfiPRrvW" opt
do
case $opt in
f ) exec $realrm "$@" ;; # exec lets us exit this script directly.
* ) flags="$flags -$opt" ;; # other flags are for 'rm', not us
esac
done
shift $(( $OPTIND - 1 ))
# make sure that the $mydir exists
if [ ! -d $mydir ] ; then
if [ ! -w $HOME ] ; then
echo "$0 failed: can't create $mydir in $HOME" >&2
exit 1
fi
mkdir $mydir
chmod 700 $mydir # a little bit of privacy, please
fi
for arg
do
newname="$mydir/$(date "+%S.%M.%H.%d.%m").$(basename "$arg")"
if [ -f "$arg" ] ; then
$copy "$arg" "$newname"
elif [ -d "$arg" ] ; then
$copy "$arg" "$newname"
fi
done
exec $realrm $flags "$@" # our shell is replaced by realrm
----------------------------------------------------------------------------------------------------------
본인계정의
.profile에 alias rm='/root/newrm.sh' 추가후
source .profile 리로드
사용 rm 파일명하면 다짜고짜 지워버립니다.
단 본인 HomeDir/.deleted-files 에 보면 지워진 파일이 들어있습니다....
수동으로 꺼내서 이름을 변경하셔도 되구요..
아래의 파을을 이용하면 됩니다. 윈도그로 따지면 휴지통에서 복원정도 명령이겠네요.
cat unrm.sh
#!/bin/sh
# unrm - search the deleted files archive for the specified file. If
# there is more than one match, show a list ordered by timestamp, and
# let the user specify which they want restored.
# Big Important Warning: you'll want a cron job or similar to keep the
# individual trash directories tamed, otherwise nothing will ever
# actually be deleted on the system and you'll run out of disk space!
mydir="$HOME/.deleted-files"
realrm="/bin/rm"
move="/bin/mv"
dest=$(pwd)
if [ ! -d $mydir ] ; then
echo "$0: No deleted files directory: nothing to unrm" >&2 ; exit 1
fi
cd $mydir
if [ $# -eq 0 ] ; then # no args, just show listing
echo "Contents of your deleted files archive (sorted by date):"
# ls -FC | sed -e 's/[[:digit:]][[:digit:]]\.//g' -e 's/^/ /'
ls -FC | sed -e 's/\([[:digit:]][[:digit:]]\.\)\{5\}//g' \
-e 's/^/ /'
exit 0
fi
# Otherwise we must have a pattern to work with. Let's see if the
# user-specified pattern matches more than one file or directory
# in the archive.
matches="$(ls *"$1" 2> /dev/null | wc -l)"
if [ $matches -eq 0 ] ; then
echo "No match for \"$1\" in the deleted file archive." >&2
exit 1
fi
if [ $matches -gt 1 ] ; then
echo "More than one file or directory match in the archive:"
index=1
for name in $(ls -td *"$1")
do
datetime="$(echo $name | cut -c1-14| \
awk -F. '{ print $5"/"$4" at "$3":"$2":"$1 }')"
if [ -d $name ] ; then
size="$(ls $name | wc -l | sed 's/[^0-9]//g')"
echo " $index) $1 (contents = ${size} items, deleted = $datetime)"
else
size="$(ls -sdk1 $name | awk '{print $1}')"
echo " $index) $1 (size = ${size}Kb, deleted = $datetime)"
fi
index=$(( $index + 1))
done
echo ""
echo -n "Which version of $1 do you want to restore ('0' to quit)? [1] : "
read desired
if [ ${desired:=1} -ge $index ] ; then
echo "$0: Restore cancelled by user: index value too big." >&2
exit 1
fi
if [ $desired -lt 1 ] ; then
echo "$0: restore cancelled by user." >&2 ; exit 1
fi
restore="$(ls -td1 *"$1" | sed -n "${desired}p")"
if [ -e "$dest/$1" ] ; then
echo "\"$1\" already exists in this directory. Cannot overwrite." >&2
exit 1
fi
echo -n "Restoring file \"$1\" ..."
$move "$restore" "$dest/$1"
echo "done."
echo -n "Delete the additional copies of this file? [y] "
read answer
if [ ${answer:=y} = "y" ] ; then
$realrm -rf *"$1"
echo "deleted."
else
echo "additional copies retained."
fi
else
if [ -e "$dest/$1" ] ; then
echo "\"$1\" already exists in this directory. Cannot overwrite." >&2
exit 1
fi
restore="$(ls -d *"$1")"
echo -n "Restoring file \"$1\" ... "
$move "$restore" "$dest/$1"
echo "done."
fi
exit 0
휴지통에서 복구하기..
unrm.sh 복구할 파일명하면 됩니다.
꼭 필요하던건데.. 이번에 책을 보면서 알게되어 저자의 사이트에서 퍼와서 테스트후
작성합니다.
책명 : 셸 스크립트 101가지 예제로 정복하는 - 에이콘출판사
광고라고 혹시 하실분 계실까 몰라서 인데 제가 22,500원주고 모 사이트에서 사서 본책입니다.
원저자는 외국인 인듯합니다.
그럼 머리가 캄캄해 지지 않도록 노력해 봅시다.
영삼넷
Categories
Recent Posts
Recent Comments
Statistics
- Total Visitors:
- 407054
- Today:
- 6843185
- Yesterday:
- 9754777
IT강국 김영삼 블로그에 오신걸 진심으로 환영합니다.
©2002 영삼넷 // openkr
©2002 영삼넷 // openkr