summaryrefslogtreecommitdiffstats
path: root/roles/ands_backup
diff options
context:
space:
mode:
authorSuren A. Chilingaryan <csa@suren.me>2018-03-11 19:56:38 +0100
committerSuren A. Chilingaryan <csa@suren.me>2018-03-11 19:56:38 +0100
commitf3c41dd13a0a86382b80d564e9de0d6b06fb1dbf (patch)
tree3522ce77203da92bb2b6f7cfa2b0999bf6cc132c /roles/ands_backup
parent6bc3a3ac71e11fb6459df715536fec373c123a97 (diff)
downloadands-f3c41dd13a0a86382b80d564e9de0d6b06fb1dbf.tar.gz
ands-f3c41dd13a0a86382b80d564e9de0d6b06fb1dbf.tar.bz2
ands-f3c41dd13a0a86382b80d564e9de0d6b06fb1dbf.tar.xz
ands-f3c41dd13a0a86382b80d564e9de0d6b06fb1dbf.zip
Various fixes before moving to hardware installation
Diffstat (limited to 'roles/ands_backup')
-rw-r--r--roles/ands_backup/defaults/main.yml9
-rw-r--r--roles/ands_backup/tasks/main.yml29
-rw-r--r--roles/ands_backup/templates/backup.cron.j24
-rwxr-xr-xroles/ands_backup/templates/backup.sh.j272
4 files changed, 114 insertions, 0 deletions
diff --git a/roles/ands_backup/defaults/main.yml b/roles/ands_backup/defaults/main.yml
new file mode 100644
index 0000000..33d1ff1
--- /dev/null
+++ b/roles/ands_backup/defaults/main.yml
@@ -0,0 +1,9 @@
+ands_script_path: "/opt/scripts"
+
+ands_backup_frequency: "17 */4 * * *"
+ands_backup_volume: "{{ ands_paths.provision }}"
+ands_backup_path: "{{ ands_backup_volume }}/backup"
+ands_backup_clean_minutes: "720"
+ands_borg_path: "{{ ands_backup_volume }}/borg"
+ands_borg_args: "-C zlib,6 -x"
+ands_borg_prune: "--keep-daily=7 --keep-weekly=4 --keep-monthly=6 --keep-within 1w"
diff --git a/roles/ands_backup/tasks/main.yml b/roles/ands_backup/tasks/main.yml
new file mode 100644
index 0000000..16a8ec3
--- /dev/null
+++ b/roles/ands_backup/tasks/main.yml
@@ -0,0 +1,29 @@
+- name: Install required packages
+ package: name={{item}} state=present
+ with_items:
+ - borgbackup
+ - heketi-client
+
+- name: Create scripts directory
+ file: path="{{ ands_script_path }}" state=directory
+
+- name: Populate backup script
+ template: src=backup.sh.j2 dest="{{ ands_script_path }}/ands_backup.sh" owner=root group=root mode=0755
+
+- name: Populate cron job
+ template: src=backup.cron.j2 dest="/etc/cron.d/9ands_backup" owner=root group=root mode=0644
+
+
+- name: Check if backup volume is mounted
+ command: mountpoint -q "{{ ands_backup_volume }}"
+
+
+- block:
+ - name: Check if borg is already initialized
+ stat: path="{{ ands_borg_path }}/config"
+ register: borg_stat_res
+
+ - name: Initialize borg repository
+ shell: "borg init {{ ands_borg_path }} --encryption=none"
+ when: not borg_stat_res.stat.exists
+ run_once: true
diff --git a/roles/ands_backup/templates/backup.cron.j2 b/roles/ands_backup/templates/backup.cron.j2
new file mode 100644
index 0000000..5c017b8
--- /dev/null
+++ b/roles/ands_backup/templates/backup.cron.j2
@@ -0,0 +1,4 @@
+SHELL=/bin/bash
+PATH=/sbin:/bin:/usr/sbin:/usr/bin
+MAILTO=root
+{{ ands_backup_frequency }} root /bin/bash {{ ands_script_path }}/ands_backup.sh
diff --git a/roles/ands_backup/templates/backup.sh.j2 b/roles/ands_backup/templates/backup.sh.j2
new file mode 100755
index 0000000..74fff85
--- /dev/null
+++ b/roles/ands_backup/templates/backup.sh.j2
@@ -0,0 +1,72 @@
+#! /bin/bash
+
+date=$(date -u "+%Y%m%d_%H%M%S")
+hostname=$(hostname)
+
+volume_path="{{ ands_backup_volume }}"
+host_path="{{ ands_backup_path }}/${hostname}"
+backup_path="${host_path}/${date}"
+borg_path="{{ ands_borg_path }}"
+
+borg_args="{{ ands_borg_args }}"
+borg_prune_args="{{ ands_borg_prune }}"
+
+etcdctl3 () {
+ ETCDCTL_API=3 /usr/bin/etcdctl --cert /etc/etcd/peer.crt --key /etc/etcd/peer.key --cacert /etc/etcd/ca.crt --endpoints "https://${hostname}:2379" ${@}
+}
+
+
+check=$(df | awk '{ print $6 }' | grep -P "^${volume_path}$")
+[ $? -ne 0 -o -z "$check" ] && { echo "The volume $volume_path is not mounted. Skipping..." ; exit 1 ; }
+
+[ -d "$backup_path" ] && { echo "Something wrong, path $backup_path already exists..." ; exit 1 ; }
+
+# Check the provision volume is mounted
+mkdir -p "$backup_path" || { echo "Can't create ${backup_path}" ; exit 1 ; }
+
+{% if 'masters' in group_names %}
+# etcd
+mkdir -p "$backup_path/etcd" || { echo "Can't create ${backup_path}/etcd" ; exit 1 ; }
+etcdctl3 --endpoints="192.168.213.1:2379" snapshot save "$backup_path/etcd/snapshot.db" > /dev/null
+
+# heketi
+mkdir -p "$backup_path/heketi" || { echo "Can't create ${backup_path}/heketi" ; exit 1 ; }
+heketi-cli -s http://heketi-storage.glusterfs.svc.cluster.local:8080 --user admin --secret "$(oc get secret heketi-storage-admin-secret -n glusterfs -o jsonpath='{.data.key}' | base64 -d)" topology info --json > "$backup_path/heketi/topology.json"
+{% endif %}
+
+
+{% if 'ands_storage_servers' in group_names %}
+# Gluster
+#mkdir -p "$backup_path/gluster" || { echo "Can't create ${backup_path}/gluster" ; exit 1 ; }
+#(
+# cd /var/lib/
+# tar cjf $backup_path/gluster/var_lib_glusterd.tar.bz2 glusterd
+#)
+{% endif %}
+
+# etc
+#mkdir -p "$backup_path/etc" || { echo "Can't create ${backup_path}/etc" ; exit 1 ; }
+#(
+# cd /
+# tar cjf $backup_path/etc/etc.tar.bz2 etc --exclude=selinux --exclude=udev --exclude=bash_completion.d --exclude=etc/pki --exclude=etc/services --exclude=postfix --exclude=mc
+#)
+
+if [ -d "$borg_path" ]; then
+ borg_glusterd="/var/lib/glusterd"
+ borg_etc="/etc -e */etc/selinux -e */etc/udev -e */etc/bash_completion.d -e */etc/pki -e */etc/services -e */etc/postfix -e */etc/mc"
+
+{% if 'masters' in group_names %}
+ borg_list="* ${borg_glusterd} ${borg_etc}"
+{% elif 'ands_storage_servers' in group_names %}
+ borg_list="${borg_glusterd} ${borg_etc}"
+{% else %}
+ borg_list="${borg_etc}"
+{% endif %}
+
+ (
+ cd ${backup_path}
+ borg create ${borg_args} "$borg_path::${hostname}-${date}" $borg_list
+ borg prune ${borg_prune_args} --prefix "${hostname}-" "$borg_path"
+ )
+ find "$host_path" -maxdepth 1 -type d -mmin +{{ands_backup_clean_minutes}} -print0 | xargs -0 rm -rf
+fi