2012-09-12 6 views
0

백업 할 폴더와 동일한 디렉토리에있는 폴더에 폴더의 내용과 그 내용을 배치하여 폴더와 그 내용을 백업하려고합니다. 폴더가 백업 디렉토리에 다시 만들어 지므로 다음 번호를 폴더 이름에 추가하려고합니다. 예를 들어 :백업 디렉토리 + bash를 사용하여 이름 바꾸기

MainDirectory 내용 : FolderImportant FolderBackup의 FolderOthers

FolderImportant 다른 이름이 될하지 않습니다. FolderImportant와 그 내용은 FolderBackup에 복사해야하며 폴더 이름에 첫 번째 백업에 001이라는 숫자가 추가되어야합니다. 내용은 변경되지 않습니다.

나는 포럼을 들여다 보았지만 몇 가지 백업 예제와 이름 바꾸기를 찾았지만, 배쉬에 대해 알고있는 거의 알 수 없지만 모든 것을 올인원 스크립트에 집어 넣는 방법을 잘 모르겠습니다.

+0

이 작업을 수행 했습니까? 나는 어떤 명령/스크립트 등을 의미합니까? –

+0

저는 현재 인기있는 검색 엔진을 탐색 중이며 그들이 작동하는 방식에 대한 아이디어를 얻으려는 다른 스크립트를 사용하려고합니다. 연습 할 때 사용하는 샘플 디렉토리가 있지만 지금까지 결과가 없습니다. –

답변

0

에 대한 내 대답은 중대하다, 나는 기능 스크립트가 있습니다. 내가이 스크립트를 향상시키기 위해 할 수있는 일이 있다면 bash 스크립팅을 배우는 데 6 시간도 안 남았습니다.

#! /bin/bash 

# The name of the folder the current backup will go into 
backupFolderBaseName="ImportantFolder_" 
# The number of the backup to be appended to the folder name 
backupFolderNumber=0 
# The destination the new backup folders will be placed in 
destinationDirectory="/home/$LOGNAME/.hiddenFolder/projectFolder/backupFolder" 
# The directory to be backed up by this script 
sourceDirectory="/home/$LOGNAME/.hiddenFolder/projectFolder/ImportantFolder" 

# backupDirectory()------------------------------------------------------------------------------------- 
# Update folder number and copy source directory to destination directory 
backupDirectory() { 
cp -r $sourceDirectory "$destinationDirectory/$backupFolderBaseName`printf "%03d" $backupFolderNumber`" 
echo "Backup complete." 
} #End backupDirectory()-------------------------------------------------------------------------------- 

# Script begins here------------------------------------------------------------------------------------ 
if ! [ -d "$destinationDirectory" ]; 
then 
    echo "Creating directory" 
    mkdir "$destinationDirectory" 
    if [ -d "$destinationDirectory" ]; 
    then 
     echo "Backup directory created successfully, continuing backup process..." 
     backupDirectory 
    else 
     echo "Failed to create directory" 
    fi 
else 
echo "Existing backup directory found, continuing backup process..." 
for currentFile in $destinationDirectory/* 
    do 
     tempNumber=$(echo $currentFile | tr -cd '[[:digit:]]' | sed -e 's/^0\{1,2\}//') 
     if [ "$tempNumber" -gt "$backupFolderNumber" ]; 
     then 
      backupFolderNumber=$tempNumber 
     fi 
    done 
    let backupFolderNumber+=1 
backupDirectory 
fi #End Script here------------------------------------------------------------------------------------- 
0

rsync을 보시면 다른 전략으로 백업 할 수있는 강력한 도구입니다. 몇 가지 예를 들어 here을 살펴보십시오.

0

rsync를 여기에 ... 떠들썩한 파티에서 충돌 과정 후 bash는 질문

#!/bin/bash 

dirToBackup=PATH_TO_DIR_TO_BACKUP 

backupDest=BACKUP_DIR 

pureDirName=${dirToBackup##*/} 

for elem in $(seq 0 1000) 
do 
    newDirName=${backupDest}/${pureDirName}_${elem} 
    if ! [ -d $newDirName ] 
    then   
     cp -r $dirToBackup $newDirName 
     exit 0 
    fi 
done  
관련 문제