From 192645dddf52831a2d32118f7aeacfe034a99662 Mon Sep 17 00:00:00 2001 From: itswl Date: Tue, 15 Nov 2022 14:16:36 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9ectd=20=E5=A4=87=E4=BB=BD?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E5=92=8C=E5=A4=87=E4=BB=BD=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=9D=87=E5=9C=A8=20ansible=20=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- playbooks/94.backup.yml | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/playbooks/94.backup.yml b/playbooks/94.backup.yml index 69452939e..10f70204c 100644 --- a/playbooks/94.backup.yml +++ b/playbooks/94.backup.yml @@ -34,20 +34,17 @@ shell: "date +'%Y%m%d%H%M'" register: timestamp - # step2: backup data on the healthy member + # step2: backup data to the ansible node - name: make a backup on the etcd node - shell: "mkdir -p /etcd_backup && cd /etcd_backup && \ - ETCDCTL_API=3 {{ bin_dir }}/etcdctl snapshot save snapshot_{{ timestamp.stdout }}.db" + shell: "mkdir -p {{ cluster_dir }}/backup && cd {{ cluster_dir }}/backup && \ + ETCDCTL_API=3 {{ base_dir }}/bin/etcdctl \ + --endpoints=https://{{ RUNNING_NODE.stdout }}:2379 \ + --cacert={{ cluster_dir }}/ssl/ca.pem \ + --cert={{ cluster_dir }}/ssl/etcd.pem \ + --key={{ cluster_dir }}/ssl/etcd-key.pem \ + snapshot save snapshot_{{ timestamp.stdout }}.db" args: warn: false - delegate_to: "{{ RUNNING_NODE.stdout }}" - - - name: fetch the backup data - fetch: - src: /etcd_backup/snapshot_{{ timestamp.stdout }}.db - dest: "{{ cluster_dir }}/backup/" - flat: yes - delegate_to: "{{ RUNNING_NODE.stdout }}" - name: update the latest backup shell: 'cd {{ cluster_dir }}/backup/ && /bin/cp -f snapshot_{{ timestamp.stdout }}.db snapshot.db' From 2b69f0008d20cb665e3a7308e2e5a823942fbb17 Mon Sep 17 00:00:00 2001 From: itswl Date: Wed, 16 Nov 2022 12:26:46 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20/etcd=5Fbackup=20?= =?UTF-8?q?=E4=B8=BA{{=20ETCD=5FDATA=5FDIR=20}}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- roles/cluster-restore/tasks/main.yml | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/roles/cluster-restore/tasks/main.yml b/roles/cluster-restore/tasks/main.yml index ddce86390..b05831fcf 100644 --- a/roles/cluster-restore/tasks/main.yml +++ b/roles/cluster-restore/tasks/main.yml @@ -4,19 +4,16 @@ - name: 清除etcd 数据目录 file: name={{ ETCD_DATA_DIR }}/member state=absent -- name: 生成备份目录 - file: name=/etcd_backup state=directory - - name: 准备指定的备份etcd 数据 copy: src: "{{ cluster_dir }}/backup/{{ db_to_restore }}" - dest: "/etcd_backup/snapshot.db" + dest: "{{ ETCD_DATA_DIR }}/snapshot.db" - name: 清理上次备份恢复数据 - file: name=/etcd_backup/etcd-{{ inventory_hostname }}.etcd state=absent + file: name={{ ETCD_DATA_DIR }}/etcd-{{ inventory_hostname }}.etcd state=absent - name: etcd 数据恢复 - shell: "cd /etcd_backup && \ + shell: "cd {{ ETCD_DATA_DIR }} && \ ETCDCTL_API=3 {{ bin_dir }}/etcdctl snapshot restore snapshot.db \ --name etcd-{{ inventory_hostname }} \ --initial-cluster {{ ETCD_NODES }} \ @@ -24,7 +21,7 @@ --initial-advertise-peer-urls https://{{ inventory_hostname }}:2380" - name: 恢复数据至etcd 数据目录 - shell: "cp -rf /etcd_backup/etcd-{{ inventory_hostname }}.etcd/member {{ ETCD_DATA_DIR }}/" + shell: "cp -rf {{ ETCD_DATA_DIR }}/etcd-{{ inventory_hostname }}.etcd/member {{ ETCD_DATA_DIR }}/" - name: 重启etcd 服务 service: name=etcd state=restarted From 56b25763cd020687c2dad888782c8e1bdcda697e Mon Sep 17 00:00:00 2001 From: itswl Date: Wed, 16 Nov 2022 17:31:44 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9etcd=20=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8Cansible=20=E4=B8=BB=E6=8E=A7?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E7=94=9F=E6=88=90=E6=81=A2=E5=A4=8D=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20=E7=84=B6=E5=90=8E=E5=88=86=E5=8F=91=E5=88=B0?= =?UTF-8?q?=E5=90=84=E4=B8=AA=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- roles/cluster-restore/tasks/main.yml | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/roles/cluster-restore/tasks/main.yml b/roles/cluster-restore/tasks/main.yml index b05831fcf..07aa4d883 100644 --- a/roles/cluster-restore/tasks/main.yml +++ b/roles/cluster-restore/tasks/main.yml @@ -4,24 +4,20 @@ - name: 清除etcd 数据目录 file: name={{ ETCD_DATA_DIR }}/member state=absent -- name: 准备指定的备份etcd 数据 - copy: - src: "{{ cluster_dir }}/backup/{{ db_to_restore }}" - dest: "{{ ETCD_DATA_DIR }}/snapshot.db" - -- name: 清理上次备份恢复数据 - file: name={{ ETCD_DATA_DIR }}/etcd-{{ inventory_hostname }}.etcd state=absent +- name: 清除 etcd 备份目录 + file: name={{ cluster_dir }}/backup/etcd-restore state=absent + delegate_to: 127.0.0.1 + run_once: true - name: etcd 数据恢复 - shell: "cd {{ ETCD_DATA_DIR }} && \ - ETCDCTL_API=3 {{ bin_dir }}/etcdctl snapshot restore snapshot.db \ - --name etcd-{{ inventory_hostname }} \ - --initial-cluster {{ ETCD_NODES }} \ - --initial-cluster-token etcd-cluster-0 \ - --initial-advertise-peer-urls https://{{ inventory_hostname }}:2380" + shell: "cd {{ cluster_dir }}/backup && \ + ETCDCTL_API=3 {{ base_dir }}/bin/etcdctl snapshot restore snapshot.db \ + --data-dir={{ cluster_dir }}/backup/etcd-restore" + delegate_to: 127.0.0.1 + run_once: true -- name: 恢复数据至etcd 数据目录 - shell: "cp -rf {{ ETCD_DATA_DIR }}/etcd-{{ inventory_hostname }}.etcd/member {{ ETCD_DATA_DIR }}/" +- name: 分发恢复文件到 etcd 各个节点 + copy: src={{ cluster_dir }}/backup/etcd-restore/member dest={{ ETCD_DATA_DIR }} - name: 重启etcd 服务 service: name=etcd state=restarted