From 0d72e9e92031c3f1780c210e29c38dbe3eb49539 Mon Sep 17 00:00:00 2001 From: Timo Makinen Date: Sun, 7 Apr 2024 13:38:21 +0000 Subject: [PATCH] backup_bitbucket: New role --- .../files/backup-bitbucket.sh | 24 ++++++++++++++ roles/backup_bitbucket/meta/main.yml | 3 ++ roles/backup_bitbucket/tasks/main.yml | 32 +++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 roles/backup_bitbucket/files/backup-bitbucket.sh create mode 100644 roles/backup_bitbucket/meta/main.yml create mode 100644 roles/backup_bitbucket/tasks/main.yml diff --git a/roles/backup_bitbucket/files/backup-bitbucket.sh b/roles/backup_bitbucket/files/backup-bitbucket.sh new file mode 100644 index 0000000..a97097e --- /dev/null +++ b/roles/backup_bitbucket/files/backup-bitbucket.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +USERS="tmakinen" + +set -eu +umask 027 + +cd /srv/backup/bitbucket.org + +for _user in $USERS ; do + curl -sSf "https://api.bitbucket.org/2.0/repositories/${_user}" | \ + jq -r '.values | .[] | [.name, .scm] | @tsv' | \ + while read -r _repo _scm + do + [ "$_scm" = "git" ] || continue + _url="https://bitbucket.org/${_user}/${_repo}" + _gitdir="${_user}/${_repo}" + if [ ! -d "$_gitdir" ]; then + mkdir -p "$_gitdir" + git --git-dir="$_gitdir" init --quiet --bare + fi + git --git-dir="$_gitdir" fetch --quiet --force --prune --tags "$_url" "refs/heads/*:refs/heads/*" + done +done diff --git a/roles/backup_bitbucket/meta/main.yml b/roles/backup_bitbucket/meta/main.yml new file mode 100644 index 0000000..9eea2ce --- /dev/null +++ b/roles/backup_bitbucket/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - {role: backup_server} diff --git a/roles/backup_bitbucket/tasks/main.yml b/roles/backup_bitbucket/tasks/main.yml new file mode 100644 index 0000000..d41605a --- /dev/null +++ b/roles/backup_bitbucket/tasks/main.yml @@ -0,0 +1,32 @@ +--- +- name: Install dependencies + ansible.builtin.package: + name: "{{ item }}" + state: installed + with_items: + - git + - jq + +- name: Create backup directory + ansible.builtin.file: + path: /srv/backup/bitbucket.org + state: directory + mode: "0770" + owner: root + group: backup + +- name: Copy backup script + ansible.builtin.copy: + dest: /usr/local/sbin/backup-bitbucket + src: backup-bitbucket.sh + mode: "0755" + owner: root + group: "{{ ansible_wheel }}" + +- name: Add cron job + ansible.builtin.cron: + name: bitbucket-backup + job: /usr/local/sbin/backup-bitbucket + hour: "03" + minute: "10" + user: backup