backup

Ansible Role: Backup

Tests Ansible Galaxy License

Role to backup and restore files and directories. Uses rsync but intended to be used with a locally mounted backup drive.

During provisioning this role will restore any previously backed-up files and directories. Once restarted the OS will perform incremental backups every 5 minutes and during shutdown.

The backup is a simple mirror of the source directory, previous file versions are discarded. The main purpose is to persist files between rebuilds of your local development VM. If you can't afford to lose/replace your files, use a remote backup solution with versioning instead.

This role is primarily indented for backing-up and restoring the contents of a users home directory.

Requirements

  • Ansible >= 2.9

  • Linux Distribution

    • Debian Family

      • Ubuntu

        • Bionic (18.04)
        • Focal (20.04)
    • Note: other versions are likely to work but have not been tested.

Role Variables

The following variables will change the behavior of this role:

# The frequency to perform the backup
backup_frequency_minutes: 5

# User who owns the files to backup/restore
backup_user: # Required

# The source directory containing the files to backup (must end with a /)
backup_src:  # Required

# The destination directory to backup the files to (must end with a /)
backup_dest: # Required

# The rsync filter of files/directories to include/exclude
backup_filter: |
  !
  # Include everything
  + /*

# Directory for backup scripts
backup_script_dir: '~/.backup'

# The name to use for cron
backup_cron_name: backup

# The name to use for systemd service
backup_service_name: backup

Example Playbooks

Here's a simple example playbook:

- hosts: servers
  roles:
    - role: gantsign.backup
      backup_user: example_username
      backup_src: /home/example_username/
      backup_dest: /mnt/backup/example_username/
      backup_filter: |
        # Reset filter
        !
        # Include file / directory
        + /include_me
        # Include subdirectory (only needed of you're going to exclude other sub-directories)
        + /include_me/me_as_well
        # Exclude everything but me_as_well
        - /include_me/*
        # Exclude by file/directory name
        - tmp
        # Exclude everything else
        - /*

Here's a real world example playbook:

- hosts: servers
  roles:
    - role: gantsign.backup
      backup_user: vagrant
      backup_src: /home/vagrant/
      backup_dest: /var/persistent/home/vagrant/
      backup_filter: |
        !
        + /.atom
        + /.atom
        + /.atom/config.cson
        - /.atom/*
        + /.bash_history
        + /.config
        + /.config/Code
        + /.config/Code/User
        + /.config/Code/User/settings.json
        - /.config/Code/User/*
        - /.config/Code/*
        - /.config/*
        + /.gitconfig
        + /.gnupg
        + /.m2
        - /.m2/repository
        - /.m2/wrapper
        + /.ssh
        - /.ssh/authorized_keys
        + /workspace
        + /.zsh_history
        - target/*
        - build/*
        - node_modules
        - /*

More Roles From GantSign

You can find more roles from GantSign on Ansible Galaxy.

Development & Testing

This project uses Molecule to aid in the development and testing; the role is unit tested using Testinfra and pytest.

To develop or test you'll need to have installed the following:

Because the above can be tricky to install, this project includes Molecule Wrapper. Molecule Wrapper is a shell script that installs Molecule and it's dependencies (apart from Linux) and then executes Molecule with the command you pass it.

To test this role using Molecule Wrapper run the following command from the project root:

./moleculew test

Note: some of the dependencies need sudo permission to install.

License

MIT

Author Information

John Freeman

GantSign Ltd. Company No. 06109112 (registered in England)

About

Role for backing up and restoring files and directories.

Install
ansible-galaxy install gantsign/ansible_role_backup
GitHub repository
License
mit
Downloads
2717