2012-09-12 79 views
0

我想要备份文件夹及其内容,方法是将该文件夹的副本及其内容放入与要备份的文件夹相同的目录中的文件夹中。由于该文件夹在备份目录中重新创建,我想将下一个数字附加到文件夹名称。 例如:备份目录+使用bash重命名

MainDirectory内容:FolderImportant FolderBackup FolderOthers

FolderImportant永远不会成为一个不同的名称。 FolderImportant,它的内容需要复制到FolderBackup中,并将001的数字附加到文件夹名称上(在第一次备份时),内容保持不变。

我已经浏览了论坛,发现了几个备份和重命名的例子,但是我对bash的了解不多,但我不确定如何将所有内容都放入一个全合一的脚本中。

+0

你有没有试过这个?我的意思是任何命令/脚本等? –

+0

我目前正在浏览一个流行的搜索引擎,并尝试使用不同的脚本来了解它们的工作原理。我有一个示例目录,我正在使用练习,但到目前为止没有结果。 –

回答

0

在bash速成班后,我有一个功能脚本。请评论是否有什么可以改进这个剧本,因为我不到6小时学习bash脚本。

#! /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