我有一个备份脚本可以完成这项工作,但需要很长时间才能完成,因为它会将用户目录逐一针对 DEV_Servers.txt 中的服务器列表进行压缩。
有没有一种方法可以同时对所有服务器列表运行 tar 命令,而不是逐一浏览列表?
#!/bin/ksh
print "==========================================="
print "Taking backup of Users dir"
print "==========================================="
serverList="DEV_Servers.txt"
while read -r server;
do
print
print "==========================================="
print "Taking Backup of Users:" $server
ssh -o StrictHostKeyChecking=no -n $server "cd /opt/test/ ; tar -zcvf 'Users$(date '+%Y%m%d').tar.gz' Users"
done < "$serverList"
这是一个 Bash 脚本,它从文件中读取目录列表并为每个目录创建备份。该脚本将每个目录压缩为一个
.tar.gz
文件并将备份存储在指定的备份目录中。
backup_dirs.sh
#!/bin/bash
# Variables
DIR_LIST="directories.txt" # File containing list of directories to back up
BACKUP_DIR="backup" # Directory to store backups
DATE=$(date +%Y-%m-%d) # Current date for timestamping backups
# Create the backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Check if the list file exists
if [[ ! -f "$DIR_LIST" ]]; then
echo "Error: File $DIR_LIST does not exist!"
exit 1
fi
# Backup each directory listed in DIR_LIST
while IFS= read -r DIR; do
# Skip empty lines or commented lines
[[ -z "$DIR" || "$DIR" =~ ^# ]] && continue
# Check if the directory exists
if [[ -d "$DIR" ]]; then
# Get the base name of the directory
DIR_NAME=$(basename "$DIR")
# Create the backup file name
BACKUP_FILE="$BACKUP_DIR/${DIR_NAME}_${DATE}.tar.gz"
# Create the tarball
tar -czf "$BACKUP_FILE" -C "$(dirname "$DIR")" "$DIR_NAME"
echo "Backup of $DIR completed: $BACKUP_FILE"
else
echo "Warning: Directory $DIR does not exist. Skipping."
fi
done < "$DIR_LIST"
echo "Backup process completed."