1

Windows PC から共有をバックアップするための小さな bash (4) スクリプトを作成しました。現在、私は 1 つの共有のみをバックアップしており、バックアップはルートにのみ表示されます。そのコードを改善するためのヒントを教えてください。

#!/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. マウント コマンド - 正しいですか、正しいアクセス許可を与えていますか?
  3. root のみがそのファイルを参照できる場合、ユーザーのホーム ディレクトリよりもバックアップ ファイルを保存するのに適した場所はありますか?
  4. 他のファイル システムのバックアップも統合すると便利だと思いますか?
  5. ギガビットネットワークシステムを使用していますが、バックアップはかなり遅いです (約 13mb/s)。おそらくこれは、rsync の ssh 暗号化が原因でしょうか? 共有がマウントされている Linux システムには、pci sata コントローラーと古いメインボード、1 GB RAM、および athlon XP 2400+ があります。遅い理由は他にあるのでしょうか?

ここで対処できるトピックが他にもある場合は、投稿してください。興味があります =)

乾杯

-ブラックジャック

4

1 に答える 1

0

1) このスクリプトをより堅牢にするために実行できる多くのエラー チェックがあります。外部実行可能ファイル (rsync、id、date、dirname など) の存在と実行ステータスをチェックし、rsync の出力をチェックします (if [ 0 -ne $?]; 次に)、リモート マシンに ping を実行してネットワーク上にあることを確認し、バックアップを実行しているユーザーがローカル マシンにホーム ディレクトリを持っていることを確認します。ディレクトリには、rsync などに十分なスペースがあります。

2) マウント コマンドは問題ないようです。読み取り専用としてマウントしようとしましたか?

3) ユーザー数とバックアップのサイズによって異なります。小規模なバックアップの場合はホーム ディレクトリで問題ない可能性がありますが、バックアップが大規模な場合、特にディスク容量が不足する可能性がある場合は、専用のバックアップ場所が適しています。

4) バックアップの目的によって異なります。私の経験では、ユーザー データ、システム データ、ユーザー構成、システム構成、ディスク全体、またはファイル システムの 5 種類のデータをバックアップします。システム データと構成をバックアップする別のシステム タスクはありますか?

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 がこのマックアップにのみ使用されている場合は、試してみてください

mnt_dir="/mnt"
rsync_src=`mktemp -d -p $mnt_dir`

forループの前にこのディレクトリを作成します。そうしないと、バックアップするディレクトリごとに作成して破棄します。

于 2011-04-11T22:01:33.787 に答える