Backup to external usb drive

raj

Active Member
Sep 17, 2011
217
4
38
www.businessparksolutions.com
Hi Team,

Could anyone assist. I used to run proxmox 4 and was able to connect a usb drive to do offsite backup. After upgrading to proxmox 5 that does not work anymore. I have tried to do a fresh install of proxmox 5 but still have the same problem. The way the drive was being mounted was via udev.
This is the rule that was put in udev
11-media-by-label-auto-mount.rules
/etc/udev/rules.d/

This is the content of the rul:

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"
# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"
# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"
# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"
# Mount the device
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"
# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"
# Exit
LABEL="media_by_label_auto_mount_end"

This would automatically mount the usb drive when it was connected and unmount it when disconnected.
Can anyone please assist.

I am willing to pay a £50 for any successful
 

raj

Active Member
Sep 17, 2011
217
4
38
www.businessparksolutions.com
Hi Dietmar,

I was wondering if that could be as a plugin or option from the gui itself. I am sure a lot of users who use proxmox for smbs and they are not running it in a cluster would be interested. I wonder if there would be enough interest to get some money together for a bounty for this so you guys do get paid for this work. Anyone else would like to put some money for this please.

This is a good way to do offsite backup and secure it afterwards specially bandwidth to external storage ie amazon etc can be an issue due to vm being about 100 GB.

Cheers,

Rajb
 

mbaldini

Active Member
Nov 7, 2015
170
21
38
In fstab add a line to mount your USB hard disk in a directory and use options
noauto,x-systemd.automount,x-systemd.device-timeout=10
After that use systemctl daemon-reload and systemd will automatically try to mount the disk when the directory will be used
 

raj

Active Member
Sep 17, 2011
217
4
38
www.businessparksolutions.com
@fireon yes i will do that also.

Here is a solution I found. I have tested this on one server and its working will test on a new install shortly also.

https://serverfault.com/questions/766506/automount-usb-drives-with-systemd


After several false starts I figured this out. The key is to add a systemd unit service between udev and a mounting script.

(For the record, I was not able to get this working using udisks2 (via something like udisksctl mount -b /dev/sdb1) called either directly from a udev rule or from a systemd unit file. There seems to be a race condition and the device node isn't quite ready, resulting in Error looking up object for device /dev/sdb1. Unfortunate, since udisks2 could take care of all the mount point messyness...)

The heavy lifting is done by a shell script, which takes care of creating and removing mount points, and mounting and unmounting the drives.

/usr/local/bin/usb-mount.sh

#!/bin/bash

# This script is called from our systemd unit file to mount or unmount
# a USB drive.

usage()
{
echo "Usage: $0 {add|remove} device_name (e.g. sdb1)"
exit 1
}

if [[ $# -ne 2 ]]; then
usage
fi

ACTION=$1
DEVBASE=$2
DEVICE="/dev/${DEVBASE}"

# See if this drive is already mounted, and if so where
MOUNT_POINT=$(/bin/mount | /bin/grep ${DEVICE} | /usr/bin/awk '{ print $3 }')

do_mount()
{
if [[ -n ${MOUNT_POINT} ]]; then
echo "Warning: ${DEVICE} is already mounted at ${MOUNT_POINT}"
exit 1
fi

# Get info for this drive: $ID_FS_LABEL, $ID_FS_UUID, and $ID_FS_TYPE
eval $(/sbin/blkid -o udev ${DEVICE})

# Figure out a mount point to use
LABEL=${ID_FS_LABEL}
if /bin/grep -q " /media/${LABEL} " /etc/mtab; then
# Already in use, make a unique one
LABEL+="-${DEVBASE}"
fi
MOUNT_POINT="/media/${LABEL}"

echo "Mount point: ${MOUNT_POINT}"

/bin/mkdir -p ${MOUNT_POINT}

# Global mount options
OPTS="rw,relatime"

# File system type specific mount options
if [[ ${ID_FS_TYPE} == "vfat" ]]; then
OPTS+=",users,gid=100,umask=000,shortname=mixed,utf8=1,flush"
fi

if ! /bin/mount -o ${OPTS} ${DEVICE} ${MOUNT_POINT}; then
echo "Error mounting ${DEVICE} (status = $?)"
/bin/rmdir ${MOUNT_POINT}
exit 1
fi

echo "**** Mounted ${DEVICE} at ${MOUNT_POINT} ****"
}

do_unmount()
{
if [[ -z ${MOUNT_POINT} ]]; then
echo "Warning: ${DEVICE} is not mounted"
else
/bin/umount -l ${DEVICE}
echo "**** Unmounted ${DEVICE}"
fi

# Delete all empty dirs in /media that aren't being used as mount
# points. This is kind of overkill, but if the drive was unmounted
# prior to removal we no longer know its mount point, and we don't
# want to leave it orphaned...
for f in /media/* ; do
if [[ -n $(/usr/bin/find "$f" -maxdepth 0 -type d -empty) ]]; then
if ! /bin/grep -q " $f " /etc/mtab; then
echo "**** Removing mount point $f"
/bin/rmdir "$f"
fi
fi
done
}

case "${ACTION}" in
add)
do_mount
;;
remove)
do_unmount
;;
*)
usage
;;
esac




The script, in turn, is called by a systemd unit file. We use the "@" filename syntax so we can pass the device name as an argument.

/etc/systemd/system/usb-mount@.service

[Unit]
Description=Mount USB Drive on %i
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/usb-mount.sh add %i
ExecStop=/usr/local/bin/usb-mount.sh remove %i

Finally, some udev rules start and stop the systemd unit service on hotplug/unplug:

/etc/udev/rules.d/99-local.rules

KERNEL=="sd[a-z][0-9]", SUBSYSTEMS=="usb", ACTION=="add", RUN+="/bin/systemctl start usb-mount@%k.service"

KERNEL=="sd[a-z][0-9]", SUBSYSTEMS=="usb", ACTION=="remove", RUN+="/bin/systemctl stop usb-mount@%k.service"

This seems to do the trick! A couple of useful commands for debugging stuff like this:

  • udevadm control -l debug turns on verbose logging to /var/log/syslog so you can see what's happening.
  • udevadm control --reload-rules after you modify files in the rules.d dir (may not be necessary, but can't hurt...).
  • systemctl daemon-reload after you modify systemd unit files.
I hope this helps someone else :)

rajbps
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get your own in 60 seconds.

Buy now!