Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I have a backup script that runs every second night to shutdown a database server task - mount a CIFS network drive, backup the databases to the mounted drive and then restart the server again on completion.
I mount the drive in the script using:
mount -t cifs -o username=someuser,password=somepassword,//IP_Address/backupfolder /mnt/backupfolder
It all works fine but each time the script runs the CIFS drive still appears to be mounted but it re-mounts the drive anyway and adds another entry to the mtab, I only noticed when I tried to umount it and it didn't appear to un-mount. I checked the mtab and noticed there was half a dozen entries for the same mount so I had to umount it 6 times to get it to actually un-mount?
Doesn't the mount have a timeout period or do I need to call a umount to remove the drive? Why does mount add duplicate entries to mtab instead of realizing its already mounted?
Umm ... yes. You do. Either that or turn off the machine. Or, you could check to see if the mount is already in place, and skip the remount. If your mount point is an empty directory, this should be an easy test.
Actually, I think the best option would be to check for files in an empty mount directory, and skip a mount that is already completed.
Ok, thanks, that makes sense. Just wasnt sure exactly how mount behaved.
I can probably add a line to the script to check if the drive is already mounted. But hypothetically - what happens if the machine which the remote drive is on is rebooted whilst the drive is still mounted? Will the mount still be valid when it back online? Or will I need to remount it and if so will I then end up with duplicate entries in mtab?
Sorry for all the questions - just trying to prevent any possible issues with the backup script.
In your script, check the rtn code of each mount/umount cmd ie
Code:
mount blah.....
if [[ $? -eq 0 ]]
then
# all ok; do something
else
# not ok; something else
fi
Same for umount. Failsafe is to do a umount at the start anyway and ignore the rtn code.
You'll get a fail code iirc if the umount fails eg mount wasn't there in the first place.
Ok I have reviewed the script, here are my additions to make sure the network drive is available when the backup runs:
Code:
#check if the backup location is accessible - un-mount existing mount and then try and remount
umount -f /mnt/backupfolder
mount -t cifs //IP_Address/backupfolder /mnt/backupfolder -o credentials=/root/.credentials>> /var/log/backup.log 2>&1
if [[ $? -eq 0 ]]
then
# backup location mount successful
#stop the Server
/etc/init.d/domino stop 1>> /var/log/backup.log 2>&1
sleep 10
tar -cvzf /mnt/backupfolder/$fname /data 1>> /var/log/backup.log 2>&1
#We start Domino again
/etc/init.d/domino start 1>> /var/log/backup.log 2>&1
echo "`date`: The backup has finished" >> /var/log/backup.log
else
# mount network backup location failed
echo "`date`: Backup drive not accessible - mounting backup location failed!!" >> /var/log/backup.log
fi
I am very new to shell scripting so excuse the basic questions! See any problems with this? Should I pause between umount and mount commands?
I may also make it so an email is sent to me if the mount fails. The network folder is on a NAS device which has been known to drop off the network at times - hence the reason I'm going to all the trouble of making sure its still there before shutting down the server and doing the backup!
Looks ok to me.
I'd add a comment at the top explaining why you're not checking the rtn code for umount.
You could add a
sleep X # X<5 imho
after the umount; prob not needed though.
I'd add the email for failure.
Hope you defined $fname further up.
Personally, for prod code I'd check the rtn codes for service stop/start & tar cmd. You never know.
Basically, write a generic bash fn at the top of the prog and pass cmd and rtn code to it. Let fn check success/fail and email on fail.
To avoid multiple redirects to 'var/log/backup.log 2>&1', do it outside your prog thus:
mybackup.sh <params if needed> >var/log/backup.log 2>&1
If this is going in cron , use absolute full paths to all cmds and files.
cron env is minimal; also better security for your data.
Ok, thanks, that makes sense. Just wasnt sure exactly how mount behaved.
I can probably add a line to the script to check if the drive is already mounted. But hypothetically - what happens if the machine which the remote drive is on is rebooted whilst the drive is still mounted? Will the mount still be valid when it back online? Or will I need to remount it and if so will I then end up with duplicate entries in mtab?
Sorry for all the questions - just trying to prevent any possible issues with the backup script.
Okay, I hadn't considered that possibility. Do this instead:
1. Mount the remote drive.
2. Perform the required activity.
3. Perform a sync to make sure all writes are committed.
Great thanks for the tips guys, my script is starting to become a lot more robust!
Yes I defined $fname earlier in the script, the name of the backup file changes dependent on which day of the week the script is run, that way I have a few copies of the backup at various points over the week.
lutusp - how would I perform the sync you mention in step 3)?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.