Added pleroma instance backup to s3.
authorLuke Hoersten <luke@hoersten.org>
Mon, 20 Jan 2020 13:18:18 -0600
changeset 88 63efc98132f7
parent 87 4f87097dd651
child 89 4e1cf6df4ac9
Added pleroma instance backup to s3.
roles/pleroma-otp/defaults/main.yaml
roles/pleroma-otp/files/pleroma-s3-backup.sh
roles/pleroma-otp/files/[email protected]
roles/pleroma-otp/handlers/main.yaml
roles/pleroma-otp/tasks/main.yaml
roles/pleroma-otp/tasks/s3-backup.yaml
--- a/roles/pleroma-otp/defaults/main.yaml	Sun Jan 19 21:51:10 2020 -0600
+++ b/roles/pleroma-otp/defaults/main.yaml	Mon Jan 20 13:18:18 2020 -0600
@@ -18,13 +18,11 @@
 pleroma_db_host: "localhost"
 pleroma_db_superuser: "postgres"
 
-pleroma_apt_packages:
-  - "curl"
-  - "unzip"
-
-pleroma_branch: "stable"
-pleroma_download_url: "https://git.pleroma.social/api/v4/projects/2/jobs/artifacts/{{pleroma_branch}}/download?job={{pleroma_flavor}}"
+pleroma_download_url: "https://git.pleroma.social/api/v4/projects/2/jobs/artifacts/stable/download?job={{pleroma_flavor}}"
 
 pleroma_db: "pleroma_{{pleroma_instance}}"
 pleroma_db_user: "pleroma_{{pleroma_instance}}"
 pleroma_data_dir: "/var/lib/pleroma/instance_data"
+
+pleroma_s3_backup_enabled: true
+pleroma_cleanup_tmp: false
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/roles/pleroma-otp/files/pleroma-s3-backup.sh	Mon Jan 20 13:18:18 2020 -0600
@@ -0,0 +1,26 @@
+#!/bin/bash
+
+INSTANCE=$1
+DATE=`date --iso-8601`
+
+BUCKET="pleroma-${INSTANCE//_/-}-backup"
+BACKUP_DIR="/tmp/s3-backup/$BUCKET"
+BACKUP_TAR="/tmp/s3-backup/$BUCKET-$DATE.tgz"
+
+DB_NAME="pleroma_$INSTANCE"
+CONFIG="/etc/pleroma/$INSTANCE.config.exs"
+
+UPLOADS_DIR=`grep uploads $CONFIG | cut -d '"' -f 2`
+STATIC_DIR=`grep static $CONFIG | cut -d '"' -f 2`
+
+mkdir -m 775 -p "$BACKUP_DIR/"
+chown root:postgres "$BACKUP_DIR/"
+
+su postgres -c "pg_dump -d $DB_NAME --format=custom -f $BACKUP_DIR/$DB_NAME.pgdump"
+cp $CONFIG "$BACKUP_DIR/"
+cp -r $UPLOADS_DIR "$BACKUP_DIR/"
+cp -r $STATIC_DIR "$BACKUP_DIR/"
+
+tar -zc -f $BACKUP_TAR $BACKUP_DIR
+aws s3 mb "s3://$BUCKET/"
+aws s3 cp $BACKUP_TAR "s3://$BUCKET/"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/roles/pleroma-otp/files/[email protected]	Mon Jan 20 13:18:18 2020 -0600
@@ -0,0 +1,9 @@
+[Unit]
+Description=Pleroma s3 backup for instance "%I"
+
+[Service]
+Type=oneshot
+ExecStart=/usr/local/bin/pleroma-prepare-backup.sh %i
+
+[Install]
+WantedBy=aws-s3-backup.target
--- a/roles/pleroma-otp/handlers/main.yaml	Sun Jan 19 21:51:10 2020 -0600
+++ b/roles/pleroma-otp/handlers/main.yaml	Mon Jan 20 13:18:18 2020 -0600
@@ -3,3 +3,7 @@
 - name: restart pleroma instance
   become: yes
   systemd: name="pleroma@{{pleroma_instance}}.service" state="restarted" daemon_reload="yes"
+
+- name: restart pleroma instance s3 backup
+  become: yes
+  systemd: name="pleroma-s3-backup@{{pleroma_instance}}.service" state="restarted" daemon_reload="yes"
--- a/roles/pleroma-otp/tasks/main.yaml	Sun Jan 19 21:51:10 2020 -0600
+++ b/roles/pleroma-otp/tasks/main.yaml	Mon Jan 20 13:18:18 2020 -0600
@@ -1,9 +1,5 @@
 ---
 
-- name: install pleroma apt packages
-  become: yes
-  apt: name="{{pleroma_apt_packages}}"
-
 - name: add users
   become: yes
   user: name="pleroma" shell="/bin/false" home="/opt/pleroma" system="yes"
@@ -72,12 +68,16 @@
   copy: remote_src="True" src="/tmp/release/" dest="/opt/pleroma/" owner="pleroma" group="pleroma"
   notify: restart pleroma instance
 
-# - name: remove tmp release folder
-#   become: yes
-#   file: path="{{item}}" state="absent"
-#   with_items:
-#     - "/tmp/setup_db.psql"
-#     - "/tmp/release/"
+- name: remove tmp release folder
+  become: yes
+  file: path="{{item}}" state="absent"
+  with_items:
+    - "/tmp/setup_db.psql"
+    - "/tmp/release/"
+  when: pleroma_cleanup_tmp
+
+- import_tasks: s3-backup.yml
+  when: pleroma_s3_backup_enabled
 
 - name: configure pleroma systemd service
   become: yes
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/roles/pleroma-otp/tasks/s3-backup.yaml	Mon Jan 20 13:18:18 2020 -0600
@@ -0,0 +1,20 @@
+---
+
+- name: create s3 backup shell script
+  become: yes
+  copy:
+    src: "pleroma-s3-backup.sh"
+    dest: "/usr/local/bin/pleroma-s3-backup.sh"
+    mode: "0755"
+
+- name: configure s3 backup systemd service
+  become: yes
+  copy:
+    src: "[email protected]"
+    dest: "/lib/systemd/system/[email protected]"
+    mode: "0755"
+  notify: restart pleroma instance s3 backup
+
+- name: ensure s3 backup is enabled
+  become: yes
+  systemd: name="pleroma-s3-backup@{{pleroma_instance}}.service" enabled="yes" state="started"