|
| 1 | +#!/usr/bin/env bash |
| 2 | +# shellcheck disable=SC2086,SC2153 |
| 3 | + |
| 4 | +set -eo pipefail |
| 5 | + |
| 6 | +export AWS_ACCESS_KEY_ID="${RESTIC_S3_ACCESS_KEY_ID}" |
| 7 | +export AWS_SECRET_ACCESS_KEY="${RESTIC_S3_SECRET_ACCESS_KEY}" |
| 8 | +export AWS_DEFAULT_REGION="${RESTIC_S3_REGION}" |
| 9 | + |
| 10 | +function try_restic_initialization() { |
| 11 | + local restic_global_args=${1} |
| 12 | + local restic_host=${2} |
| 13 | + |
| 14 | + if [[ -z "${restic_host}" ]]; then |
| 15 | + echo "Variable restic_host is required, exiting" |
| 16 | + exit 1 |
| 17 | + fi |
| 18 | + |
| 19 | + restic snapshots ${restic_global_args} --host ${restic_host} || |
| 20 | + ( |
| 21 | + echo "Initializing the restic repository" && |
| 22 | + restic init ${restic_global_args} |
| 23 | + ) && |
| 24 | + ( |
| 25 | + echo "The restic repository has already been initialized" |
| 26 | + ) |
| 27 | +} |
| 28 | + |
| 29 | +function init() { |
| 30 | + if [[ -z "${RESTIC_REPOSITORY}" ]]; then |
| 31 | + echo "Environment variable RESTIC_REPOSITORY not set, exiting" |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | + |
| 35 | + if [[ -z "${RESTIC_S3_REGION}" ]]; then |
| 36 | + echo "Environment variable RESTIC_S3_REGION not set, exiting" |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + |
| 40 | + if [[ -z "${RESTIC_PASSWORD}" ]]; then |
| 41 | + echo "Environment variable RESTIC_PASSWORD not set, exiting" |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | + |
| 45 | + if [[ -z "${RESTIC_S3_ACCESS_KEY_ID}" ]]; then |
| 46 | + echo "Environment variable RESTIC_S3_ACCESS_KEY_ID not set, exiting" |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | + |
| 50 | + if [[ -z "${RESTIC_S3_SECRET_ACCESS_KEY}" ]]; then |
| 51 | + echo "Environment variable RESTIC_S3_SECRET_ACCESS_KEY not set, exiting" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + |
| 55 | + if [[ -z "${RESTIC_HOST}" ]]; then |
| 56 | + echo "Environment variable RESTIC_HOST not set, exiting" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + |
| 60 | + if [[ -z "${RESTIC_GLOBAL_ARGS}" ]]; then |
| 61 | + try_restic_initialization --host "${RESTIC_HOST}" |
| 62 | + else |
| 63 | + try_restic_initialization "${RESTIC_GLOBAL_ARGS}" --host "${RESTIC_HOST}" |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +function execute_pg_dump() { |
| 68 | + local postgresql_password=${1} |
| 69 | + local postgresql_host=${2} |
| 70 | + local postgresql_port=${3} |
| 71 | + local postgresql_user=${4} |
| 72 | + local postgresql_database=${5} |
| 73 | + local postgresql_additional_args=${6} |
| 74 | + |
| 75 | + if [[ ! -d /home/saleor/backups/database ]]; then |
| 76 | + mkdir -p /home/saleor/backups/database |
| 77 | + fi |
| 78 | + |
| 79 | + echo "Dumping postgresql database with the following arguments:" |
| 80 | + echo "host: ${postgresql_host}" |
| 81 | + echo "port: ${postgresql_port}" |
| 82 | + echo "username: ${postgresql_user}" |
| 83 | + echo "dbname: ${postgresql_database}" |
| 84 | + |
| 85 | + if [[ -z "${postgresql_additional_args}" ]]; then |
| 86 | + PGPASSWORD="${postgresql_password}" \ |
| 87 | + pg_dump \ |
| 88 | + --verbose \ |
| 89 | + --host="${postgresql_host}" \ |
| 90 | + --port="${postgresql_port}" \ |
| 91 | + --username="${postgresql_user}" \ |
| 92 | + --dbname="${postgresql_database}" \ |
| 93 | + --no-password >/home/saleor/backups/database/saleor-core-postgresql.dump |
| 94 | + else |
| 95 | + echo "additional arguments: ${postgresql_additional_args}" |
| 96 | + PGPASSWORD="${postgresql_password}" \ |
| 97 | + pg_dump \ |
| 98 | + ${postgresql_additional_args} \ |
| 99 | + --host="${postgresql_host}" \ |
| 100 | + --port="${postgresql_port}" \ |
| 101 | + --username="${postgresql_user}" \ |
| 102 | + --dbname="${postgresql_database}" \ |
| 103 | + --no-password >/home/saleor/backups/database/saleor-core-postgresql.dump |
| 104 | + fi && |
| 105 | + echo "Finished pg_dump successfully" || |
| 106 | + echo "Finished pg_dump unsuccessfuly" |
| 107 | +} |
| 108 | + |
| 109 | +function restic_save_db() { |
| 110 | + local restic_global_args=${1} |
| 111 | + local restic_host=${2} |
| 112 | + |
| 113 | + restic backup \ |
| 114 | + ${restic_global_args} \ |
| 115 | + --host "${restic_host}-db" \ |
| 116 | + /home/saleor/backups/database/saleor-core-postgresql.dump && |
| 117 | + echo "Saved media backup via restic tool successfully" || |
| 118 | + echo "Saved media backup via restic tool unsuccessfully" |
| 119 | +} |
| 120 | + |
| 121 | +function restic_save_media() { |
| 122 | + local restic_global_args=${1} |
| 123 | + local restic_host=${2} |
| 124 | + |
| 125 | + restic backup \ |
| 126 | + ${restic_global_args} \ |
| 127 | + --host "${restic_host}-media" \ |
| 128 | + /app/media && |
| 129 | + echo "Saved media backup via restic tool successfully" || |
| 130 | + echo "Saved media backup via restic tool unsuccessfully" |
| 131 | +} |
| 132 | + |
| 133 | +function display_snapshots() { |
| 134 | + restic snapshots --verbose --no-cache |
| 135 | +} |
| 136 | + |
| 137 | +function main() { |
| 138 | + local do_postgresql_backup=${1} |
| 139 | + local postgresql_password=${2} |
| 140 | + local postgresql_host=${3} |
| 141 | + local postgresql_port=${4} |
| 142 | + local postgresql_user=${5} |
| 143 | + local postgresql_database=${6} |
| 144 | + local postgresql_additional_args=${7} |
| 145 | + local do_media_backup=${8} |
| 146 | + local restic_global_args=${9} |
| 147 | + local restic_host=${10} |
| 148 | + |
| 149 | + init |
| 150 | + |
| 151 | + if [[ "${do_postgresql_backup}" == "true" ]]; then |
| 152 | + echo "Postgresql backup is to be executed" |
| 153 | + |
| 154 | + execute_pg_dump \ |
| 155 | + "${postgresql_password}" \ |
| 156 | + "${postgresql_host}" \ |
| 157 | + "${postgresql_port}" \ |
| 158 | + "${postgresql_user}" \ |
| 159 | + "${postgresql_database}" \ |
| 160 | + "${postgresql_additional_args}" |
| 161 | + fi |
| 162 | + if [[ "${do_media_backup}" == "true" ]]; then |
| 163 | + echo "Saving media files using restic" |
| 164 | + |
| 165 | + restic_save_media \ |
| 166 | + "${restic_global_args}" \ |
| 167 | + "${restic_host}" |
| 168 | + fi |
| 169 | + if [[ "${do_postgresql_backup}" == "true" ]]; then |
| 170 | + echo "Saving postgresql backup using restic" |
| 171 | + |
| 172 | + restic_save_db \ |
| 173 | + "${restic_global_args}" \ |
| 174 | + "${restic_host}" |
| 175 | + fi |
| 176 | + |
| 177 | + display_snapshots |
| 178 | +} |
| 179 | + |
| 180 | +main "${@}" |
0 commit comments