drifterf
Posts: 136
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Check and overwrite a old file on the pi from usb

Wed Aug 21, 2019 5:33 pm

hi everyone.

cut a long story short. .......

i have a csv database stored on the pi, it checks every minuite and downloads a online csv so if any changed occur its up to date.

obviously this is only working if the pi is online, if it cannot download the file from the location on the web it has a temp and keeps that so it dosent fail.

lets say my database is in /home/pi/database and it has no internet to download a database so it sits running stand-alone. is it possible to update the csv file on a usb and just do the following:

1 - update the csv on a computer and put on a usb stick under the same named database (lets says database.csv)
2 - insert in the pi
3 - have the pi auto replace the file when the usb is plugged in

thanks

gordon77
Posts: 4221
Joined: Sun Aug 05, 2012 3:12 pm

Re: Check and overwrite a old file on the pi from usb

Thu Aug 22, 2019 2:09 pm

Here's a couple of ways of copying a file when a USB stick inserted.

Python:

Code: Select all

#!/usr/bin/python3
import os
import sys
usb_path = '/media/pi/*****/' # ***** is the USB stick name
csv_path = '/home/pi/database/'
file_name = 'database.csv'
while True:
    if os.path.exists(usb_path+file_name) and not os.path.exists(csv_path+file_name):
        command = 'sudo cp ' + usb_path+file_name + " " + csv_path+file_name
        print (command)
        os.system(command)
but clearly this would have to be running all the time.

or at boot...

I plugged in a USB stick and ran

Code: Select all

sudo systemctl list-units -t mount
, which gave me..

Code: Select all


UNIT                          LOAD   ACTIVE SUB     DESCRIPTION                 
-.mount                       loaded active mounted Root Mount                  
boot.mount                    loaded active mounted /boot                       
dev-mqueue.mount              loaded active mounted POSIX Message Queue File Sys
media-pi-SLX8000.mount        loaded active mounted /media/pi/SLX8000 
this shows the USB I used is identified as media-pi-SLX8000.mount, yours will be different so use yours where relevant.

I then wrote a service file with sudo nano /etc/systemd/system/your.service

Code: Select all


[Unit]
Description=My flashdrive script trigger
Requires=media-pi-SLX8000.mount
After=media-pi-SLX8000.mount

[Service]
ExecStart=/opt/plug_usb_in.sh

[Install]
WantedBy=media-pi-SLX8000.mount

then I wrote a file to run by this service.. sudo nano /opt/plug_usb_in.sh
containing

Code: Select all

#!/bin/sh
sudo cp /media-pi-SLX8000/database.csv /home.pi/database/database.csv
make the file executable with sudo chmod + x /opt/plug_usb_in.sh
then enable the service, I added this to /etc/xdg/lxsession/LXDE-pi/autostart so it runs at boot

Code: Select all


@sudo systemctl enable your.service
I then needed to stop filemanager running when I plugged in the USB...
Open File manager
Choose edit
Choose Preferences
Choose Volume Management
untick 'Show available options for removable media when they are inserted'
reboot

I hope this helps, it works for me.

Return to “Python”