diff --git a/delete.sh b/delete.sh index ee93aa52b8cd0dff825cfafe0157a569429d563a..e79f387be418d013d525e5b7d5ffaf0a0a1f574c 100755 --- a/delete.sh +++ b/delete.sh @@ -14,3 +14,4 @@ kubectl delete pvc \ redis-data-$releaseName-redis-master-0 \ redis-data-$releaseName-redis-slave-0 \ redis-data-$releaseName-redis-slave-1 + diff --git a/values-local.yaml.example b/values-local.yaml.example index ffa1882bcad97c745dd0de7fd9026a6c7e7d7837..dabad8e6091ee82ef4b81c7d97a5312d8859359f 100644 --- a/values-local.yaml.example +++ b/values-local.yaml.example @@ -173,6 +173,10 @@ redis: # # file. You can use `ssh-keyscan` on a trusted network to find host keys. # sshKnownHosts: | # <hostname> <keytype> <key> +# # when isDate is true or unset the database is backed up as wp-db-RELEASE-DATE.sql +# # If isDate is set to false then backup names are a 2 week cycle of A(day number) or B(day number) +# # A monthly database backup and monthly wordpress manifest are always made with monthnumber prefix +# isDate: true # # The interval at which backups occur. Defaults to 86400 seconds (24 hours) # intervalSeconds: # It's advisable to set resource limits to prevent your K8s cluster from @@ -180,7 +184,7 @@ redis: # resources: # limits: # cpu: 100m -# memory: 512Mi +# memory: 512i # requests: # cpu: 50m # memory: 256Mi diff --git a/values.yaml b/values.yaml index c04295f436753510f2bab36e92b6db31eff43a45..3a43d298b9d7671ec073c4a96725c78b432580b8 100644 --- a/values.yaml +++ b/values.yaml @@ -337,6 +337,7 @@ redis: backup: enabled: false intervalSeconds: 86400 + isDate: true wpSalts: {} @@ -346,6 +347,7 @@ wpSalts: {} ansibleSecrets: | BACKUP_NAME: {{ .Release.Name }} BACKUP_TARGET: {{ .Values.backup.target }} + BACKUP_ISDATE: {{ .Values.backup.isDate }} DB_HOST: {{ .Release.Name }}-database DB_NAME: {{ .Values.database.db.name }} DB_PASS: {{ .Values.database.db.password }} diff --git a/wp-cli-docker/roles/wordpress-backup/tasks/main.yml b/wp-cli-docker/roles/wordpress-backup/tasks/main.yml index fdb682306ac198cb0ec4bf2c9fd4b728d13b26c5..3211517c804a88731e26965db4d70d067f6715e8 100644 --- a/wp-cli-docker/roles/wordpress-backup/tasks/main.yml +++ b/wp-cli-docker/roles/wordpress-backup/tasks/main.yml @@ -1,12 +1,24 @@ --- -- name: Get date +- name: Backup - Get date shell: date '+%F' register: date changed_when: false -- block: +- name: Backup - monthnumber for monthly prefix and daydate to check day of the month + set_fact: + daydate: "{{ lookup('pipe', 'date +%d') }}" + monthnumber: "{{ lookup('pipe', 'date +%m') }}" + +- name: Backup - Make DB filename + set_fact: + backup_filename: "wp-db-{{ BACKUP_NAME }}-{{ date.stdout }}.sql" + +## DATE BACKUP + +- name: Date backup - RSync + block: - name: Create temporary backups directory tempfile: state: directory @@ -14,13 +26,101 @@ register: backup_dir - name: Export WordPress database to file shell: - wp {{ cli_args }} db export "{{ backup_dir.path }}/{{ backup_filename }}" - - name: Copy export to backup location + wp {{ cli_args }} db export --add-drop-table --porcelain "{{ backup_dir.path }}/{{ backup_filename }}" + - name: Monthly DB backup + shell: + wp {{ cli_args }} db export --add-drop-table --porcelain "{{ backup_dir.path }}/{{ BACKUP_NAME }}-month{{ monthnumber }}.db.sql" + when: (daydate | int) == 1 + - name: Core manifest month + shell: + wp {{ cli_args }} core version --extra >> "{{ backup_dir.path }}/wp-manifest-{{ BACKUP_NAME }}-month{{ monthnumber}}.txt" + when: (daydate | int) == 1 + - name: Plugin manifest month + shell: + wp {{ cli_args }} plugin list --fields=name,version,update,status >> "{{ backup_dir.path }}/wp-manifest-{{ BACKUP_NAME }}-month{{ monthnumber}}.txt" + when: (daydate | int) == 1 + - name: Theme manifest month + shell: + wp {{ cli_args }} theme list --fields=name,version,update,status >> "{{ backup_dir.path }}/wp-manifest-{{ BACKUP_NAME }}-month{{ monthnumber}}.txt" + when: (daydate | int) == 1 + - name: Copy the export to backup location shell: rsync -a "{{ backup_dir.path }}/{{ backup_filename }}" "{{ BACKUP_TARGET }}" + - name: Copy the export of monthly backup to backup location + shell: + rsync -a "{{ backup_dir.path }}/{{ BACKUP_NAME }}-month{{ monthnumber }}.db.sql" "{{ BACKUP_TARGET }}" + when: (daydate | int) == 1 + - name: Copy the export of monthly wp manifest to backup location + shell: + rsync -a "{{ backup_dir.path }}/wp-manifest-{{ BACKUP_NAME }}-month{{ monthnumber}}.txt" "{{ BACKUP_TARGET }}" + when: (daydate | int) == 1 - name: Remove temporary backups directory file: state: absent path: "{{ backup_dir.path }}/" - vars: - backup_filename: "wp-db-{{ BACKUP_NAME }}-{{ date.stdout }}.sql" + when: (BACKUP_ISDATE is undefined) or (BACKUP_ISDATE|bool == True) + +## CYCLE BACKUP + +- name: Cycle backup - two week prefix look up week number and day number + set_fact: + weeknumber: "{{ lookup('pipe', 'date +%U') }}" + daynumber: "{{ lookup('pipe', 'date +%u') }}" + when: not BACKUP_ISDATE + +- name: Cycle backup - two week prefix Week A is Odd + set_fact: + prefix: "A" + when: ( weeknumber | int) is odd + when: BACKUP_ISDATE|bool == false + + +- name: Cycle backup - two week prefix Week B is Even + set_fact: + prefix: "B" + when: ( weeknumber | int) is even + when: BACKUP_ISDATE|bool == false + +- name: Cycle backup - Rsync database exports + block: + - name: Cycle - Create temporary backups directory + tempfile: + state: directory + suffix: backup + register: backup_dir + - name: Cycle - Export WordPress database with cycle prefix and weekday number to file + shell: + wp {{ cli_args }} db export --add-drop-table --porcelain "{{ backup_dir.path }}/{{ prefix }}{{ daynumber}}-{{ BACKUP_NAME }}.db.sql" + - name: Cycle - Monthly DB backup + shell: + wp {{ cli_args }} db export --add-drop-table --porcelain "{{ backup_dir.path }}/month{{ monthnumber }}-{{ BACKUP_NAME }}.db.sql" + when: (daydate | int) == 1 + - name: Core manifest day + shell: + wp {{ cli_args }} core version --extra >> "{{ backup_dir.path }}/{{ prefix }}{{ daynumber}}-manifest.txt" + - name: Plugin manifest day + shell: + wp {{ cli_args }} plugin list --fields=name,version,update,status >> "{{ backup_dir.path }}/{{ prefix }}{{ daynumber}}-manifest.txt" + - name: Theme manifest day + shell: + wp {{ cli_args }} theme list --fields=name,version,update,status >> "{{ backup_dir.path }}/{{ prefix }}{{ daynumber}}-manifest.txt" + - name: Cycle - RSync day DB export to backup location + shell: + rsync -a "{{ backup_dir.path }}/{{ prefix }}{{ daynumber}}-{{ BACKUP_NAME }}.db.sql" "{{ BACKUP_TARGET }}" + - name: Cycle - RSync monthly DB export to backup location + shell: + rsync -a "{{ backup_dir.path }}/month{{ monthnumber }}-{{ BACKUP_NAME }}.db.sql" "{{ BACKUP_TARGET }}" + when: (daydate | int) == 1 + - name: Cycle - RSync day wordpress manifest to backup location + shell: + rsync -a "{{ backup_dir.path }}/{{ prefix }}{{ daynumber}}-manifest.txt" "{{ BACKUP_TARGET }}/wordpress/" + - name: Cycle - Remove temporary backups directory + file: + state: absent + path: "{{ backup_dir.path }}/" + when: BACKUP_ISDATE|bool == false + +- name: Cycle backup - Rsync media files to biweekly prefix directory + shell: + rsync -a --no-links --exclude '\.htaccess' --exclude='*cache*' --exclude='*backup*' --exclude='*.php' --exclude='*gz' --exclude='*.zip' "{{ WP_UPLOAD_DIR }}/" "{{ BACKUP_TARGET }}/media-{{prefix}}/" + when: BACKUP_ISDATE|bool == false