我已经编写了一个小的bash(4)脚本来备份我的windows pc中的共享。目前我只备份一个共享,备份只对root可见。请给我一些改进这段代码的提示:
#!/bin/bash
# Script to back up directories on several windows machines
# Permissions, owners, etc. are preserved (-av option)
# Only differences are submitted
# Execute this script from where you want
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Specify the current date for the log-file name
current_date=$(date +%Y-%m-%d_%H%M%S)
# Specify the path to a list of file patterns not included in backup
script_path=$(dirname $(readlink -f $0))
rsync_excludes=$script_path/rsync_exclude.patterns
# Specify mount/rsync options
credential_file="/root/.smbcredentials"
# Specify windows shares
smb_shares=( //192.168.0.100/myFiles )
# Specify the last path component of the directory name to backup shares
# content into
smb_share_ids=( "blacksmith" )
# Specify with trailing '/' to transfer only the dir content
rsync_src="/mnt/smb_backup_mount_point/"
rsync_dst_root=(~/BACKUPS)
# Check if all arrays have the same size
if [ "${#smb_shares[@]}" -ne "${#smb_share_ids[@]}" ]; then
echo "Please specify one id for each samba share!"
exit 1
fi
# Run foor loop to sync all specified shares
for (( i = 0 ; i < ${#smb_shares[@]} ; i++ ))
do
# Check if mount point already exists
echo -n "Checking if mount point exists ... "
if [ -d $rsync_src ]; then
echo "Exists, exit!"
exit 1
else
echo "No, create it"
mkdir $rsync_src
fi
# Try to mount share and perform rsync in case of success
echo -n "Try to mount ${smb_shares[$i]} to $rsync_src ... "
mount -t cifs ${smb_shares[$i]} $rsync_src -o credentials=/root/.smbcredentials,iocharset=utf8,uid=0,file_mode=0600,dir_mode=0600
if [ "$?" -eq "0" ]; then
echo "Success"
# Specify the log-file name
rsync_logfile="$rsync_dst_root/BUP_${smb_share_ids[$i]}_$current_date.log"
# Build rsync destination root
rsync_dst=( $rsync_dst_root"/"${smb_share_ids[$i]} )
# Check if rsync destination root already exists
echo -n "Check if rsync destination root already exists ... "
if [ -d $rsync_dst ]; then
echo "Exists"
else
echo "Does not exist, create it"
mkdir -p $rsync_dst
fi
# Run rsync process
# -av > archieve (preserve owner, permissions, etc.) and verbosity
# --stats > print a set of statistics showing the effectiveness of the rsync algorithm for your files
# --bwlimit=KBPS > transfer rate limit - 0 defines no limit
# --progress > show progress
# --delete > delete files in $DEST that have been deletet in $SOURCE
# --delete-after > delete files at the end of the file transfer on the receiving machine
# --delete-excluded > delete excluded files in $DEST
# --modify-window > files differ first after this modification time
# --log-file > save log file
# --exclude-from > exclude everything from within an exclude file
rsync -av --stats --bwlimit=0 --progress --delete --delete-after --delete-excluded --modify-window=2 --log-file=$rsync_logfile --exclude-from=$rsync_excludes $rsync_src $rsync_dst
fi
# Unmount samba share
echo -n "Unmount $rsync_src ... "
umount $rsync_src
[ "$?" -eq "0" ] && echo "Success"
# Delete mount point
echo -n "Delete $rsync_src ... "
rmdir $rsync_src
[ "$?" -eq "0" ] && echo "Success"
done
现在我需要一些关于以下主题的帮助:
如果您有更多可以在此处理的主题,欢迎发布。我很感兴趣=)
干杯
-Blackjacx
答案 0 :(得分:0)
1)你可以做很多错误检查,使这个脚本更健壮:检查外部可执行文件的存在和执行状态(rsync,id,date,dirname等),检查rsync的输出(使用if [0 -ne $?];然后),ping远程机器以确保它在网络上,检查以确保您正在进行备份的用户在本地计算机上有一个主目录,检查以使确保目标目录有足够的空间用于rsync等。
2)mount命令看起来没问题,你想尝试以只读方式挂载吗?
3)取决于用户数量和备份大小。对于小型备份,主目录可能没问题,但如果备份很大,特别是如果磁盘空间不足,那么专用备份位置就会很好。
4)取决于备份的目的是什么。根据我的经验,人们有五种备份方式:用户数据,系统数据,用户配置,系统配置,整个磁盘或文件系统。是否有单独的系统任务来备份系统数据和配置?
5)备份发生时还有哪些其他任务正在运行?如果备份在凌晨1点自动运行,则可能会同时安排其他系统密集型任务。您对其他类型数据的典型吞吐率是多少?
您正在对非数组变量执行数组检查。
使用变量指定远程计算机。
# Remote machine
declare remote_machine="192.168.0.100"
# Specify windows shares array
declare -a smb_shares=([0]="//$remote_machine/myFiles"
[1]="//$remote_machine/otherFiles" )
# Specify the last path component of the directory name to backup shares
# content into
declare -a smb_share_ids=( [1]="blacksmith" [2]="tanner")
你可以这样
ping -c 1 $remote_machine
if [ 0 -ne $? ] ; then
echo "ping on $remote_machine Failed, exiting..."
exit 1
fi
如果rsync_src仅用于此mackup,您可能会尝试
mnt_dir="/mnt"
rsync_src=`mktemp -d -p $mnt_dir`
我会在for循环之前创建这个目录,否则你会为你备份的每个目录创建并销毁它。