简体   繁体   English

Bash脚本用于备份配置文件中指定的多个目录

[英]Bash script to backup multiple directories specified in config file

I'm having some difficulties with this. 我遇到了一些困难。 Basically, for work I need a bash script that backs up a variable number of directories that are stored in a config file. 基本上,对于工作,我需要一个bash脚本来备份存储在配置文件中的可变数量的目录。

I'm sure I need to import the list from the config file and just use a loop to copy all the directories across. 我确定我需要从配置文件中导入列表,只需使用循环来复制所有目录。 I have it working for a single directory. 我让它适用于单个目录。 My code is below. 我的代码如下。 I've cut it down to a minimum. 我把它减少到最低限度。

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

The config file is as follows 配置文件如下

SOURCE=path
DESTINATION=path2
BACKUPFOLDERNAME=/laptopBackup

My question is what is the best approach to do this task. 我的问题是执行此任务的最佳方法是什么。 ie how should I format the config file to import a variable amount of paths to an array? 即如何格式化配置文件以将可变数量的路径导入数组? or is there a better way of doing this? 或者有更好的方法吗?

I'd personally do it slightly differently and have my configuration file more of a "control file". 我个人会略有不同,让我的配置文件更像是一个“控制文件”。 For example: 例如:

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

etc.. 1 line per mount, 3 fields per line (source, destination, backupfoldername) 等。每行1行,每行3个字段(源,目的地,backupfoldername)

Then use something like : 然后使用类似的东西:

while read SOURCE DESTINATION BACKUPFOLDERNAME
do

<stuff>

done < ${configfile}

(removed the cat so as not to shame myself further :( ) (取出猫,以免进一步羞辱自己:()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM