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

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

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.4.2.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

udevbackup-1.4.2-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: udevbackup-1.4.2.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.13.9 Darwin/25.1.0

File hashes

Hashes for udevbackup-1.4.2.tar.gz
Algorithm Hash digest
SHA256 6fb610781d36d4284ed37f194cc55e394fa1ab5f6736375c12672d0eacfa9f53
MD5 4064d894e67395b2d4e69d44450059f5
BLAKE2b-256 a296161a89f3cc6ec4e0b90dfe728e0fedf6bf2a8ebf9fa77d4580f3832c3a8d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: udevbackup-1.4.2-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.13.9 Darwin/25.1.0

File hashes

Hashes for udevbackup-1.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eb626b44e5f6d8f43d4cd7de26366e02d5fbe5146a46cf8c353fa05c37bcd87b
MD5 48b7b3eb0f99b53d544088e702b2d868
BLAKE2b-256 09f9340ed420ed98ae0b9aa83df58574e63c00984c214692c24dc9111df7c42c

See more details on using hashes here.

Supported by

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