{"id":195,"date":"2019-12-30T16:00:00","date_gmt":"2019-12-30T15:00:00","guid":{"rendered":"http:\/\/fred151.net\/site\/?p=195"},"modified":"2021-03-29T17:23:13","modified_gmt":"2021-03-29T15:23:13","slug":"how-backup-files-to-a-specific-external-disk-hdd-automatically-when-it-is-connected","status":"publish","type":"post","link":"https:\/\/fred151.net\/site\/2019\/12\/30\/how-backup-files-to-a-specific-external-disk-hdd-automatically-when-it-is-connected\/","title":{"rendered":"How backup files to a specific external disk automatically when it is connected &#8211; [Linux]"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A strong backup policy provides to keep an offline (not connect to any kind of device) copy of own files, protecting your data against ransomware, device failure, etc. We can use an external Hard Disk Drive (HDD) to connected  every week to our server\/workstation and automatically backup files and send an email at the end of the operation. We can also decide which files can be copied and also on which hard disk. For example if you have a family server, each users can backup their files on own external hard disk in a simple way.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Requirements<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Linux server\/workstation (source of the backup)<\/li><li>Hard Disk Drive (don&#8217;t use Static Solid Drive (SDD) to backup files)<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Steps<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We need to get the serial number of our external hard disk. So start to connect the external device to out server\/workstation. We will use udevadm to get the information. Replace sd<strong>xy<\/strong> with your device.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># udevadm info -a \/dev\/sd<strong>xy<\/strong> | grep serial\nE: ID_SERIAL_SHORT=000001D91CC3<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s create a rule to trigger when the external disk will be connected.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># nano \/etc\/udev\/rules.d\/10-local.rules\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \nACTION==\"add\", ENV{DEVTYPE}==\"partition\", SUBSYSTEMS==\"usb\", KERNEL==\"sd?1\", ATTRS{serial}==\" <strong>000001D91CC3<\/strong>\", SYMLINK+=\"<strong>usbhddbackup<\/strong>\", RUN+=\"\/bin\/bash <strong>\/home\/BACKUP\/backupWrapper.sh<\/strong>\" <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Some explanation about the string parameters of the rule:<br>ENV{DEVTYPE}==\u201dpartition\u201d search valid partition<br>SUBSYSTEMS==\u201dusb\u201d search in USB devices<br>KERNEL==\u201dsd?1\u2033 = get the first partition (?=can be any letter)<br>ATTRS{serial} = <strong>serial of device<\/strong>, only if this device is found the rule is matched<br>ENV{DEVTYPE} = use this option to avoid udev exec twice the the action run.<br>SYMLINK = name of device, it will create a \/dev\/usbhddbackup useful to mount the device in the script<br>RUN = the script you have to exec, <strong>call a wrapper script because<\/strong> <strong>udev exec the process with a timeout<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># systemctl restart udev<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you have some trouble or just for debug look inside \/var\/log\/syslog.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ok go on, create the script wrapper called in the rule above:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># mkdir \/home\/BACKUP\n# cd \/home\/BACKUP\n# touch backupWrapper.sh \n# chmod +x backupWrapper.sh<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Edit the script adding just few lines.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># nano \/home\/BACKUP\/backupWrapper.sh\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n#!\/bin\/sh\nat -f \/home\/BACKUP\/backup_to_usb.sh now + 2 minutes\nexit<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now we are ready to create the main script of the backup. Remind please that we have indicated to udev to use a symbolic link &#8220;\/dev\/usbhddbackup&#8221;. We create a folder \/home\/BACKUP that cointains script and a folder with the last backup named &#8220;BACKUP_DAILY&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># touch backup_to_usb.sh\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n#!\/bin\/sh\n# Source folder\nsource=\/home\/BACKUP_DAILY\n# Destination folder on external device\ndest=\/media\/usb_backup\ndest_full_path=$dest\/BACKUP\n\n# Use SYMLINK to mount the device\nmount \/dev\/usbhddbackup \/media\/usb_backup\n\n# Check if destination exist\nif [ ! -d $dest_full_path ]; then\n echo \"destination do not exist\"\n exit\nfi\n\n# Backup the folder using rsync\nrsync -aruzv --delete $source\/ $dest_full_path \n\n# Change permission to all files on destination\nchmod -R 777 $dest_full_path \n\n# Force an immediate write of all cached data to disk \nsync\n\n# Umount the device\numount $dest\n\n# Send email to alert the end of the operation\necho \"Backup has just finished, you can detached the external disk safely\" | mail -s \"Backup finished\" <strong>myemail@email.com<\/strong>\n\nexit<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A strong backup policy provides to keep an offline (not connect to any kind of device) copy of own files, protecting your data against ransomware, device failure, etc. We can use an external Hard Disk Drive (HDD) to connected every week to our server\/workstation and automatically backup files and send an email at the end [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":256,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[32,22],"class_list":["post-195","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sysadmin","tag-backup","tag-linux"],"_links":{"self":[{"href":"https:\/\/fred151.net\/site\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fred151.net\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fred151.net\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fred151.net\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fred151.net\/site\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":4,"href":"https:\/\/fred151.net\/site\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":262,"href":"https:\/\/fred151.net\/site\/wp-json\/wp\/v2\/posts\/195\/revisions\/262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fred151.net\/site\/wp-json\/wp\/v2\/media\/256"}],"wp:attachment":[{"href":"https:\/\/fred151.net\/site\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fred151.net\/site\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fred151.net\/site\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}