{"id":613,"date":"2008-09-23T21:02:08","date_gmt":"2008-09-23T20:02:08","guid":{"rendered":"http:\/\/www.palmerini.net\/blog\/?p=613"},"modified":"2009-09-23T21:04:58","modified_gmt":"2009-09-23T20:04:58","slug":"how-to-config-an-automatic-backup-of-an-ubuntu-server-to-an-usb-external-hard-disk","status":"publish","type":"post","link":"https:\/\/palmerini.net\/blog\/how-to-config-an-automatic-backup-of-an-ubuntu-server-to-an-usb-external-hard-disk\/","title":{"rendered":"How to config an automatic backup of an Ubuntu server to an USB external hard disk"},"content":{"rendered":"<pre>How to config an automatic backup of an Ubuntu server to an USB external hard disk \r\n\r\nFirst plug in the disk to an usb port so that Linux mount it.\r\nI read from https:\/\/help.ubuntu.com\/community\/RenameUSBDrive\r\n\"Labeled devices that automount will be mounted in the \/media directory using their label as the mount point, \/media\/&lt;label&gt;. ex: \/media\/my_external\"\r\n\r\nThe usb drive is auto mounted an I find out the device using:\r\ndf\r\n\r\nor find out the device descriptor with\r\nmount\r\n\r\nI see \/dev\/sdc1 on \/media\/VERBATIM type vfat (rw)\r\n\r\nanyway I can identify my device also if not mounted with \r\nfdisk -l\r\n\r\nMy device is \/dev\/sdc1 and the label is VERBATIM\r\n\r\nThe USB disk is a VFAT filesystem, and I want to keep it that way .\r\nThe backup will be performed by sbackupd, it will do a full backup every 21 days and daily it will do an incremental backup.\r\n\r\nUnfortunately, if another external disk is plugged before the mine, the \/dev\/sdc1\u00a0 will be another FS of another disk.\r\nThis will happen especially if You do not change the label to the disk\r\n\r\nThe problem is to assure that the usb-disk (Vfat Fs) is mounted correctly before the backup in the right place.\r\nUnfortunately 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.\r\n\r\nSure We can use the \/etc\/mtab, but it's possible the fs is mounted in the wrong position due to USB plug-in mechanism.\r\nThe problem is that \/dev\/sdc1 it's not necessary the right disk , especially with a label like \"verbatim\" \r\nI should change the label of\u00a0 the disk to something unique so that\u00a0 to\u00a0 assure\u00a0 sbackupd copy\u00a0 the data in the right place.\r\n(In theory I should also implement a check and mount every available disk and verify it But I will not do here !)\r\n\r\nBelow http:\/\/embraceubuntu.com\/2006\/03\/01\/editing-fat32-partition-labels-using-mtools\/\r\n\r\n I\u00a0 assure a label to the disk\r\n\r\n# Install mtools:\r\nsudo apt-get install mtools\r\n\r\n#change the label to the disk ( must it be mounted before?)\r\nmlabel -i \/dev\/sdc1 -s ::SERVERCOPY\r\n\r\nNow We should modify\u00a0 \/etc\/mtools.conf to specify a windows-like drive letter:\r\n\r\necho \"\" &gt;&gt; \/etc\/mtools.conf\r\necho \"# # backup USB\u00a0 disk\" &gt;&gt; \/etc\/mtools.conf\r\necho \"drive p: file=\\\"\/dev\/sdc1\\\"\" &gt;&gt; \/etc\/mtools.conf\r\necho \"\" &gt;&gt; \/etc\/mtools.conf\r\n\r\nThe disk is p:\r\n\r\nSo now verify again the label of the disk\r\n\r\nmlabel -s p:\r\nVolume label is SERVERCOPY \r\n\r\nOk, You should do it, I will not do beacouse I have already configured SBACKUP to copy data in \/media\/VERBATIM.\r\n\r\nSo, form now on, please use Your disk label instead of VERBATIM\r\n\r\nNow let do the script that will verify \/dev\/sdc1 is mounted on \/media\/SERVERCOPY or VERBATIM or whatever is Your disk label.\r\n\r\nmount \/dev\/sdc1 \/media\/VERBATIM\r\n\r\nNow let's do a script that verify all conditions and start sbackup\u00a0 notice I'm poor in bash\r\n\r\n# --------------------- cut the code from here\r\n#!\/bin\/bash\r\n# Loris Palmerini 2009 - copyright http:\/\/www.mozilla.org\/MPL\/MPL-1.1.html\r\n# This bash script verify that a certain VFat filesystem is mounted in a certain mount point\r\n# Vfat Fs have a label, and on Ubuntu usb disk\u00a0 it's mount on \/media\/\"label\"\r\n# \r\n# So the mounting is verified also against the label and umounted if mounted elsewhere \r\n# if it's not possible to mount the FS correctly, send an email to admin\r\n# if correctly mounted start sbackup\r\n# if sbackup is not present , send an email\r\n\r\nmountpoint=\"\/media\/VERBATIM\"; # sbackup is configured for that destination\r\ndeviceid=\"\/dev\/sdc1\";\r\nlabel=\"SERVERCOPY\";\r\n\r\n# the \"date\" of today in form of YYmmDDhhMMss\r\ntodayis=$(date +%Y%m%d%H%M%S)\u00a0 # See date command\r\n\r\n# script to send simple email from here http:\/\/theos.in\/shell-scripting\/send-mail-bash-script\/\r\nSUBJECT=\"Backup filed to mount the device\"\r\nFROMEMAIL=\"server@yourdomain.info\"\r\nDESTEMAIL=\"youremail@adminsite.info\"\r\nEMAILMESSAGE=\"\/tmp\/emailmessage.txt\"\r\necho \"Error in mounting backup FS \"$deviceid\" on \"$mountpoint\" with volume label \"$label\u00a0\u00a0 &gt; $EMAILMESSAGE\r\necho \"Today is \"$todayis\" . Please verify that the unit is properly connected or backup will not be done\" &gt;&gt; $EMAILMESSAGE\r\n\r\n# umount device \r\nif grep $deviceid \/etc\/mtab &gt; \/dev\/null 2&gt;&amp;1; then\r\numount $deviceid\r\nfi \r\n# umount any device in the mount point \r\nif grep $mountpoint \/etc\/mtab &gt; \/dev\/null 2&gt;&amp;1; then\r\numount $mountpoint\r\nfi \r\n# umount the fs if mounted automatically\r\nif grep $label \/etc\/mtab &gt; \/dev\/null 2&gt;&amp;1; then\r\numount $label\r\nfi \r\n\r\n# mount the device on the rigth place\r\nif ! mount -v -t vfat $deviceid $mountpoint &gt; \/dev\/null 2&gt;&amp;1; then\r\n #\u00a0\u00a0 echo \"not able to mount -\u00a0 send an\u00a0 email to admin\"\r\n # send an email using \/bin\/mail\r\n \/usr\/sbin\/sendmail -f $FROMEMAIL $DESTEMAIL &lt; $EMAILMESSAGE\r\n exit 1 \r\nfi\r\n\r\n# now verify the label of the mounted disk \r\ncommandstring=\"mlabel -s p:\"\r\nX=$($commandstring)\r\nY=\" Volume label is \"$label\" \"\r\nif [ \"$X\" != \"$Y\" ]; then\r\n echo \"not able to mount volume label \"$label\" send an\u00a0 email to admin\"\r\n # send an email \r\n \/usr\/sbin\/sendmail -f $FROMEMAIL $DESTEMAIL &lt; $EMAILMESSAGE\r\n exit 1 \r\nfi\r\n# Let's verify if sbackup is installed\r\nif [ -x \/usr\/sbin\/sbackupd ]; then \r\n \/usr\/sbin\/sbackupd \r\nelse \r\n echo \"sbackup not found -\u00a0 send an email using sendmail\"\r\n EMAILMESSAGE=\"The sbackup utility is not properly installed.Please install it\"\r\n#\u00a0\u00a0\u00a0 \/usr\/sbin\/sendmail -f $FROMEMAIL $DESTEMAIL &lt; $EMAILMESSAGE\r\n exit 1\r\nfi\r\n\r\n# --------------- END OF SCRIPT CUT TO HERE\r\n\r\nNow , save the file in the root directory , suppose as the name mount_and_sbackup.sh\r\nYou can change the \/etc\/cron.d\/sbackup file that way\r\n\r\n0 3 * * *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 root\u00a0\u00a0\u00a0 if [ -x \/root\/mount_and_sbackup.sh ]; then \/root\/mount_and_sbackup; fi;\r\n\r\n-------------------------- Loris Palmerini\r\nHow-to reference\r\nLittle guide to bash\r\nhttp:\/\/www.panix.com\/~elflord\/unix\/bash-tute.html\r\non exit status\r\nhttp:\/\/tldp.org\/LDP\/abs\/html\/exit-status.html\r\nconcerning the labelling of a Vfat and fat32 disk\r\nhttps:\/\/help.ubuntu.com\/community\/RenameUSBDrive \r\nhttp:\/\/embraceubuntu.com\/2006\/03\/01\/editing-fat32-partition-labels-using-mtools\/<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;Labeled devices that automount will be mounted in the \/media directory using their label as the mount point, \/media\/&lt;label&gt;. ex: \/media\/my_external&#8221; [&hellip;]<\/p>\n","protected":false},"author":292,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[9],"tags":[],"class_list":["post-613","post","type-post","status-publish","format-standard","hentry","category-informatica","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"builder_content":"","_links":{"self":[{"href":"https:\/\/palmerini.net\/blog\/wp-json\/wp\/v2\/posts\/613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/palmerini.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/palmerini.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/palmerini.net\/blog\/wp-json\/wp\/v2\/users\/292"}],"replies":[{"embeddable":true,"href":"https:\/\/palmerini.net\/blog\/wp-json\/wp\/v2\/comments?post=613"}],"version-history":[{"count":0,"href":"https:\/\/palmerini.net\/blog\/wp-json\/wp\/v2\/posts\/613\/revisions"}],"wp:attachment":[{"href":"https:\/\/palmerini.net\/blog\/wp-json\/wp\/v2\/media?parent=613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/palmerini.net\/blog\/wp-json\/wp\/v2\/categories?post=613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/palmerini.net\/blog\/wp-json\/wp\/v2\/tags?post=613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}