2

I want to copy a directory and its contents to my external USB hard drive when I connect it to my laptop automatically.

So, I created a file called 00-usb-backup.rules on /etc/udev/rules.d with this content:

ACTION=="add", ATTRS{idVendor}=="0480", ATTRS{idProduct}=="a208", RUN+="/home/fabio/bin/backup"

The backup script has permission to run as a program. And its content is:

#! /bin/bash
cp -r /home/fabio/Ads/ /media/fabio/BACKUP/teste/

Following the answer on another thread, I edited visudo and added the following on the last line:

fabio ALL=(ALL) NOPASSWD: /home/fabio/bin/backup

But when I connect the USB hard drive nothing happens. :(

Any ideas on what could be wrong?

Thanks!

Liso
  • 15,377
  • 3
  • 51
  • 80
FabioB
  • 63
  • 1
  • 8
  • 2
    I don't think this will work, since when udev rule runs your file system is not ready ye, and RUN should not run a long task. You need to schedule another job to do this. – Alvin Liang Nov 13 '19 at 03:14
  • Why don't you run a script via cron that executes what you need provided a file is created in certain place? something like:
    1. cron runs every minute and checks for file content on ~/.backupme
    2. if content, then mount the USB and copy files accordingly
    3. On the udev rule, the script that just adds the sdX that is been connected to ~/.backupme

    This way, when USB connects, your UDEV will add which sdX has been plugged in. Your cron script will look for the mount point accordingly (you can grep on mount for that device) and then copy files accordingly.

    – Joaquín Ayuso de Paúl Nov 13 '19 at 03:38
  • I saw many users with solutions with udev. I copied from them, but mine isn't working. I think a cron job it isn't the case. I want to perform file backup just after I plugin the hard-drive on the laptop. – FabioB Nov 13 '19 at 03:44
  • systemd may be best path. – user.dz Apr 14 '20 at 23:51

2 Answers2

2

Execute script when usb drive are plugged in.

RUN{type}:
This can only be used for very short-running foreground tasks. Running an event process for a long period of time may block all further events for this or a dependent device. Starting daemons or other long-running processes is not appropriate for udev; the forked processes, detached or not, will be unconditionally killed after the event handling has finished. Note that running programs that access the network or mount/unmount filesystems is not allowed inside of udev rules, due to the default sandbox that is enforced on systemd-udevd.service.

/etc/udev/rules.d/90-usb-backup.rules:

ACTION=="add", SUBSYSTEM=="block", \
ATTRS{idVendor}=="0480", ATTRS{idProduct}=="a208", \
ENV{DEVTYPE}=="partition", RUN+="/bin/sh -c '/home/fabio/bin/backup.sh'"

/home/fabio/bin/backup.sh:
(make sure your script has execution bit set; chmod +x script)

#!bin/bash

if [[ -b $DEVNAME ]] && \
   mount $DEVNAME /home/fabio/Ads/
then
   cp -a /home/fabio/Ads/ /media/fabio/BACKUP/teste/
fi

this will not mount the drive on the system, just inside the sandbox.

  • What about this [https://askubuntu.com/questions/401390/running-a-script-on-connecting-usb-device] solution? Because the script I want to run when the USB device is connected involves more operations with rsync. Is there a way to just execute a script? I don't get it how to use the first two lines you wrote on the backup.sh script. Can you help me? Thanks! – FabioB Nov 13 '19 at 12:36
  • 1
    By the way, the solution you gave didn't work. I did like that: the 90-usb-backup.rules is owned by root and I gave chmod +x to it. The backup.sh script is owned by the user (fabio) and hasn't execution permissions. Is that right? Thanks. – FabioB Nov 13 '19 at 12:47
  • the backup script should have execution set; the rule can be 644; –  Nov 13 '19 at 12:57
  • I set the execution on the backup.sh but still isn't working. – FabioB Nov 13 '19 at 13:07
0

This is what I do: a systemd service (e.g. borgmatic.service) is triggered by a udev rule based on the hot plug event.

SUBSYSTEM=="block", ACTION=="add", ENV{DEVTYPE}=="partition", TAG+="systemd", ENV{SYSTEMD_USER_WANTS}="borgmatic.service"

For the service in my ~/.config/systemd/user folder

[Unit] Description=/home backup

[Service]

Type=oneshot

ExecStart=/home/xxx/.local/bin/borgmatic