Skip to main content

Automate execution of various rsync commands based on profiles defined on a YAML configuration file

Project description

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"
    

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

auto_remote_sync-2.1.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

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

auto_remote_sync-2.1-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file auto_remote_sync-2.1.tar.gz.

File metadata

  • Download URL: auto_remote_sync-2.1.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for auto_remote_sync-2.1.tar.gz
Algorithm Hash digest
SHA256 91f1f55e1a338577139cec032130804cda49ade74481ceed2c012186d98e9484
MD5 0d279ec75ef051091b97d50f1e5e4b75
BLAKE2b-256 1d0016c955ef8657db5160ad70dc28c350f563fdf12e85fed4b756f3b8a8d523

See more details on using hashes here.

File details

Details for the file auto_remote_sync-2.1-py3-none-any.whl.

File metadata

  • Download URL: auto_remote_sync-2.1-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for auto_remote_sync-2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c8ec5c7f20fe868e0954ba218bad3309d02e37ec2a04f03006f4ac671cf56f00
MD5 ec2cb2a2a2e905265961bab8c053f6d6
BLAKE2b-256 f25ee7f611c430252fd9db979f6d2ac5a510c704ac0e1a3ff05ed7e5817d9a80

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