如何使用 Backup.sh 脚本备份多个文件夹和文件

如何使用 Backup.sh 脚本备份多个文件夹和文件

我正在尝试创建一个在登录或启动时运行的脚本,用于备份某些特定文件夹和文件。我只能备份“ # What to Backup.”文件夹和文件位置的最后一行。

我可以将其中任何一个位置移动到 下的最后一行# What to Backup.,这就是将备份到存档的所有内容。我是否缺少另一个命令才能完成不同文件夹和文件的多个备份,或者我是否必须为backup.sh要备份的每个文件/文件夹创建一个?

如果我确实必须创建独立的backup(NAME).sh脚本,那么是否必须在每个正在运行的脚本之间添加某种类型的延迟?

#!/bin/bash
####################################
#
# Backup script.
#
####################################

# What to backup. 
backup_files="/home/wyzer/.config/google-chrome/Default/Local*/kbmfpngjjgdllneeigpgjifpgocmfgmb"
backup_files="/home/wyzer/.config/google-chrome/Default/Favicons*"
backup_files="/home/wyzer/.config/google-chrome/Default/Favicons-journal*"
backup_files="/home/wyzer/.config/google-chrome/Default/Google*.png"


# Where to backup to.
dest="/home/wyzer/Downloads/Scripts/Test_Folder"

# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-TEST-$day.tgz"

# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest

感谢 Dorian 提供的一些非常有用的信息,让我能够弄清楚如何备份我想要的文件。结果我只需要通知 TAR 需要备份哪个文件夹。这是我现在的脚本。

#!/bin/bash
####################################
#
# Backup script.
#
####################################

# What to backup. 
backup_files1="/home/wyzer/.config/google-chrome/Default/Local*/kbmfpngjjgdllneeigpgjifpgocmfgmb"
backup_files2="/home/wyzer/.config/google-chrome/Default/Favicons"
backup_files3="/home/wyzer/.config/google-chrome/Default/Google*.png"
backup_files4="/home/wyzer/.config/google-chrome/Default/Favicons-journal"

# Where to backup to.
dest="/home/wyzer/Downloads/Scripts/Test_Folder"

# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"

# Print start status message.
echo "Backing up $backup_files1 to $dest/$archive_file"
echo "Backing up $backup_files2 to $dest/$archive_file"
echo "Backing up $backup_files3 to $dest/$archive_file"
echo "Backing up $backup_files4 to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files1 $backup_files2 $backup_files3 $backup_files4

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest

答案1

这是因为您不断地覆盖的值backup_files,所以只有最后一个值存储在变量中。

您需要将其制成一个数组,然后循环遍历数组中的每个项目。

dest="/home/wyzer/Downloads/Scripts/Test_Folder"

#The line below will store multiple folders in an array (files)
files=( "/folder1" "/folder2" "/home/username/anotherfolder" )
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-TEST-$day.tgz"


for i in "${files[@]}"  # For every line in FILES
do # Do the following, starting with the first one:
    # Print start status message.
    echo "Backing up $i to $dest/$archive_file"  # Here, $i will be the first directory
    date
    echo

    # Backup the files using tar.
    tar czf $dest/$archive_file $i # Again, $i is the directory    
done # Stop here and do the loop again with the next item in FILES

# Print end status message.
echo
echo "Backup finished"

答案2

以下是代码查找多个文件/文件夹的方式,我提供的脚本的关键是告知 TAR 如何“解析”它正在备份的文件夹。

只需添加更多,如果您想查看它是如何备份的,backup_filesX=请不要忘记添加。echo "Backing up $backup_filesX to..

当然,另一个关键点是确保将 TAR 行添加到# Backup the files using tar.1附加$backup_filesX命令中。

再次感谢 Dorian 的帮助。非常感谢。

#!/bin/bash
####################################
#
# Backup script.
#
####################################

# What to backup. 
backup_files1="/home/wyzer/.config/google-chrome/Default/Local*/kbmfpngjjgdllneeigpgjifpgocmfgmb"
backup_files2="/home/wyzer/.config/google-chrome/Default/Favicons"
backup_files3="/home/wyzer/.config/google-chrome/Default/Google*.png"
backup_files4="/home/wyzer/.config/google-chrome/Default/Favicons-journal"

# Where to backup to.
dest="/home/wyzer/Downloads/Scripts/Test_Folder"

# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"

# Print start status message.
echo "Backing up $backup_files1 to $dest/$archive_file"
echo "Backing up $backup_files2 to $dest/$archive_file"
echo "Backing up $backup_files3 to $dest/$archive_file"
echo "Backing up $backup_files4 to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files1 $backup_files2 $backup_files3 $backup_files4

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest

相关内容