How to config an automatic backup of an Ubuntu server to an USB external hard disk
First plug in the disk to an usb port so that Linux mount it.
I read from https://help.ubuntu.com/community/RenameUSBDrive
"Labeled devices that automount will be mounted in the /media directory using their label as the mount point, /media/<label>. ex: /media/my_external"
The usb drive is auto mounted an I find out the device using:
df
or find out the device descriptor with
mount
I see /dev/sdc1 on /media/VERBATIM type vfat (rw)
anyway I can identify my device also if not mounted with
fdisk -l
My device is /dev/sdc1 and the label is VERBATIM
The USB disk is a VFAT filesystem, and I want to keep it that way .
The backup will be performed by sbackupd, it will do a full backup every 21 days and daily it will do an incremental backup.
Unfortunately, if another external disk is plugged before the mine, the /dev/sdc1 will be another FS of another disk.
This will happen especially if You do not change the label to the disk
The problem is to assure that the usb-disk (Vfat Fs) is mounted correctly before the backup in the right place.
Unfortunately VFAT do not support UUID, so it's not possibile to verify if a Vfat FS is mounted via the "ls -l /dev/disk/by-uuid" command.
Sure We can use the /etc/mtab, but it's possible the fs is mounted in the wrong position due to USB plug-in mechanism.
The problem is that /dev/sdc1 it's not necessary the right disk , especially with a label like "verbatim"
I should change the label of the disk to something unique so that to assure sbackupd copy the data in the right place.
(In theory I should also implement a check and mount every available disk and verify it But I will not do here !)
Below http://embraceubuntu.com/2006/03/01/editing-fat32-partition-labels-using-mtools/
I assure a label to the disk
# Install mtools:
sudo apt-get install mtools
#change the label to the disk ( must it be mounted before?)
mlabel -i /dev/sdc1 -s ::SERVERCOPY
Now We should modify /etc/mtools.conf to specify a windows-like drive letter:
echo "" >> /etc/mtools.conf
echo "# # backup USB disk" >> /etc/mtools.conf
echo "drive p: file=\"/dev/sdc1\"" >> /etc/mtools.conf
echo "" >> /etc/mtools.conf
The disk is p:
So now verify again the label of the disk
mlabel -s p:
Volume label is SERVERCOPY
Ok, You should do it, I will not do beacouse I have already configured SBACKUP to copy data in /media/VERBATIM.
So, form now on, please use Your disk label instead of VERBATIM
Now let do the script that will verify /dev/sdc1 is mounted on /media/SERVERCOPY or VERBATIM or whatever is Your disk label.
mount /dev/sdc1 /media/VERBATIM
Now let's do a script that verify all conditions and start sbackup notice I'm poor in bash
# --------------------- cut the code from here
#!/bin/bash
# Loris Palmerini 2009 - copyright http://www.mozilla.org/MPL/MPL-1.1.html
# This bash script verify that a certain VFat filesystem is mounted in a certain mount point
# Vfat Fs have a label, and on Ubuntu usb disk it's mount on /media/"label"
#
# So the mounting is verified also against the label and umounted if mounted elsewhere
# if it's not possible to mount the FS correctly, send an email to admin
# if correctly mounted start sbackup
# if sbackup is not present , send an email
mountpoint="/media/VERBATIM"; # sbackup is configured for that destination
deviceid="/dev/sdc1";
label="SERVERCOPY";
# the "date" of today in form of YYmmDDhhMMss
todayis=$(date +%Y%m%d%H%M%S) # See date command
# script to send simple email from here http://theos.in/shell-scripting/send-mail-bash-script/
SUBJECT="Backup filed to mount the device"
FROMEMAIL="server@yourdomain.info"
DESTEMAIL="youremail@adminsite.info"
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "Error in mounting backup FS "$deviceid" on "$mountpoint" with volume label "$label > $EMAILMESSAGE
echo "Today is "$todayis" . Please verify that the unit is properly connected or backup will not be done" >> $EMAILMESSAGE
# umount device
if grep $deviceid /etc/mtab > /dev/null 2>&1; then
umount $deviceid
fi
# umount any device in the mount point
if grep $mountpoint /etc/mtab > /dev/null 2>&1; then
umount $mountpoint
fi
# umount the fs if mounted automatically
if grep $label /etc/mtab > /dev/null 2>&1; then
umount $label
fi
# mount the device on the rigth place
if ! mount -v -t vfat $deviceid $mountpoint > /dev/null 2>&1; then
# echo "not able to mount - send an email to admin"
# send an email using /bin/mail
/usr/sbin/sendmail -f $FROMEMAIL $DESTEMAIL < $EMAILMESSAGE
exit 1
fi
# now verify the label of the mounted disk
commandstring="mlabel -s p:"
X=$($commandstring)
Y=" Volume label is "$label" "
if [ "$X" != "$Y" ]; then
echo "not able to mount volume label "$label" send an email to admin"
# send an email
/usr/sbin/sendmail -f $FROMEMAIL $DESTEMAIL < $EMAILMESSAGE
exit 1
fi
# Let's verify if sbackup is installed
if [ -x /usr/sbin/sbackupd ]; then
/usr/sbin/sbackupd
else
echo "sbackup not found - send an email using sendmail"
EMAILMESSAGE="The sbackup utility is not properly installed.Please install it"
# /usr/sbin/sendmail -f $FROMEMAIL $DESTEMAIL < $EMAILMESSAGE
exit 1
fi
# --------------- END OF SCRIPT CUT TO HERE
Now , save the file in the root directory , suppose as the name mount_and_sbackup.sh
You can change the /etc/cron.d/sbackup file that way
0 3 * * * root if [ -x /root/mount_and_sbackup.sh ]; then /root/mount_and_sbackup; fi;
-------------------------- Loris Palmerini
How-to reference
Little guide to bash
http://www.panix.com/~elflord/unix/bash-tute.html
on exit status
http://tldp.org/LDP/abs/html/exit-status.html
concerning the labelling of a Vfat and fat32 disk
https://help.ubuntu.com/community/RenameUSBDrive
http://embraceubuntu.com/2006/03/01/editing-fat32-partition-labels-using-mtools/
Mi piace:
Mi piace Caricamento...