Skip to main content

UdevBackup

Launch a backup script when an external storage device is connected.

Create a configuration file to define which devices to monitor and which scripts to run. When this device is connected, the volume is mounted, the script is launched automatically and then the volume is unmounted. An email can be sent with the output of the script at the end.

If the device is LUKS encrypted, it can be automatically unlocked if its UUID is defined in the config file and if a key is provided in the /etc/crypttab file.

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 email before turning it off again.

Only works on Linux, detecting the connection with udev rules and with the "at" utility to run long jobs (more than 30 seconds).

installation

sudo pipx install udevbackup

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

    sudo udevbackup install

configuration

Create a .ini config file with a "main" section for global options, and another section for each target partition. The filename 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
[main]
lock_file = Name of a global lock file to avoid parallel runs.
log_file = Name of the global log file.
smtp_auth_password = SMTP password. Default to "".
smtp_auth_user = SMTP user. Default to "".
smtp_from_email = E-mail address for the FROM: value. Default to "".
smtp_server = SMTP server. Default to "localhost".
smtp_smtp_port = The SMTP port. Default to 25.
smtp_to_email = Recipient of the e-mail. Required to send e-mails.
smtp_use_starttls = Use STARTTLS for emails. Default to 0.
smtp_use_tls = Use TLS (smtps) for emails. Default to 0.
use_log_file = Write all errors to the log file. Default to 1.
use_smtp = Send messages by email (with the whole content of stdout/stderr of your scripts). Default to 0.
use_stdout = Display messages on stdout. Default to 0.

[example]
command = Command running the script (whose name is passed as first argument). Default to "bash".
fs_uuid = UUID of the target partition.
luks_uuid = UUID of the LUKS partition (a key must be provided in the /etc/crypttab file).
mount_options = Extra mount options. Default to "".
post_script = Script to run after the disk umount. Only run if the disk was mounted. Default to "".
pre_script = Script to run before mounting the disk. The disk will not be mounted if this script does not returns 0. Default to "".
script = Content of the script to execute when the disk is mounted. Working dir is the mounted directory.This script will be copied in a temporary file, whose name is passed to the command.
stderr = Write stderr to this filename.
stdout = Write stdout to this filename.
user = User used for running the script and mounting the disk.

Here is a complete example:

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

[my_config]
fs_uuid = b5094075-9f23-4881-9315-86fe4e97f029
script = mkdir -p ./data
         rsync -av /data/to_backup/ ./data/

You can display the current config:

udevbackup show

Metadata

Release files for udevbackup 1.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for udevbackup 1.4.2
File Size Uploaded
udevbackup-1.4.2.tar.gz 17.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for udevbackup 1.4.2
File Interpreter ABI Platform
udevbackup-1.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 35.4 kB

Release files / udevbackup-1.4.2.tar.gz

Download URL udevbackup-1.4.2.tar.gz
Size 17.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6fb610781d36d4284ed37f194cc55e394fa1ab5f6736375c12672d0eacfa9f53
BLAKE2b-256 checksum
How to use checksums
a296161a89f3cc6ec4e0b90dfe728e0fedf6bf2a8ebf9fa77d4580f3832c3a8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.13.9 Darwin/25.1.0

Release files / udevbackup-1.4.2-py3-none-any.whl

Download URL udevbackup-1.4.2-py3-none-any.whl
Size 18.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb626b44e5f6d8f43d4cd7de26366e02d5fbe5146a46cf8c353fa05c37bcd87b
BLAKE2b-256 checksum
How to use checksums
09f9340ed420ed98ae0b9aa83df58574e63c00984c214692c24dc9111df7c42c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.13.9 Darwin/25.1.0

Release history Release notifications | RSS feed

This release

1.4.2 This release

2 release files

1.3.0

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page