Skip to content

Instantly share code, notes, and snippets.

@thomasmassmann
Last active August 19, 2016 13:25
Show Gist options
  • Save thomasmassmann/6ede6ab6b0e6f3668f3043843fe4d0f1 to your computer and use it in GitHub Desktop.
Save thomasmassmann/6ede6ab6b0e6f3668f3043843fe4d0f1 to your computer and use it in GitHub Desktop.
Manage Plone database backups with ansible.
---
- name: Download a copy of the ZODB and blobstorage.
hosts: plone-db
gather_facts: true
tasks:
- name: Get current date and time.
set_fact:
date: '{{ lookup("pipe", "date +%Y-%m-%d-%H-%M") }}'
- name: Get current working directory.
shell: pwd
delegate_to: 127.0.0.1
register: pwd
changed_when: false
- name: Ensure download directory exists.
file:
path: '{{ pwd.stdout }}/data/{{ inventory_hostname }}/zodb/{{ date }}'
state: directory
delegate_to: 127.0.0.1
register: new_date
- name: Create Data.fs zipbackup.
command: '{{ plone_target_path }}/bin/zipbackup'
become: true
become_user: '{{ plone_user }}'
register: zipbackup
when: new_date.changed
- name: Download zipbackup files.
synchronize:
mode: pull
src: '{{ plone_backup_path }}/{{ item }}'
dest: '{{ pwd.stdout }}/data/{{ inventory_hostname }}/zodb/{{ date }}'
with_items:
- 'blobstoragezips'
- 'zipbackups'
when: new_date.changed and zipbackup | succeeded
become: false
register: download
- name: Ensure latest downloads are linked.
file:
src: '{{ pwd.stdout }}/data/{{ inventory_hostname }}/zodb/{{ date }}'
dest: '{{ pwd.stdout }}/data/{{ inventory_hostname }}/zodb/latest'
state: link
delegate_to: 127.0.0.1
when: new_date.changed and zipbackup | succeeded and download | succeeded
---
- name: Restore the ZODB and blobstorage from a zipped backup.
hosts: plone-db
gather_facts: true
become: true
pre_tasks:
- name: Create a snapshotbackup just in case something goes wrong.
command: '{{ plone_target_path }}/bin/snapshotbackup'
become_user: '{{ plone_user }}'
- name: Ensure Plone zeoserver is stopped.
supervisorctl:
name: '{{ plone_instance_name }}_zeoserver'
state: stopped
post_tasks:
- name: Ensure Plone zeoserver is started.
supervisorctl:
name: '{{ plone_instance_name }}_zeoserver'
state: started
tasks:
- block:
- name: Restore database with ziprestore.
command: '{{ plone_target_path }}/bin/ziprestore -n'
become_user: '{{ plone_user }}'
register: ziprestore
rescue:
- name: Restore from snapshotbackup.
command: '{{ plone_target_path }}/bin/snapshotrestore -n'
become_user: '{{ plone_user }}'
---
- name: Upload a local copy of the ZODB and blobstorage.
hosts: plone-db
gather_facts: true
vars:
zipbackups:
- 'blobstoragezips'
- 'zipbackups'
tasks:
- name: Get current working directory.
shell: pwd
delegate_to: 127.0.0.1
register: pwd
changed_when: false
- name: Ensure upload directory exists.
stat:
path: '{{ pwd.stdout }}/data/{{ inventory_hostname }}/zodb/upload'
delegate_to: 127.0.0.1
register: data
- block:
- name: Upload zipbackup files.
synchronize:
mode: push
src: '{{ pwd.stdout }}/data/{{ inventory_hostname }}/zodb/upload/{{ item }}'
dest: '/tmp/'
with_items: '{{ zipbackups }}'
become: false
register: upload
- name: Ensure correct ownership settings of synchronized files.
file:
path: '/tmp/{{ item }}'
owner: '{{ plone_user }}'
group: '{{ plone_group }}'
recurse: yes
with_items: '{{ zipbackups }}'
become: true
when: upload.changed
- name: Move zipbackups into place.
shell: 'mv /tmp/{{ item }}/* {{ plone_backup_path }}/{{ item }}'
with_items: '{{ zipbackups }}'
become: true
become_user: '{{ plone_user }}'
when: upload.changed
always:
- name: Remove uploaded directories.
file:
path: '/tmp/{{ item }}'
state: absent
with_items: '{{ zipbackups }}'
become: true
when: data.stat.islnk is defined and data.stat.islnk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment