Backup containers w/Ansible – closingtags </>
Categories
Linux Server

Backup containers w/Ansible

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.

By Dylan Hildenbrand

Author and full stack web developer experienced with #PHP, #SvelteKit, #JS, #NodeJS, #Linux, #WordPress, and #Ansible. Check out my book at sveltekitbook.dev!

Do you like these posts? Consider sponsoring me on GitHub!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.