Add a command to dump the database
authorymh <ymh.work@gmail.com>
Mon, 17 Jun 2019 12:56:05 +0200
changeset 23 5249c3c623a6
parent 22 55b01e4ebc64
child 24 86535a5969b8
Add a command to dump the database
deploy/README.md
deploy/dump_db.sh
deploy/dump_db.yml
--- a/deploy/README.md	Mon Jun 17 12:21:24 2019 +0200
+++ b/deploy/README.md	Mon Jun 17 12:56:05 2019 +0200
@@ -31,3 +31,20 @@
 - delete _site_transient_update_themes and _site_transient_theme_roots options (wp-cli option)
 - update admin password (wp-cli user update)
 - flush cache (wp-cli cache flush)
+
+
+## Database dump
+
+- `dump_db.sh [CONFIG] [DB_FILE_PATH]`
+
+With:
+- `CONFIG`: test or prod
+- `DB_FILE_PATH`: The database definition file (SQL)
+
+This script call the `dump_db.sql` ansible script and perform the following operation
+- create a temporary file on the remote host
+- dump the content of the database into this file (wp-cli db export)
+- fetch the file and write it to the givent path
+- delete the temporary file
+
+**WARNING** : This command write to the destination file and replace it without warning
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/deploy/dump_db.sh	Mon Jun 17 12:56:05 2019 +0200
@@ -0,0 +1,37 @@
+#!/usr/bin/env bash
+
+SCRIPTPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+SCRIPTNAME=`basename "$0"`
+
+usage() {
+  echo -n "${SCRIPTNAME} [CONFIG] [DB_FILE_PATH]
+
+Dump the database to <DB_FILE_PATH> using ansible.
+Beware this will replace the dest file without warning
+"
+}
+
+if [[ "$#" -ne 2 ]]; then
+    usage
+    exit 1
+fi
+
+config=${1}
+db_file_path=${2}
+
+case $config in
+    test) configOK=true;;
+    prod) configOK=true;;
+    *)    configOK=false;;
+esac
+
+if [[ "$configOK" = false ]]; then
+    usage
+    exit 1
+fi
+
+pushd "$SCRIPTPATH"
+
+ANSIBLE_SSH_PIPELINING=1 ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook -v -i "./hosts/hosts.$config" -l "$config" ./dump_db.yml --extra-vars "db_file_path='${db_file_path}'" --ask-vault-pass
+
+popd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/deploy/dump_db.yml	Mon Jun 17 12:56:05 2019 +0200
@@ -0,0 +1,37 @@
+# vendor/bin/wp option delete _site_transient_update_themes
+# vendor/bin/wp option delete _site_transient_theme_roots
+
+- hosts: remote
+  vars:
+    src_dir: "{{playbook_dir}}/build/tmp/rc/src/"
+  tasks:
+
+    - name: create temporary file
+      tempfile:
+        state: file
+        suffix: sql
+      register: sql_tmp_path
+
+    - name: dump and fetch
+      block:
+        - name: dump database
+          command:
+            argv:
+              - "{{remote_path}}/vendor/bin/wp"
+              - db
+              - export
+              - "{{sql_tmp_path.path}}"
+          args:
+            chdir: "{{remote_path}}"
+
+        - name: transfer sql file
+          fetch:
+            src: "{{sql_tmp_path.path}}"
+            dest: "{{db_file_path}}"
+            flat: yes
+      always:
+        - name: delete temporary file
+          file:
+            path: "{{sql_tmp_path.path}}"
+            state: absent
+