1

我写了一个小的 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

现在我需要一些关于以下主题的帮助:

  1. 检查是否存在共享、挂载点等条件(制作防呆脚本)
  2. mount 命令 - 是否正确,我是否给予正确的权限?
  3. 如果只有 root 可以看到备份文件,是否有比用户的主目录更好的位置?
  4. 您认为集成其他文件系统的备份是否也有帮助?
  5. 尽管我有一个千兆网络系统,但备份相当慢(大约 13mb/s)——这可能是因为 rsync 的 ssh 加密?安装共享的 linux 系统具有 pci sata 控制器和旧主板、1gb ram 和 athlon xp 2400+。速度慢还有其他原因吗?

如果您有更多可以在这里解决的主题 - 欢迎发布它们。我有兴趣 =)

干杯

-Blackjacx

4

1 回答 1

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 循环之前创建此目录,否则您将为备份的每个目录创建和销毁它。

于 2011-04-11T22:01:33.787 回答