0

我想通过将文件夹及其内容的副本放置到与要备份的文件夹位于同一目录中的文件夹中来备份文件夹及其内容。由于在备份目录中重新创建了文件夹,我想将下一个数字附加到文件夹名称。例如:

MainDirectory Contents: 文件夹重要文件夹备份文件夹其他

FolderImportant 永远不会是不同的名称。FolderImportant 及其内容需要复制到 FolderBackup 并在文件夹名称后附加数字 001(在第一次备份时),内容保持不变。

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

4

3 回答 3

0

看看rsync,它是一个强大的工具,可以使用不同的策略进行备份。看看这里的一些例子。

于 2012-09-12T10:04:01.867 回答
0

在 bash 速成课程之后,我有了一个功能脚本。请评论是否可以做一些事情来改进这个脚本,因为我学习 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-------------------------------------------------------------------------------------
于 2012-09-13T01:04:25.267 回答
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    
于 2012-09-12T10:50:54.583 回答