Page 1 of 1

Auto mount/unmount USB

Posted: 25 Nov 2024, 21:32
by JohnDorian
Hi to everybody, I'm new in the forum and also a beginner with this device! So please patience with me.
I'm a worker on automation field, normally I work with PLC and HMI, this is my world while Raspberry is quite dark for me.
I have more than a few client that asked me to give them some production log by csv file exported in a USB. In automation field it happens very often to works with old machinery where an ethernet cable is unthinkble to carry. So the easiest solution is the USB.
I know that by default linux does not recognize a new hardware and it's required to mount/unmount the disk ecc.

My goal would be to make the whole procedure automatic (clear some action from operator must be done for example click button export...) but as I told at the begin I'm really a begginer.

So there's somebody can help me with some sample or some manual/guide to give me the right direction?

Really thank's in advanced!

Re: Auto mount/unmount USB

Posted: 06 Dec 2024, 12:53
by u.biakoup
Hi JohnDorian,
Welcome to the forum, and thank you for reaching out.
Your use case is a common scenario in automation, and you're absolutely on the right track exploring Raspberry Pi and Linux for solving it. I'll guide you step-by-step to automate the process of exporting logs to a USB drive when it's plugged in.

Overview of the Solution
The process involves:
  • Detecting when a USB drive is connected.
  • Automatically mounting the USB drive.
  • Copying your logs (or exporting your CSV files) to the USB.
  • Safely unmounting the USB drive to avoid data corruption.
Step-by-Step Instructions

1. Detect the USB Drive
Linux uses udev to manage hardware events. We can create a rule to detect when a USB drive is inserted and trigger a custom script.

Open a terminal and create a new udev rule:

Code: Select all

sudo nano /etc/udev/rules.d/99-usb-auto-export.rules
  • Add the following rule to execute a script when a USB drive is inserted:

Code: Select all

ACTION=="add", KERNEL=="sd[a-z][0-9]", RUN+="/usr/local/bin/usb-export-logs.sh"
  • Reload the udev rules:

Code: Select all

sudo udevadm control --reload-rules
2. Write the Export Script

This script will handle mounting the USB drive, copying the logs, and optionally unmounting it after the operation.
  • Create the script file:

Code: Select all

sudo nano /usr/local/bin/usb-export-logs.sh
  • Add the following code to the script:

Code: Select all

#!/bin/bash

MOUNT_POINT="/media/usb"
LOG_SOURCE="/var/log"
EXPORT_FILE="production_log_$(date +'%Y%m%d_%H%M%S').csv"

# Create the mount point if it doesn't exist
[ ! -d $MOUNT_POINT ] && mkdir -p $MOUNT_POINT

# Mount the USB drive
mount $1 $MOUNT_POINT

# Copy logs or export CSV
if [ -d $MOUNT_POINT ]; then
    tar -czf $MOUNT_POINT/$EXPORT_FILE -C $LOG_SOURCE .
    echo "Logs exported to $MOUNT_POINT/$EXPORT_FILE"
else
    echo "Error: Unable to mount the USB drive!"
fi

# Optionally, unmount the USB drive
umount $MOUNT_POINT

    Make the script executable:

    Code: Select all

    chmod +x /usr/local/bin/usb-export-logs.sh
    3. Test the Setup
    • Insert a USB drive into the Raspberry Pi.
    • The script should automatically mount the drive, copy the logs, and optionally unmount it.
    • Check the USB drive for the exported logs.

    Re: Auto mount/unmount USB

    Posted: 18 Dec 2024, 10:51
    by alina104
    You should try an automatic way to update USB driver.