使用 rsync 进行 samba 共享的 bash 备份脚本的有用建议
我编写了一个小型 bash (4) 脚本来备份 Windows 电脑上的共享。目前我只备份一个共享,并且备份仅对 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
现在我需要一些有关以下主题的帮助:
- 检查共享是否存在、安装点是否存在等条件(以制作一个防呆脚本)
- 安装命令 - 它是否正确,我做吗?给予正确的权限?
- 如果只有 root 可以看到备份文件,是否有比用户的主目录更好的位置?
- 您认为集成其他文件系统的备份也会有帮助吗?
- 尽管我有千兆网络系统,但备份相当慢(大约13mb/s) - 可能这是因为rsync的ssh加密?安装共享的 Linux 系统有一个 pci sata 控制器和一个旧主板、1gb 内存和 athlon xp 2400+。速度缓慢是否还有其他原因?
如果您有更多可以在这里讨论的主题 - 欢迎发布它们。我有兴趣 =)
干杯
-Blackjacx
I have written a small bash (4) script to backup shares from my windows pc's. Currently I backup only one share and the backup is only visible to root. Please give me some hints for improvements of that piece of code:
#!/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
Now I need some help concerning following topics:
- Checking if conditions like share existence, mount point existence (to make a fool proof script)
- The mount command - is it correct, do I give the correct permissions?
- Is there a better place for the backup files than the home directory of a user if only root can see that files?
- Do you think it would be helpful to integrate the backup of other file systems too?
- The backup is rather slow (around 13mb/s) although I have a gigabit network system - possibly this is because of the ssh encryption of rsync? The linux system, where the share is mounted on, has a pci sata controller and an old mainboard, 1gb ram and an athlon xp 2400+. Could there be other reasons for the slowness?
If you have more topics that can be addressed here - be welcome to post them. I'm interested =)
Cheers
-Blackjacx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1) 您可以执行很多错误检查来使此脚本更加健壮:检查外部可执行文件(rsync、id、日期、目录名等)是否存在和执行状态,检查 rsync 的输出(使用 if [ 0 -ne $?]; 然后),ping 远程计算机以确保其位于网络上,检查以确保您正在执行备份的用户在本地计算机上有主目录,检查以确保目标目录有足够的空间用于 rsync 等。
2) mount 命令看起来没问题,您想尝试以只读方式挂载吗?
3) 取决于用户数量和备份大小。对于小型备份,主目录可能没问题,但如果备份很大,特别是如果您可能耗尽磁盘空间,那么专用的备份位置会很好。
4) 取决于备份的目的。根据我的经验,人们备份五种内容:用户数据、系统数据、用户配置、系统配置、整个磁盘或文件系统。是否有单独的系统任务来备份系统数据和配置?
5) 备份时还有哪些其他任务正在运行?如果备份在凌晨 1 点自动运行,则可能会同时安排其他系统密集型任务。其他类型数据的典型吞吐率是多少?
您正在对非数组变量执行数组检查。
使用变量来指定远程计算机。
这样你就可以
如果 rsync_src 仅用于此 mackup,你可能会尝试
,我会在 for 循环之前创建此目录,否则你将为备份的每个目录创建和销毁它。
1) There is a lot of error checking you can do to make this script more robust: check for existance and execute status of external executables (rsync, id, date, dirname, etc.), check the output of rsync (with if [ 0 -ne $?]; then), ping the remote machine to make sure it is on the network, check to make sure thet user you are doing the backup for has a home directory on the local machine, check to make sure the destination directory has enough space for the rsync, etc.
2) mount command looks ok, did you want to try to mount as read only?
3) Depends on the number of users and the size of the backup. For small backups the home directory is likely ok, but if the backups are large, especialy if you might run out of disk space, a dedicated backup location would be nice.
4) Depends on what the purpose of the backup is. In my experience there are five kinds of things that people backup: user data, system data, user configurations, system configurations, entire disks or filesystems. Is there a seperate system task that backs-up the system data and configurations?
5) What other tasks are running while the backup takes place? If the backup is running automated at say 1 AM, there may be other system intensive tasks scheduled for the same time. What is your typical throughput rate for other kinds of data?
You are performing an array check on non-array variables.
Use a variable to specify the remote machine.
that way you can
If rsync_src is only used for this mackup, you might wat to try
and I would make this dir before the for loop otherwise you are creating and destroying it for every directory you back up.