Skip to main content

Auto rsync

Command to automate execution of various rsync commands based on profiles defined on a YAML configuration file.

  • Instead of doing:

    rsync -avySH --delete --backup --backup-dir=../deleted/$timestamp/ "/media/Media/Photos" "user@host.com:/media/backup/filesets/$hostname.photos"
    

    Just do:

    autorsync -p photos
    
  • Instead of doing:

    rsync -avySH --delete --backup --backup-dir=../deleted/$timestamp/ "/media/Media/Photos" "user@host.com:/media/backup/filesets/$hostname.photos"
    rsync -avySH --delete --backup --backup-dir=../deleted/$timestamp/ "/media/Media/Books" "user@host.com:/media/backup/filesets/$hostname.books"
    rsync -avySH --delete --backup --backup-dir=../deleted/$timestamp/ "/media/Media/Music" "user@host.com:/media/backup/filesets/$hostname.music"
    

    Just do:

    autorsync
    

Or, in more general terms, instead of doing long rsync commands for your everyday backups, or putting them in adhoc scripts, write the clear ~/autorsync.yaml file and let autorsync do the work for you.

Installation

pip3 install auto-remote-sync --user

(Sorry for the long package name, ideally this would be simply autorsync, but name autosync was already taken and PyPi would not allow similar names).

Check PyPi and GitHub for autorsync.

Usage

Organize Profiles in ~/autorsync.yaml

Here is an example with some defaults and a few profiles:

DEFAULTS:
    source_part1: '{{home}}/Media'
    target_part1: user@remote.host.com:/media/backup/filesets
    delete: True
    backup: True
    backup_dir: ../deleted/{{time.strftime('%Y.%m.%d-%H.%M.%S')}}/
    background: False
    extra_part1: --rsh "ssh -i ~/.ssh/id_operator" --no-atimes

profiles:
    - name: books
      source_part2: Books/
      target_part2: '{{hostname}}.books/files'
      background: True
      extra: --copy-links

    - name: nextcloud.data
      source: /var/lib/nextcloud/data
      target_part2: '{{hostname}}.nextcloud_files'
      extra_part2: --copy-links --itemize-changes

    - name: navidrome.data
      env:
          NAVI_DB: /var/lib/navidrome/data/navidrome.db
      before: |
          systemctl stop navidrome
          sqlite3 $NAVI_DB "PRAGMA wal_checkpoint(FULL); PRAGMA wal_checkpoint(TRUNCATE); PRAGMA journal_mode=DELETE; vacuum;"
      after: |
          sqlite3 $NAVI_DB "PRAGMA journal_mode=WAL;"
          systemctl restart navidrome
      source: /var/lib/navidrome/
      target_part2: '{{hostname}}.navidrome/files'
      extra_part2: --exclude=cache/

    - name: databases
      before: |
        TARGET=/path/to/db-backup/`date --rfc-3339=date`
        mkdir -p "$TARGET"
        runuser -u postgres -- psql --csv -t -c "SELECT datname FROM pg_database WHERE datistemplate=false;" | while read db; do
           runuser -u postgres -- pg_dump -F c "$db" | gzip -c --best > "$TARGET/$db.sql.gz"
        done
      source: /home/aviram/Backup/databases/
      target_part2: '{{hostname}}-databases/'

Notes about this configuration

  • All profiles inherit parameters from DEFAULTS. If parameter isn’t set in the profile, the value defined in DEFAULTS will be used.
  • For each profile, the Source is defined by source parameter, or, if not defined, by source_part1/source_part2
  • Target follows same logic: target or target_part1/target_part2
  • delete makes rsync delete files in target that are absent in source
  • backup and backup_dir makes rsync save backups on target of deleted or modified files on source. Value on backup_dir is a path relative to target folder
  • extra lets you add extra rsync switches and can be used in the DEFAULTS section (to affect all profiles) or just into a specific profile. You can also use extra_part1 and extra_part2 between profiles and DEFAULTS, which will cause your switches to be concatenated.
  • before and after are shell scripts to be executed before and after rsync command.
  • env is a list of environment variables that will be set for before and after scripts.
  • You can use Jinja logic in source*, target* and extra* parts, surrounded by {{}}. Currently these are the available variables:
    • time, a Python datetime.datetime object which includes local timezone
    • hostname, such as “rocket”
    • Hostname, such as “rocket.mydomain.com” (FQDN)
    • username, UNIX user name as “joanbaez”
    • home, user’s home folder as “/home/joanbaez”
    • userid, user ID as “504”
    • gecos, user long name as “Joan Baez”

Se my real /root/autorsync.yaml file that is used everyday to run my incremental offsite backup via cron.

By the way, this is my root user crontab:

# Several backups everyday 4:15 AM
15 4 * * * $HOME/.local/bin/autorsync

As simple as that.

Example usage

  • Show all profiles:

    autorsync -l
    
  • Run rsync for all profiles:

    autorsync
    
  • Run rsync only for profile books

    autorsync -p books
    
  • Simulate rsync only for profile books (force rsync’s --dry-run)

    autorsync -n -p books
    

    or

    autorsync --dry-run -p books
    
  • Run rsync for 2 profiles from a non-default configuration file:

    autorsync -c /etc/autorsync.yaml -p "books, photos"
    

Metadata

Release files for auto-remote-sync 2.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 auto-remote-sync 2.2
File Size Uploaded Provenance
auto_remote_sync-2.2.tar.gz 19.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for auto-remote-sync 2.2
File Interpreter ABI Platform
auto_remote_sync-2.2-py3-none-any.whl Python 3 none any Details

Total release size: 39.8 kB

Release files / auto_remote_sync-2.2.tar.gz

Download URL auto_remote_sync-2.2.tar.gz
Size 19.8 kB
Tags Source
SHA-256 checksum
How to use checksums
09447c35b41ae438d91646db3682730c0292f6d498e304263fcfedcb21489e15
BLAKE2b-256 checksum
How to use checksums
709f74331f75a3758bfe6828d8e0f754c18eed94db986835d2c47fdcd1d6ac6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / auto_remote_sync-2.2-py3-none-any.whl

Download URL auto_remote_sync-2.2-py3-none-any.whl
Size 20.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a922da9c39acb5ad9b1e220399ee7bce42aae7952213a7d79d5e551a2b98b9a5
BLAKE2b-256 checksum
How to use checksums
f9606417bae41b7fb3bc183393562de2ca30bc90c9a14978ff7aa4cf0110b317
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for auto-remote-sync 2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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