Skip to main content

detects when specified storage devices are connected, mounts them, executes a script, umounts them and tells when it is done.

Project description

UdevBackup

On Linux, detects when specified storage devices are connected, then mounts them, executes a script, unmounts them and tells when it is done (using mail or text to speech).

A config file defines storage devices and the scripts to run.

I wrote this script for a simple offline backup of my server: I just have to turn the external USB drive on and wait for the message (using text to speech) before turning it off again. UdevBackup double forks before running the script, so there is no timeout problem with udev and slow scripts.

Require the "at" utility for running long jobs (more than 30 seconds).

installation

pipx install udevbackup

you need to create a udev rule to launch udevbackup when a new device (with a file system) is connected:

    echo 'ACTION=="add", ENV{DEVTYPE}=="partition", RUN+="/usr/local/bin/udevbackup at"' | sudo tee /etc/udev/rules.d/udevbackup.rules
    udevadm control --reload-rules

If you only have short jobs (less than 30s), you can use:

    echo 'ACTION=="add", ENV{DEVTYPE}=="partition", RUN+="/usr/local/bin/udevbackup run"' | sudo tee /etc/udev/rules.d/udevbackup.rules
    udevadm control --reload-rules

configuration

Create a .ini config file with a "main" section for global options, and another section for each target partition. The name is not important. All .ini files in /etc/udevbackup are read. These files must use the UTF-8 encoding.

You can display all available options through an example of config file with the "example" command.

udevbackup example

Create one or more .ini files in /etc/udevbackup.
Yellow lines are mandatory.
[main]
smtp_auth_user = SMTP user. Default to "".
smtp_auth_password = SMTP password. Default to "".
smtp_server = SMTP server. Default to "localhost".
smtp_from_email = Recipient of the e-mail.  Default to "".
smtp_to_email = E-mail address for the FROM: value. Default to "".
use_speech = Use google speech for announcing successes and failures. Default to 0.
use_stdout = Display messages on stdout. Default to 0.
use_smtp = Send messages by email (with the whole content of stdout/stderr of your scripts). Default to 0.
smtp_use_tls = Use TLS (smtps) for emails. Default to 0.
smtp_use_starttls = Use STARTTLS for emails. Default to 0.
smtp_smtp_port = The SMTP port. Default to 25.

[example]
fs_uuid = UUID of the used file system. Check /dev/disk/by-uuid/ before and after having connected your disk to get it.
command = Command to call for running the script (whose name is passed as first argument). Default to "bash".
script = Content of the script to execute when the disk is mounted. Current working dir is the mounted directory. This script will be copied in a temporary file, whose name is passed to the command.
stdout = Write stdout to this filename.
stderr = Write stderr to this filename.
mount_options = Extra mount options. Default to "".
user = User used for running the script and mounting the disk.Default to "current user".
pre_script = Script to run before mounting the disk. The disk will not be mounted if this script does not returns 0. Default to "".
post_script = Script to run after the disk umount. Only run if the disk was mounted. Default to "".

Here is a complete example:

cat /etc/udevbackup/example.ini
[main]
smtp_auth_user = user
smtp_auth_password = s3cr3tP@ssw0rd
smtp_server = localhost
use_speech = 1
use_stdout = 0
use_smtp = 1

[my_config]
fs_uuid = 58EE-7CAE
script = mkdir -p ./data
    rsync -av /data/to_backup/ ./data/

You can display the current config:

udevbackup show

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

udevbackup-1.3.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

udevbackup-1.3.0-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file udevbackup-1.3.0.tar.gz.

File metadata

  • Download URL: udevbackup-1.3.0.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.3.0

File hashes

Hashes for udevbackup-1.3.0.tar.gz
Algorithm Hash digest
SHA256 b40e60c1edd79d281621fdb14a803ca24c19ffd71782cede2bb51c1c3e73112d
MD5 d9d2e0ab10ff48b044c54d5eb744f95e
BLAKE2b-256 623eb95192c9f73b762496640395353d93ac237a5c13ba77a210c0cee3f467d8

See more details on using hashes here.

File details

Details for the file udevbackup-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: udevbackup-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.3.0

File hashes

Hashes for udevbackup-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 08f69b5f95fe89c13405e75361ba1d869be5a9548a262a6b13a5a04f547bfa84
MD5 aa352b6d1324f656a65f2ab62c05d3f8
BLAKE2b-256 a7a8dec54c457561c9979e511dc2f49b78689bb9ba143dd0cf1556fa005c6aa5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page