web dev & more!

Backup containers w/Ansible

Published: February 23, 2021

The one thing that you should absolutely be doing routinely is backing up your stuff. Yet you’ll notice in my previous post about using Ansible to automate my homelab, there is no mention of backing up the environment. If you’re wondering why that is, it’s because I am both lazy and forgetful.

Having been bitten one too many times by my fancy automated updates, I decided I’d had enough. I was spending too much time connecting to the Proxmox web GUI, starting a backup, then running updates so if (when) things broke, I’d have access to a more recent backup. I needed to be able to take backups within Ansible as well as run my updates.

This is the solution I came up with:

---
- hosts: host
  remote_user: root

  vars_prompt:
    - name: ctid
      prompt: 'Specify container ID or leave blank for all'
      private: no

  tasks:
      - name: Backup all containers.
        command:
            cmd: vzdump --all --maxfiles 5 --compress gzip
        when: ctid == ""

      - name: Backup specified container.
        command:
            cmd: vzdump {{ ctid }} --maxfiles 5 --compress gzip
        when: ctid != ""

This playbook should be easy enough to understand without too much exlaining but I’ll sum it up: if a container ID is specified when run, use the Proxmox tool vzdump to backup that container; otherwise, backup all containers (compress those files and only keep the most recent 5). Please borrow, tweak, share, and critique.