【问题标题】:Bash script to backup multiple directories specified in config file用于备份配置文件中指定的多个目录的 Bash 脚本
【发布时间】:2012-10-02 10:01:42
【问题描述】:

我在这方面遇到了一些困难。基本上,为了工作,我需要一个 bash 脚本来备份存储在配置文件中的可变数量的目录。

我确定我需要从配置文件中导入列表,然后使用循环复制所有目录。我让它适用于单个目录。我的代码如下。我已将其减少到最低限度。

#!/bin/sh
if [ ! -f ./backup.conf ]
then
echo "Configuration file not found. Exiting!!"
exit
fi
. ./backup.conf

unset PATH

# make sure we're running as root
if (( `$ID -u` != 0 )) ; then { $ECHO "Sorry, must be root.  Exiting..."; exit; } fi ;

# attempt to remount the RW mount point as RW; else abort
$MOUNT -o remount,rw $SOURCEFILE $DESTINATIONFOLDER ;
if (( $? )); then
{
$ECHO "snapshot: could not remount $DESTINATIONFOLDER readwrite";
exit;
}
fi ;

# step 2: create new backup folder:
$MKDIR $FULLPATH


**Loop should go here**
#copy source directories to backup folder
$RSYNC                              \
-va --delete --delete-excluded              \
--exclude-from="$EXCLUDES"              \
$SOURCEFILE $FULLPATH;

配置文件如下

SOURCE=path
DESTINATION=path2
BACKUPFOLDERNAME=/laptopBackup

我的问题是完成这项任务的最佳方法是什么。即我应该如何格式化配置文件以将可变数量的路径导入数组?还是有更好的方法?

【问题讨论】:

    标签: bash unix directory backup config


    【解决方案1】:

    我个人会稍有不同,让我的配置文件更像是一个“控制文件”。例如:

    /path       /path2     /laptopBackup
    /tmp        /test      /bigmachine
    

    等.. 每次装载 1 行,每行 3 个字段(源、目标、备份文件夹名称)

    然后使用类似的东西:

    while read SOURCE DESTINATION BACKUPFOLDERNAME
    do
    
    <stuff>
    
    done < ${configfile}
    

    (为了不让自己更丢脸,把猫拿走了:()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多