我想知道是否可以自动挂载并启动 rsync 来同步 USB 驱动器和目录?具体来说,我想复制驱动器可以容纳的尽可能多的数据,并且仅在需要空间时删除最旧的文件。我假设我会用脚本做这样的事情,但我的问题是,从哪里开始!
答案1
答案2
以下是检查已安装的 USB 硬盘的备份作业的核心。很容易将其修改为其他用途,然后继续通过 cron 调用它,或者在关键部分周围使用睡眠循环在后台运行它。此脚本允许您指定可能找到 USB 磁盘的位置 (sdd),但很容易将其修改为通过驱动器 ID 查找 USB 棒。
备份源是 NFS 挂载驱动器,但显然可以根据需要进行更改/删除。
中间有一个for循环,指定需要备份的位置
# V1.01
# Uses variable for usbdisk partition
started=`date`
usbdisk='sdd1'
if [ $(mount | grep -c /mnt/usbdisk) != 1 ]; then
echo "USB disk is not mounted - trying to mount..."
mount /dev/$usbdisk /mnt/usbdisk
if [ $(mount | grep -c /mnt/usbdisk) != 1 ]; then
echo "USB disk still not mounted - alerting admins and quitting"
echo "USB disk is not mounted on TUX9 so backup cannot continue" | mail -s "TUX9 backup problem" [email protected]
exit 1
fi
fi
if [ $(mount | grep -c /mnt/nas) != 1 ]; then
echo "NAS NFS mount is not present - trying to mount..."
mount -t nfs -o proto=tcp,port=2049 mynas3:nas /mnt/nas
if [ $(mount | grep -c /mnt/nas) != 1 ]; then
echo "NAS NFS mount is still not present - alerting admins and quitting"
echo "NAS NFS mount is not present on TUX9 so backup cannot continue" | mail -s "ASLTUX9 backup problem" [email protected]
exit 1
fi
fi
echo "All mount points present - backup starting..."
for i in /boot / /home /var /mnt/nas
# for testing for i in /boot
do
if [ $i = "/" ]; then
rsync -axv --delete [email protected]:$i /mnt/usbdisk/backup/root
elif [ $i = "/mnt/nas" ]; then
rsync -axv --delete $i/ /mnt/usbdisk/backup/nas
else
rsync -axv --delete [email protected]:$i/ /mnt/usbdisk/backup$i
fi
done
echo done, started at $started, finished at `date`
echo "Backup of NAS3 and TUX8 to USB disk on TUX9 done. Started at $started, finished at `date`" | mail -s "TUX9 backup completed" [email protected]