📜 ⬆️ ⬇️

Automatic backup when connecting an external HDD in Ubuntu

Recently, posts have become popular on the topic of backup. 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 long been using rsnapshot for backup servers, and not only. For those who do not know what it is, this is an rsync-based Perl utility for creating backups. A special feature is the orientation towards creating full file system snapshots at a predetermined time interval, to save disk space, 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 itself on an external USB HDD, which is not constantly connected, but only periodically connected. At the time of connection, a script will be launched to back up the system.

So, let's begin. To begin, we will prepare an external screw to create backups on it. Run

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

and connect the screw via USB. The main thing is to understand how the device connected our disk, in my case - sdb. This screw can use any Linux file system. I use the old ext3 habit:

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


The output of the last command will be sooo long, but we need to find something like this:

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 somewhere to write or remember. Now the screw can be turned off while we prepare 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 specify the number of backups in the daily line. What we want to backup, specify the lines of the form:

backup /home/user/Sync localhost/
backup /home/user/ localhost/
backup /home/user/_RAW localhost/
backup /home/user/ localhost/


If it is necessary to backup the data from remote servers to the same screw, I recommend reading the Kolger article . I do not need this, and I did not set it up at myself.

It remains to automate the launch of rsnapshot when you connect the HDD.

Create the file /etc/udev/rules.d/94-usb-backup.rules with the contents:

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


This is where we need those digital letters “XXXX” and “YYYY”. We enter what corresponds to our HDD and restart udev:

$ sudo service udev restart

More details on udev configuration can be found at this link .

It remains to prepare the script /user/local/bin/usb-backup.sh:

 #!/bin/bash export XAUTHORITY=/home/user/.Xauthority export DISPLAY=:0.0 timeout=5000 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; fi ###   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 it to your login in the system. You can also put your icon in place /usr/share/pixmaps/usbhdd.png for a more beautiful message.

When a device is connected, the script checks for the presence of today's backup and, if it does, simply exits. If it does not exist, a new snapshot of the specified data is created.

I hope someone will be useful.

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


All Articles