What's new

CIFS folder sync to Router attached USB Disk (script)

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Speedy1205

Regular Contributor
Hi Guys,

I have a NAS which has just one Disk inside which is a bit bad. When the Disk is gone, all data is gone as well. So I tried to create a script which do the mounts and sync operations by using rsync. The script checks and mount the cifs on the router so the rsync can sync the files from the CIFS mount point to the USB disk mount point.

Code:
#!/bin/bash

datestr=$(date +%Y%m%d_%H%M%S)
# date used to add to the log file (append) so all the logs will be collected with the date and time no log overwriting....

nas=/tmp/mnt/ICY-BOX
# actual mount path for the cifs share we want to sync to the disk connected to the rooter (Source in this case CIFS sahre)

backup=/tmp/mnt/ESB-BACKUP/ESB-BACKUP/ICYBOX
# The Backup Destination which we use to SYNC the data from the remote share

logs=/tmp/mnt/ESB-BACKUP/ESB-BACKUP/ICYBOX/Logs
# directory where the sync logs will be saved wit the current time stamp

mount=/tmp/mnt/ESB-BACKUP
# Mount point where the destination USB disk will get mounted after its plugged in to the router

script=/tmp/mnt/ESB-TB/TB-DISK/Scripts/sync_icy_box.sh
if [ ! -d "$nas" ]; then
mkdir $nas
# If we can not find the folder /tmp/mnt/ICY-BOX ($nas) we will created it after check if it exists
fi

if grep $nas /etc/mtab &>/dev/null; then
grep $nas /etc/mtab | awk '{print "NAS is already mounted on " $2}'
else
/bin/mount -t cifs -o username='user',password='password' "//IP_CIFS/FamilyLibrary" /mnt/ICY-BOX
fi
# checking if the remote cifs / samba share is already mounted. If it is mounted than we just displaying a message. If it isn't mounted than we are mounting it with the command above.

if grep $mount /etc/mtab &>/dev/null; then
grep $mount /etc/mtab | awk '{print "USB Disk is mounted on " $2}'
else
echo $datestr "USB disk for SYNC operation not mounted or connected to the Router" >> /tmp/syslog.log
echo $datestr "USB disk for SYNC operation not mounted or connected to the Router" >> /tmp/syslog.log
echo $datestr "USB disk for SYNC operation not mounted or connected to the Router" >> /tmp/syslog.log
echo $datestr "USB disk for SYNC operation not mounted or connected to the Router" >> /tmp/syslog.log
echo $datestr "USB disk for SYNC operation not mounted or connected to the Router" >> /tmp/syslog.log
exit
fi
#Checking if the USB DISK (destination) is mounted at the expected mount point. If the Disk will not be mounted there, we are appending Error to syslog and exit the script.

sleep 5

rsync -rtvu $nas/Printer $backup > $logs/printer_sync_$datestr
rsync -rtvu $nas/FamilyVideos/Video-Privat $backup > $logs/videos_private_sync_$datestr
#rsync -rtvu $nas/Business $backup > $logs/Business_sync_$datestr
rsync -rtvu $nas/FamilyPhotos $backup > $logs/FamilyPhotos_sync_$datestr
rsync -rtvu $nas/FamilyMusik $backup > $logs/FamilyMusik_sync_$datestr
# Syncing the mounted cifs shared folder to the local USB Disk connected to the Router. Used -u to Update the folder Only which will do in the next run only incremental data movement.

/bin/chmod -R 777 $backup
/bin/chown -R admin:root $backup

# sleep 4h
# exit && $script

I have there some problems in repeating the script. I would like that it will sleep for 4 hours and than start the script from the beginning.

Also I would like to know from the script experts if there are some errors in the script, or some what could give me some issues.

Thanks a lot !
 

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top