Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 使用输入列表备份的Shell脚本_Linux_Bash_Shell_Ubuntu_Backup - Fatal编程技术网

Linux 使用输入列表备份的Shell脚本

Linux 使用输入列表备份的Shell脚本,linux,bash,shell,ubuntu,backup,Linux,Bash,Shell,Ubuntu,Backup,我正在构建一个脚本来备份linux中的文件和目录。 这些备份存储在地图存档中 现在我在我的源代码中使用了一个特定的目录,但是我有 input.txt包含以下目录: /Downloads /etc/var 或者也可以使用特定的文件 /etc/log/test.txt 使用/Downloads或/etc/var,它还必须备份这些目录的子目录。例如,./Downloads还有3个其他目录/dir1、/dir2、/dir3,这些目录也需要存档 如何使用该文件input.txt作为源文件? 对于整个i

我正在构建一个脚本来备份linux中的文件和目录。 这些备份存储在地图存档中

现在我在我的源代码中使用了一个特定的目录,但是我有

input.txt包含以下目录:

/Downloads
/etc/var
或者也可以使用特定的文件

/etc/log/test.txt
使用/Downloads或/etc/var,它还必须备份这些目录的子目录。例如,./Downloads还有3个其他目录/dir1、/dir2、/dir3,这些目录也需要存档

如何使用该文件input.txt作为源文件? 对于整个input.txt文件,我必须进行1次存档

#!/bin/sh

source="/Downloads"     
archive="example.tgz"       
dest="Archive"              
path="input.txt"                

tar czf $dest/$archive $source

ls $dest

#this is how i read my file
while read line
    do
        echo $line
done < $path
#/垃圾箱/垃圾箱
source=“/Downloads”
archive=“example.tgz”
dest=“存档”
path=“input.txt”
tar czf$dest/$archive$source
ls$dest
#这是我读文件的方式
读行时
做
回音$线
完成<$path

使用tar创建存档
-T
选项可用于从输入文件获取文件

input.txt:

/Downloads
/etc/var
/etc/log/test.txt
脚本:

#!/bin/sh

archive="example.tgz"       
dest="Archive"              
path="input.txt"                

tar czf $dest/$archive -T $path

您可以这样做:

#!/bin/bash

output_dir="/tmp/snapshots"
current_date="$( date +%F_%H-%M-%S )"
script_name="$( basename $0 )"

dirs=""

while IFS='' read -r line || [[ -n "$line" ]]; do
    # directory exists?
    if [ -d "$line" ] || [ -f "$line" ]; then
        dirs="$dirs $line"
    else
        echo "${script_name}: cannot access $line. No such directory. Skip"
    fi
done < "$1"

output_file="${output_dir}/${current_date}.tar.gz"

if [ -n "$dirs" ] ; then
    tar -czvf $output_file $dirs
    echo "${script_name}: done!"
    exit 0
else
    echo "${script_name}: operation failed. Cannot create an empty archive."
fi
./snapshot.sh input.txt

这比使用数组要好得多,对吗?那么-T会将所有输入文件添加到一个存档中吗?@linuxwar是的,这将创建一个存档。