📜 ⬆️ ⬇️

Incremental Backup with USB HDD in Ubuntu

Finally, I found the time to do a freshly arrived piece of iron - a 500GB USB screw. This post can be useful to anyone who has thought about a reliable and convenient backup of data that requires minimal effort.
So let's get started.


Given: USB HDD, laptop with Ubuntu 7.10, lots of useful data :)

I would like to: Automatic incremental backup, when connecting a specific USB HDD, provided that at least N-minutes have passed since the last backup.
')
Instruments:

  1. sbackup is a great python softphone, it works through rdiff-backup, it can write to local folders and to an SSH session. Archives data. Saves the list of installed packages.
    udev rules file - here we slyly define and mount our disk
    some bash scripts - check if we need to back up


    Let's start:

    First of all, we need to force udev to define our particular screw. Make it very easy. Create a file
    /etc/udev/rules.d/94-usb-backup.rules The number 94 is selected as 95-hal.rules -1. This is important because it's better to do our dirty deeds before HAL recognizes the device.

     - / etc / udev / rules.d / 94-usb-backup.rules--
     SUBSYSTEM == "block", ENV {DEVTYPE} == "partition", SYSFS {idProduct} == "2339", SYSFS {idVendor} == "152d", NAME = "backup", RUN + = "/ usr / local / bin / usb-backup "
    


    Next, you need to decide on what parameters we will recognize the device. If you need to back up only to a specific specific screw, then select the most unique set of parameters. I had enough idProduct and idVendor pairs. To be sure, you can add for example iSerial (thanks to small_jam and spiritedflow for the comments). The idProduct and idVendor values ​​need to be recognized for each specific device using the command

     $ lsusb –v
    

    And we get something like that
     ...
     Bus 005 Device 089: ID 152d: 2339 
     Device Descriptor:
       bLength 18
       bDescriptorType 1
       bcdUSB 2.00
       bDeviceClass 0 (Defined at Interface level)
       bDeviceSubClass 0
       bDeviceProtocol 0
       bMaxPacketSize0 64
       idVendor 0x152d
       idProduct 0x2339
       bcdDevice 1.00
       iManufacturer 1 JMicron
       iProduct 2 USB to ATA / ATAPI Bridge
       iSerial 5 901EFFFFFFFF
       bNumConfigurations 1
       Configuration Descriptor:
         bLength 9
         bDescriptorType 2
         wTotalLength 32
         bNumInterfaces 1
         bConfigurationValue 1
     ...
    

    NAME = “backup” means that our screw will be mounted to / dev / backup
    RUN + = "/ usr / local / bin / usb-backup" - specify which script to run when connected. Important! Here we write the name of the script, not the command, for example, if we write RUN + = "/ usr / local / bin / usb-backup &" it will not work.
    We will return to the script a little later. Let's create a stub for now and give rights to execute.
     - / usr / local / bin / usb-backup--
     #! / bin / bash
    

    Now we finish with the mount.
    To automatically mount the device, add a line to / etc / fstab
     - / etc / fstab / -
     ...
     / dev / backup / media / backup ext3 users, atime, noauto, rw, nodev, exec, nosuid 0 0
    

    This is provided that the file system is on the ext3 screw.

    Then you can test. Turn off the screw. We save all configs. Making /etc/init.d/udev restart
    Connect the screw. We look. Should have been mounted in / media / backup.

    Now configure sbackup. You can put it in the repository using apt-get install sbackup
    Config /etc/sbackup.conf has enough configuration information. It is important to specify the same target directory as in the scripts below.

    Next, we return to the script / usr / local / bin / usb-backup
    He has one feature. Until it is mounted, it will not go further. Therefore, we use a workaround.
     - / usr / local / bin / usb-backup--
     #! / bin / bash
     / usr / local / bin / usb-backup-script &
    

    Run the second script in the background. He will then do all the work.
     - / usr / local / bin / usb-backup-script--
     #! / bin / bash
     BCPDIR = / media / backup / backup_storage / # this is the backup directory
     for i in `seq 1 10`;
     do
      if [-e $ BCPDIR] # do we see if it was mounted?
       then # count files under 180 minutes without accessing the directory
        if [`find $ BCPDIR -maxdepth 1 -mmin -180 |  grep -v ^ $ BCPDIR $ |  grep.  -c` -eq 0];
         then
          sbackupd # run backup
         fi
    
    
         break # now no need to loop
      fi
      sleep 1 # wait for the second while it is mounted (I usually have a couple of seconds)
     done
    

    Voila We received automatic incremental backups when connecting our screw.

Source: https://habr.com/ru/post/27889/


All Articles