Unmounting a USB stick - a reminder.

Well, this is embarrassing.

For many years I have been doing regular backups of all my files to a USB stick using a fancy script that I wrote.
It’s worked well until yesterday when nothing seemed to be written despite no error messages being displayed.
It turned out that the USB stick had been corrupted and I had to consult the internet to fix and reformat the device.

When one inserts a USB stick the file manager is started automatically and opens at the USB device. My script ignores the file manager and, when the backup is finished, unmounts the device and tells me to remove it. BUT the device is still active in the file manager. I suspect that over the years I have been lucky in removing my device after my script has unmounted it without damage to the device - until now!
Clearly, a device can be mounted by separate applications independently so it’s necessary to ensure that all applications accessing the device have unmounted it. Specifically; unmounting via the terminal will not unmount it from the file manager, but unmounting in the file manager will unmount it fully . Simply closing the file manager (without unmounting the USB) does not unmount it from the terminal.
Perhaps a knowledgeable member can tell us what’s happening.

My solution: as soon as the script has found the USB it now closes the file manager window that has just opened. As the script is now the only application mounting the device, it is safe to remove it after the script unmounts it.
Yes - I feel very foolish but thought that a reminder was in order.

Keith

(Dell Latitude E5570 running Ubuntu 20.04)

I too use a script to backup to USB. It’s not fancy, though: the first line is the rsync command with parameters and the second line is just sync.

If you unmount from a terminal, then the device is indeed unmounted. You say it’s “still active in the file manager”. Well, I can’t vouch for what Ununtu and Gnome get up to, but with PCLinuxOS and Xfce it’s still listed in the file manger — because the device is still physically available until you pull it out — but it’s not active. The mount command in the terminal will not list it. If you click on it in the file manager, then the contents will display because the file manager will re-mount it. Being a belt-and-braces type, I usually right-click and choose “safely remove”. If I hadn’t used sync, I’d get a warning not to remove the device as there was data to be written, but instead I get a message that it’s safe to remove.

Thank you David - and you are right.

Upon checking again I find that when unmounting in the terminal, the file manager display changes from the USB file list to the Home directory - indicating the unmounting of the device. The confusion for me was that in file manager:
a) the USB device is still listed, and
b) in Ubuntu the USB name shown has a symbol next to it that says “unmount” when hovering the mouse over it, giving me the impression that the device is still mounted.

If all this means that I am safe to unmount in the terminal and unplug, without worrying about it “appearing” to be mounted in file manager, then how have I corrupted the device?
Just life, I guess.

Thanks, David,
Keith

Is it possible that the backup was likely to overflow the USB’s capacity?

No: it’s a 32GB USB and I back up 16GB using rsync so the USB’s trash directory never gets used.
After reformatting the device, it backed up without problems.

Thanks,
Keith

Mmm, it’s just that corrupting a drive is unusual. Typically if you pull it out too soon, either your stuff isn’t there because it’s just not been sync’d, or if you pull it out mid-copy, then it needs an “fsck” and you lose “something” … same principle as powering off your machine without shutting down …

Yep - the instructions I found on the web for fixing recalcitrant USBs used fsck, which I am always a bit wary of using as it’s so easy to screwup one’s computer. Then reformatted.
Anyway; problem solved for now and my addition to the script to close the file manager as soon as it starts will ease my mind if nothing else.

Thank you for your thoughts.

Keith

USB drives are not immortal. Over the years, my rule has been

  • first failure, use fsck
  • if it happens again, reformat
  • third time, dump it!

USB sticks are so cheap and reliable these days that your advice is sensible.
Might be wise for me to buy a few spares.

It occurs to me also that one ought to actually check that a backup really has been performed and not just finished without a reported error. I shall do that from now on.

Thanks, David.
Keith