3

I'm currently doing a project which requires a file to be automatically copied to USB-stick on mount. Based on my research in internet, I believe it is doable using udev rule. So, when an USB is inserted into my RPi2, the udev rule will then execute a python script which allows file to be copied to the USB-stick.

Problem is, I also heard that the script will be executed before the RPi mount the USB, which means the file will not be copied. Is there a solution to this? The python script was executed when i copied the file internally (on RPi itself not USB), it just doesn't work when I tried to copy it to USB.

Below are my code:

Udev rule

KERNEL=="sd*1", ACTION=="add", RUN=="/home/pi/datalogger/autocopy.sh"

Shell script

cd /
cd /home/pi/datalogger
sudo /usr/bin/python autocopy.py
cd / 
exit

Python script

import shutil
import datetime

# File to be copied
source = "/home/pi/copied.txt"

# USB name must be changed to 'USB1' in order for auto copy to work
destination = "/media/pi/USB1/datalogger_backup_%s.txt" % datetime.datetime.now().date()

try:
   # Copy file to destination
   shutil.copy2(source, destination)
   # E.g. source and destination is the same location
except shutil.Error as e:
   print("Error: %s" % e)
   # E.g. source or destination does not exist
except IOError as e:
   print("Error: %s" % e.strerror)
dboy
  • 73
  • 4
  • 9
  • 1
    Check if it is mounted already, if not wait and check again. – Klaus D. May 03 '17 at 14:39
  • I did, still not working – dboy May 03 '17 at 16:17
  • Please explain, didn't understand: _"The python script was executed when i copied the file internally (on RPi itself not USB), it just doesn't work when I tried to copy it to USB."_. – stovfl May 03 '17 at 17:09
  • 1
    @stovfl In order to verify if the python script was actually executed or not, i change the destination where the file should be copied. Instead of to the USB, the file will be copied to another folder in raspberry pi home directory. The file is copied successfully as soon as a USB stick is inserted. However, when i tried again (this time copy destination is on USB itself), the file is not copied (or probably the python script was executed but the destination is not available as the USB is yet to be mounted) – dboy May 03 '17 at 17:17
  • 1
    Why don't you log/print to a file rather than to console so you can see what the error is. Also make sure you log a successful copy. – DisappointedByUnaccountableMod May 03 '17 at 17:24

1 Answers1

2

Question: ... the destination is not available as the USB is yet to be mounted

Add the following to your script to verify the mount status:

mount >> /tmp/mount.log

Read this auto-mounting-usb-storage/
Maybe you can adapt to your needs.

stovfl
  • 14,998
  • 7
  • 24
  • 51
  • I applied the code from the link you gave me, managed to get the file copied on the USB-Stick. However, when I physically unmount the stick from my RPi, the copied file is not on the USB. Can't seem to understand why. – dboy May 04 '17 at 10:51
  • @dboy: Did you realy execute `umount` command, before removing the USB-Stick? – stovfl May 04 '17 at 10:56
  • Yes, I didn't change anything from the code from the link you gave me. I created a new rule for the automount/unmount and applied the example code and my second rule copied the file to the USB stick. – dboy May 04 '17 at 11:25
  • Funny thing is, if I leave the USB attached to the RPI for a long time (maybe 2-3 mins), the copied file is there on the USB when I removed it. Confusing – dboy May 04 '17 at 11:26
  • @dboy: I didn't mean `ACTION=="remove",` from the linked `udev rule`. – stovfl May 04 '17 at 14:58
  • I mean have you typed the command `umount` and **waiting** the buffer is written, before removing the USB-Stick. The **delay** is normal behavior. In general it's not recommend to remove the Stick without do `unmount`. – stovfl May 04 '17 at 15:03
  • 1
    I added the umount command in my shell script, now it works, The file is safely written on the USB when i unplugged it. Thanks for the help man – dboy May 05 '17 at 10:34
  • 1
    Dear @dboy, would you share your script for reference? I guess you have a stick with led light to know when copying has finished? – rriemann Apr 24 '18 at 13:21