Hi,
I have a hotswap sata drive that I use for backups on a PC running Ubuntu 12.04LTS. When I insert the drive it shows correctly on the desktop and I can double click the icon to show the contents of the device.
I want to write a script to copy certain files from the pc onto the removable drive, but I am having trouble mounting the hotswap drive as I never know what device id it will have allocated.
When I first insert the drive it allocates it as /dev/sdf1, but if I remove the drive and re-insert it, it allocates it as /dev/sdg1. How do I write the mount command (or copy command?) so I can find the drive id?
This is what I have:
sudo mount /dev/sdf1 /media/128GB_SSDcp /home/dfsql01/dumps/*.sql /media/128GB_SSD/SQL_Backups
`sudo umount /dev/sdf1

Many thanks,
Toomutch

Dani AI

Generated

’s suggestion to use a filesystem label is the right direction — do not rely on transient names like /dev/sdf1. udev creates persistent symlinks under /dev/disk/by-label and /dev/disk/by-uuid that survive re-inserts; scripts should use those instead of /dev/sdX. (wiki.debian.org)

Because the drive is NTFS (noted by ), set a label with the NTFS tool (or in Windows) and then resolve the device by label or UUID in the script. Example workflow: set the label (run on the unmounted partition), verify the label/UUID with lsblk or blkid, then mount via the by-label link or findfs. Practical example script:

LABEL=128GB_SSD
MOUNT=/media/128GB_SSD

DEV=$(findfs LABEL=$LABEL)           # or: DEV=/dev/disk/by-label/$LABEL
sudo mount -t ntfs-3g "$DEV" "$MOUNT"
cp /home/dfsql01/dumps/*.sql "$MOUNT/SQL_Backups/"
sudo umount "$MOUNT"

Use ntfslabel to write the label and findfs or blkid to discover devices. These tools are the standard way to make mounts deterministic. (manpages.debian.org)

Additional notes: lsblk -f is handy to list LABEL/UUID and FSTYPE before mounting. If write access is required by a non-root account, prefer ntfs-3g and set uid=, gid=, umask= or a user-mapping file in the mount options. Ensure the NTFS volume is clean (Windows not hibernated / fast-startup off) before mounting read-write. Test the script manually first and keep labels/UUIDs unique if multiple removable drives are used. (man7.org)

Recommended Answers

All 2 Replies

Give the file system on the drive a label. You can then find it with the e2label command.

Thanks for the idea rubberman. The drive is currently formatted NTFS as it also has to be accessed from a Windoze PC.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.