Redhat, CentOS 및 Fedora Linux에서 실수로 삭제된 파일 또는 손상된 드라이브에서 데이터 복구

Redhat, CentOS 및 Fedora Linux에서 실수로 삭제된 파일 또는 손상된 드라이브에서 데이터 복구

2022-10-19 last update

16 minutes reading fedora centos
버전 2.0 작성자: Srijan Kishore
이 튜토리얼은 Linux 파일 시스템에서 실수로 삭제된 데이터에서 데이터를 복구하는 데 도움이 될 것입니다. 이는 사용자/관리자에 의한 매우 과감한 실수로 막대한 벌금이 부과됩니다. 이 스크립트는 데이터 관리를 위한 초보자/전문가에게 도움이 될 것입니다. 데이터 복구를 위해 TestDisk를 사용할 것입니다. 여기서는 스크립트를 사용하여 작업을 단순화했습니다.

1. 배경


TestDisk는 강력한 무료 데이터 복구 소프트웨어입니다. rm -rf 또는 shift+delete 같은 명령으로 충돌/삭제된 ext3/ext4 드라이브가 있다고 가정하고 TestDisk의 도움으로 데이터/파일을 복구할 수 있습니다.
  • 파티션 테이블 수정
  • FAT, exFAT, NTFS 및 ext2/ext3/ext4 파티션에서 파일 삭제 취소
  • 이러한 기능 외에도 TesdDisk에는 데이터 유지/관리를 위한 여러 기능이 있습니다.

    2. 요구사항

    저는 TestDisk를 설치하고 손상된 드라이브에 데이터 복구 스크립트를 구현할 CentOS를 사용하고 있습니다. 이 스크립트는 CentOS/Fedora/Redhat에서 잘 작동합니다. 하드 디스크가 삭제된 경우에도 하드 디스크를 CentOS 시스템에 연결하고 스크립트를 실행하여 데이터를 복구하면 됩니다.

    3. 스크립트

    #!/bin/bash
    #######################################################################################################
    #######################################################################################################
    #######################################################################################################
    ######                                                                                          #######
    ######                                                                                          #######
    ######                 This script will help you to recover the accidentally                    #######
    ######                     deleted data from crashed linux file systems                         #######
    ######                                     Version-2                                            #######
    ######                          Script created by (Srijan Kishore)                              #######
    ######                                                                                          #######
    ######                                                                                          #######
    #######################################################################################################                                                                                       
    #######################################################################################################
    #######################################################################################################
    
    
    # User Check
    
    if [ "$USER" = 'root' ]
        then
    
    whiptail --title "User check" --msgbox "User is root, you can execute the script successfully." 8 78
                 
            echo "User is root, you can execute the script successfully"
        else
    whiptail --title "User check" --msgbox "User is not Root. Please run the script as root user." 8 78
    
            
            echo "User is not Root. Please run the script as root user."
            exit 1
    fi
    
    
    # Check your operating system
    
    cat /etc/redhat-release >> /dev/null 
    if [ "$?" = 0 ]
        then
    
    whiptail --title "Your OS" --msgbox "You are using CentOS/Redhat/Fedora" 8 78
    echo "You are using CentOS/Redhat/Fedora"
    
        else
    
    whiptail --title "Your OS" --msgbox "You are not using CentOS/Redhat/Fedora.You can download the TestDisk from this link http://www.cgsecurity.org/wiki/TestDisk_Download" 8 78
    
        echo "You are not using CentOS/Redhat/Fedora.You can download the TestDisk from this link http://www.cgsecurity.org/wiki/TestDisk_Download"
        exit 1        
    fi
    
    #adding repo
    ver=`cat /etc/redhat-release | cut -d " " -f3 | cut -d "." -f1`
    ls -l /etc/yum.repos.d/rpmforge*
    
    if [ "$?" != 0 ]
        
        then
        whiptail --title "Repository requirement" --msgbox "You need to add rpmforge repository to install testdisk" 8 78
            if [[ `uname -i` = 'i386' && $ver = 6 ]]
                then 
                echo "you are running i386 with 6 version"
                yum install -y http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.i686.rpm
            fi
    
            if [[ `uname -i` = 'x86_64' && $ver = 6 ]]
                then 
                echo "you are running i386 with 6 version"
                yum install -y http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
            fi
    
            if [[ `uname -i` = 'i386' && $ver = 5 ]]
                then 
                echo "you are running i386 with 5 version"
                yum install -y http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el5.rf.i386.rpm
            fi
    
            if [[ `uname -i` = 'x86_64' && $ver = 5 ]]
                then 
                echo "you are running x86_64 with 5 version"
                yum install -y http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el5.rf.x86_64.rpm
            fi
    
            if [[ `uname -i` = 'x86_64' && $ver = 4 ]]
                then 
                echo "you are running i386 with 4 version"
                yum install -y http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el4.rf.i386.rpm
            fi
    
            if [[ `uname -i` = 'x86_64' && $ver = 4 ]]
                then 
                echo "you are running x86_64 with 4 version"
                yum install -y http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el4.rf.x86_64.rpm
            fi
    else
    whiptail --title "Repository requirement" --msgbox "You already have TestDisk repos configured" 8 78
    echo "You already have TestDisk repos configured"
    fi
    
    
    # TestDisk installation
    testdisk --version >> /dev/null
    if [ "$?" = 0 ]
        then
        whiptail --title "Info" --msgbox "Testdisk already installed" 8 78
        echo "Testdisk already installed"
        else
        whiptail --title "Info" --msgbox "Testdisk not installed, installing Testdisk" 8 78
            echo "Testdisk not installed, installing Testdisk"
    
        yum install -y testdisk
    fi
       
    
    #Recovery part of deleted files
    
    
    ls -l /root/result/lostfiles
    if [ $? != 0 ]
        then
        mkdir -p /root/result/lostfiles
        whiptail --title "Data recovery" --msgbox "You are proceeding towards recovering the data from the lost drive. Please select the affected drive to get the data recovered" 8 78
        photorec /d /root/result/lostfiles
    
        else        
        whiptail --title "Folder Exists" --msgbox "There is already an existing folder viz /root/result/lostfiles, you are adviced to rename/remove the folder to allow the data recovery process" 8 78        
    exit 2
    fi
    
    #File filtering
     
    user=`ps aux | grep gnome-session | grep -v grep | head -1 | cut -d " " -f1`
    mkdir "/home/recovered_output"
    mkdir "/home/recovered_output/Debians"
    mkdir "/home/recovered_output/rpms"
    mkdir "/home/recovered_output/conf_files"
    mkdir "/home/recovered_output/exe"
    mkdir "/home/recovered_output/binaries"
    mkdir "/home/recovered_output/Docs"
    mkdir "/home/recovered_output/Pdfs"
    mkdir "/home/recovered_output/Mbox"
    mkdir "/home/recovered_output/Images"
    mkdir "/home/recovered_output/Videos"
    mkdir "/home/recovered_output/Sound"
    mkdir "/home/recovered_output/ISO"
    mkdir "/home/recovered_output/Excel"
    mkdir "/home/recovered_output/Presentation"
    mkdir "/home/recovered_output/Web_Files"
    mkdir "/home/recovered_output/Archives"
    mkdir "/home/recovered_output/Others"
    
    
    #Sorting the Recovered data
    
    
    find /root/result/ -name "*.doc"    -type f  -exec mv {} "/home/recovered_output/Docs/" \;
    find /root/result/ -name "*.docx"   -type f  -exec mv {} "/home/recovered_output/Docs/" \;
    find /root/result/ -name "*.odt"    -type f  -exec mv {} "/home/recovered_output/Docs/" \;
    find /root/result/ -name "*.pdf"    -type f  -exec mv {} "/home/recovered_output/Pdfs/" \;
    find /root/result/ -name "*.mbox"   -type f  -exec mv {} "/home/recovered_output/Mbox/"  \;
    find /root/result/ -name "*.png"    -type f  -exec mv {} "/home/recovered_output/Images/" \;
    find /root/result/ -name "*.jpg"    -type f  -exec mv {} "/home/recovered_output/Images/" \;
    find /root/result/ -name "*.jpeg"   -type f  -exec mv {} "/home/recovered_output/Images/" \;
    find /root/result/ -name "*.gif"    -type f  -exec mv {} "/home/recovered_output/Images/" \;
    find /root/result/ -name "*.avi"    -type f  -exec mv {} "/home/recovered_output/Videos/" \;
    find /root/result/ -name "*.mpeg"   -type f  -exec mv {} "/home/recovered_output/Videos/" \;
    find /root/result/ -name "*.mp4"    -type f  -exec mv {} "/home/recovered_output/Videos/" \;
    find /root/result/ -name "*.mkv"    -type f  -exec mv {} "/home/recovered_output/Videos/" \;
    find /root/result/ -name "*.webm"   -type f  -exec mv {} "/home/recovered_output/Videos/" \;
    find /root/result/ -name "*.wmv"    -type f  -exec mv {} "/home/recovered_output/Videos/" \;
    find /root/result/ -name "*.flv"    -type f  -exec mv {} "/home/recovered_output/Videos/" \;
    find /root/result/ -name "*.mp3"    -type f  -exec mv {} "/home/recovered_output/Sound/" \;
    find /root/result/ -name "*.wav"    -type f  -exec mv {} "/home/recovered_output/Sound/" \;
    find /root/result/ -name "*.deb"    -type f  -exec mv {} "/home/recovered_output/Debians/" \;
    find /root/result/ -name "*.bin"    -type f  -exec mv {} "/home/recovered_output/binaries/" \;
    find /root/result/ -name "*.exe"    -type f  -exec mv {} "/home/recovered_output/exe/" \;
    find /root/result/ -name "*.rpm"    -type f  -exec mv {} "/home/recovered_output/rpms/" \;
    find /root/result/ -name "*.conf"   -type f  -exec mv {} "/home/recovered_output/conf_files" \;
    find /root/result/ -name "*.iso"    -type f  -exec mv {} "/home/recovered_output/ISO/" \;
    find /root/result/ -name "*.xls"    -type f  -exec mv {} "/home/recovered_output/Excel/" \;
    find /root/result/ -name "*.xlsx"   -type f  -exec mv {} "/home/recovered_output/Excel/" \;
    find /root/result/ -name "*.csv"    -type f  -exec mv {} "/home/recovered_output/Excel/" \;
    find /root/result/ -name "*.ods"    -type f  -exec mv {} "/home/recovered_output/Excel/" \;
    find /root/result/ -name "*.ppt"    -type f  -exec mv {} "/home/recovered_output/Presentation/" \;
    find /root/result/ -name "*.pptx"   -type f  -exec mv {} "/home/recovered_output/Presentation/" \;
    find /root/result/ -name "*.odp"    -type f  -exec mv {} "/home/recovered_output/Presentation/" \;
    find /root/result/ -name "*.html"   -type f  -exec mv {} "/home/recovered_output/Web_Files/" \;
    find /root/result/ -name "*.htm"    -type f  -exec mv {} "/home/recovered_output/Web_Files/" \;
    find /root/result/ -name "*.jsp"    -type f  -exec mv {} "/home/recovered_output/Web_Files/" \;
    find /root/result/ -name "*.xml"    -type f  -exec mv {} "/home/recovered_output/Web_Files/" \;
    find /root/result/ -name "*.css"    -type f  -exec mv {} "/home/recovered_output/Web_Files/" \;
    find /root/result/ -name "*.js"     -type f  -exec mv {} "/home/recovered_output/Web_Files/" \;
    find /root/result/ -name "*.zip"    -type f  -exec mv {} "/home/recovered_output/Archives/" \;
    find /root/result/ -name "*.tar"    -type f  -exec mv {} "/home/recovered_output/Archives/" \;
    find /root/result/ -name "*.rar"    -type f  -exec mv {} "/home/recovered_output/Archives/" \;
    find /root/result/ -name "*.gzip"   -type f  -exec mv {} "/home/recovered_output/Archives/" \;
    find /root/result/ -name "*.tar.gz" -type f  -exec mv {} "/home/recovered_output/Archives/" \;
    find /root/result/ -name "*.7z"     -type f  -exec mv {} "/home/recovered_output/Archives/" \;
    find /root/result/ -name "*.bz"     -type f  -exec mv {} "/home/recovered_output/Archives/" \;
    find /root/result/ -name "*.bz2"    -type f  -exec mv {} "/home/recovered_output/Archives" \;
    find /root/result/ -name "*.*"      -type f  -exec mv {} "/home/recovered_output/Others/" \;
    
    #Finalization
    whiptail --title "Congratulations" --msgbox "You have successfully recovered your data in folder /home/recovered_output cheers :)" 8 78
    echo "You have successfully recovered your data in folder /home/recovered_output cheers :)"
    exit 0
          

    4. 스크립트 실행


    위의 스크립트를 복사하여 파일 viz에 붙여넣기만 하면 됩니다. script.sh 및 스크립트에 실행 권한을 부여합니다.
    chmod +x 스크립트.sh
    참고: 명령을 사용하여 루트로 스크립트를 실행하십시오.
    su -                                        #### 루트 비밀번호를 입력하세요##########
    스크립트를 실행하면 더 자세히 안내해 드립니다.
    sh 스크립트.sh

    5. 마무리


    먼저 스크립트는 스크립트를 실행하는 사용자가 루트인지 확인한 다음 자동 저장소 추가를 포함하는 TestDisk 도구 설치를 확인합니다. 데이터 복구를 위한 드라이브 선택 페이지로 리디렉션됩니다. OS를 확인합니다.
    스크립트는 TestDisk에 필요한 저장소를 자동으로 확인하고 추가합니다. 스크립트가 TestDisk의 설치를 확인합니다.
    복구하려는 드라이브를 선택하십시오. 프로세스 완료 후 화면에서 종료합니다. 건배 데이터를 복구했습니다.