Automatic backup when connecting an external HDD in Ubuntu

    Recently, posts on the topic of backup have become popular. I will continue the baton under the motto “Many backups are good and different.”

    Article 1 and article 2 , which prompted me to this idea.

    I myself have been using rsnapshot for backup servers for a long time, and not only that. For those who don't know what it is, it's an rsync-based Perl-based backup utility. The peculiarity is the orientation to creating full snapshots of the file system after a specified time interval, in order to save disk space, the files that have not changed since the first backup are linked as hard link.

    I work on Ubuntu 11.10 on a laptop and therefore the article will describe the backup method of this laptop on an external USB HDD, which is not constantly connected, but only connects periodically. At the time of connection, a script will be launched to backup the system.

    So, let's begin. First, prepare an external screw to create backups on it. We start

    $ sudo tail -f /var/log/syslog | grep "[sd"

    and connect the screw via USB. The main thing is to understand which device connected our drive, in my case - sdb. You can use any Linux file system on this screw. I use ext3 as an old habit: The output of the last command will be very long, but we need to find something like the following:

    $ sudo umount /dev/sdb1
    $ sudo mkfs.ext3 /dev/sdb1
    $ sudo lsusb -v

    Bus 001 Device 004: ID 04fc: 0c25 Sunplus Technology Co., Ltd SATALink SPIF225A
    Device Descriptor:
    idVendor 0xYYYY Sunplus Technology Co., Ltd
    idProduct 0xXXXX SATALink SPIF225A

    These "XXXX" and "YYYY" we need to write down or remember somewhere. Now the screw can be turned off while we prepare the rsnapshot configs for work. First, install it:

    $ sudo apt-get install rsnapshot

    Now configure the config:

    $ sudo nano /etc/rsnapshot.conf

    Change snapshot_root to / media / backup and indicate the number of backups in the daily line. What we want to backup, we indicate in lines of the form: If you need to backup data from remote servers to the same screw, I recommend that you read the Kolger article . I do not need this, and I did not configure this for myself. It remains to automate the launch of rsnapshot when connecting the HDD. We create the file /etc/udev/rules.d/94-usb-backup.rules with the contents:

    backup /home/user/Sync localhost/
    backup /home/user/Картинки localhost/
    backup /home/user/Картинки_RAW localhost/
    backup /home/user/Видео localhost/

    SUBSYSTEM=="block", ENV{DEVTYPE}=="partition", SYSFS{idProduct}=="XXXX", SYSFS{idVendor}=="YYYY", SYMLINK+="backup", RUN+="/usr/local/bin/"

    Here we need those digital letters "XXXX" and "YYYY". We enter what corresponds to our HDD and restart udev:

    $ sudo service udev restart

    For more details on setting up udev, see this link .

    It remains to prepare the script /user/local/bin/

    export XAUTHORITY=/home/user/.Xauthority
    export DISPLAY=:0.0
    notify-send -i /usr/share/pixmaps/usbhdd.png -t $timeout Backup "Found backup device"
    ### Монтируем устройство
    mkdir /media/backup
    mount -t ext3 /dev/backup /media/backup
    chmod 777 /media/backup
    ### Проверяем на наличие свежего бекапа
    DATE=`date +%Y-%m-%d`
    OLDDATE=`ls -l --time-style=+%Y-%m-%d /media/backup | grep daily.0 | awk '{print $6}'`
    if [[ "$DATE" == "$OLDDATE" ]]; then
        notify-send -t $timeout -i /usr/share/pixmaps/usbhdd.png Backup "Backup alredy exists. Exiting." 
    ### Размонтируем устройство и выходим
        umount /media/backup
        rmdir /media/backup
        exit 1;
    ### Создаем бекап
    rsnapshot daily
    ### Unmounting device
    umount /media/backup
    rmdir /media/backup
    notify-send -i /usr/share/pixmaps/usbhdd.png -t $timeout Backup "Backup finished"
    exit 0

    In the first line of the "user" script, correct for your login in the system. You can also put your icon in the place of /usr/share/pixmaps/usbhdd.png for a more beautiful message.

    When connecting a device, the script checks for today's backup and if it is, it just quits. If it is not, a new snapshot of the specified data is created.

    I hope someone will find this helpful.

    Also popular now: