Commit 62aa4e8c authored by Ignacio's avatar Ignacio
Browse files

Merge branch 'dev' into 'master'

Arregla tablas en README

See merge request redmic-project/postgres/backup-db!8
parents 35d4af8f 52b87e08
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -9,7 +9,9 @@ ENV POSTGRES_PORT="5432" \
	POSTGRES_PASS_FILE='/root/.pgpass' \
	POSTGRES_DUMP_PATH="/tmp/backup" \
	AWS_DEFAULT_REGION="eu-west-1" \
	AWS_OUTPUT="json"
	PUSHGATEWAY_HOST="pushgateway:9091" \
	AWS_OUTPUT="json" \
	GZIP="-9"

COPY scripts /

README.md

0 → 100644
+31 −0
Original line number Diff line number Diff line
# Backup DB
Backup DB is a docker for create database backup (PostgreSQL)
and uploads to AWS S3. Also sends metrics a Prometheus through
PushGateWay.

![alt schema-db](images/schema-backup-db.png)

# Variables
|          Name         	|          Description          	|      Default      	|
|---------------------------|-----------------------------------|----------------------	|
| POSTGRES_USER         	| Database username             	| postgres          	|
| POSTGRES_PASSWORD     	| Database password             	| password          	|
| POSTGRES_HOSTNAME     	| Database hostname             	| postgresql        	|
| POSTGRES_DUMP_PATH    	| Temporal path                 	| /tmp/backup       	|
| BUCKET_BACKUP_DB      	| Bucket name for upload backup 	|                   	|
| AWS_ACCESS_KEY_ID     	| Credentials AWS               	|                   	|
| AWS_SECRET_ACCESS_KEY 	| Credentials AWS               	|                   	|
| AWS_DEFAULT_REGION    	| Region AWS                    	| eu-west-1         	|
| PUSHGATEWAY_HOST      	| PushGateWay hostname          	| pushgateway:9091  	|
| PUSHGATEWAY_JOB       	| PushGateWay job name          	| POSTGRES_HOSTNAME 	|
| GZIP                      | Compression ratio                 | -9                    |

# Metrics
|                           Name                           	|                        Description                       	|
|-----------------------------------------------------------|-----------------------------------------------------------|
| backup_db{label="redmic"}                                	| Outcome of the backup database job (0=failed, 1=success) 	|
| backup_duration_seconds{label="redmic",stage="dump"}     	| Duration of create dump execution in seconds             	|
| backup_duration_seconds{label="redmic",stage="compress"} 	| Duration of compress dump execution in seconds           	|
| backup_duration_seconds{label="redmic",stage="upload"}   	| Duration of upload backup to S3 execution in seconds     	|
| backup_size_bytes{label="redmic"}                        	| Duration of the script execution in seconds              	|
| backup_created_date_seconds{label="redmic"}              	| Created date in seconds                                  	|
 No newline at end of file
+32.8 KiB
Loading image diff...
+28 −32
Original line number Diff line number Diff line
@@ -7,9 +7,8 @@ ZIP_FILENAME="${NOW_DATE}-backup.tar.gz"
DUMP_FILENAME="${DUMP_FILENAME:-db.dump}"

TIMEFORMAT="%R"
EXISTS_ERROR=0
NO_ERRORS=1

: ${PUSHGATEWAY_HOST:="pushgateway:9091"}
: ${PUSHGATEWAY_JOB:=${POSTGRES_HOSTNAME}}


@@ -17,19 +16,19 @@ function check_constraint_variable() {
	if [ -z "${BUCKET_BACKUP_DB}" ]
	then
		echo "ERROR! Variable BUCKET_BACKUP_DB is empty"
		EXISTS_ERROR=1
		NO_ERRORS=0
	fi

	if [ -z "${AWS_ACCESS_KEY_ID}" ]
	then
		echo "ERROR! Variable AWS_ACCESS_KEY_ID is empty"
		EXISTS_ERROR=1
		NO_ERRORS=0
	fi

	if [ -z "${AWS_SECRET_ACCESS_KEY}" ]
	then
		echo "ERROR! Variable AWS_SECRET_ACCESS_KEY is empty"
		EXISTS_ERROR=1
		NO_ERRORS=0
	fi
}

@@ -55,7 +54,7 @@ function dump_all() {
		DUMP_SIZE=$( size_file "${POSTGRES_DUMP_PATH}/${DUMP_FILENAME}" )
		if [ ${DUMP_SIZE} -eq 0 ]; then
			echo "ERROR created empty backup"
			EXISTS_ERROR=1
			NO_ERRORS=1
		else
			DUMP_DURATION_SECONDS=$(( SECONDS - start_seconds ))
			echo "Backup created"
@@ -64,7 +63,7 @@ function dump_all() {
		fi
	else
		echo "ERROR creating backup"
		EXISTS_ERROR=1
		NO_ERRORS=1
	fi
}

@@ -77,8 +76,7 @@ function compress() {
	echo "Compressing backup"
	local start_seconds=${SECONDS}

	sleep 5
	tar czf ${ZIP_FILENAME} ${DUMP_FILENAME}
	tar czfj ${ZIP_FILENAME} ${DUMP_FILENAME}

	COMPRESS_DURATION_SECONDS=$(( SECONDS - start_seconds ))
	COMPRESS_SIZE=$( size_file "${ZIP_FILENAME}" )
@@ -96,7 +94,6 @@ function upload_s3() {
	echo "Uploading backup to S3"
	local start_seconds=${SECONDS}

	sleep 3
	aws s3 cp ${POSTGRES_DUMP_PATH}/${ZIP_FILENAME} s3://${BUCKET_BACKUP_DB}

	UPLOAD_DURATION_SECONDS=$(( SECONDS - start_seconds ))
@@ -113,29 +110,28 @@ function clean_dump() {


function push_metrics() {

	CREATED_DATE_SECONDS=$(date +%s)

# No indent
cat <<EOF | curl --data-binary @- ${PUSHGATEWAY_HOST}/metrics/job/${PUSHGATEWAY_JOB}
# HELP backup_db outcome of the backup database job (1=failed, 0=success).
# HELP backup_db outcome of the backup database job (0=failed, 1=success).
# TYPE backup_db gauge
backup_db{label="${POSTGRES_HOSTNAME}"} ${EXISTS_ERROR}
# HELP dump_duration_seconds duration of the generate dump execution in seconds.
# TYPE dump_duration_seconds gauge
dump_duration_seconds{label="${POSTGRES_HOSTNAME}"} ${DUMP_DURATION_SECONDS:-0}
# HELP dump_size size of dump.
# TYPE dump_size gauge
dump_size{label="${POSTGRES_HOSTNAME}"} ${DUMP_SIZE:-0}
# HELP compress_duration_seconds duration of the compress dump execution in seconds.
# TYPE compress_duration_seconds gauge
compress_duration_seconds{label="${POSTGRES_HOSTNAME}"} ${COMPRESS_DURATION_SECONDS:-0}
# HELP compress_size size of backup.
# TYPE compress_size gauge
compress_size{label="${POSTGRES_HOSTNAME}"} ${COMPRESS_SIZE:-0}
# HELP upload_backup_to_s3_duration_seconds duration of upload backutp to S3 in seconds.
# TYPE upload_backup_to_s3_duration_seconds gauge
upload_backup_to_s3_duration_seconds{label="${POSTGRES_HOSTNAME}"} ${UPLOAD_DURATION_SECONDS:-0}
# HELP backup_duration_seconds duration of the script execution in seconds.
backup_db{label="${POSTGRES_HOSTNAME}"} ${NO_ERRORS}
# HELP backup_duration_seconds duration of each stage execution in seconds.
# TYPE backup_duration_seconds gauge
backup_duration_seconds{label="${POSTGRES_HOSTNAME}"} ${BACKUP_DURATION_SECONDS:-0}
backup_duration_seconds{label="${POSTGRES_HOSTNAME}",stage="dump"} ${DUMP_DURATION_SECONDS:-0}
backup_duration_seconds{label="${POSTGRES_HOSTNAME}",stage="compress"} ${COMPRESS_DURATION_SECONDS:-0}
backup_duration_seconds{label="${POSTGRES_HOSTNAME}",stage="upload"} ${UPLOAD_DURATION_SECONDS:-0}
# HELP backup_duration_seconds_total duration of the script execution in seconds.
# TYPE backup_duration_seconds_total gauge
backup_duration_seconds_total{label="${POSTGRES_HOSTNAME}"} ${BACKUP_DURATION_SECONDS:-0}
# HELP backup_size size of backup in bytes.
# TYPE backup_size gauge
backup_size_bytes{label="${POSTGRES_HOSTNAME}"} ${COMPRESS_SIZE:-0}
# HELP backup_created_date_seconds created date in seconds.
# TYPE backup_created_date_seconds gauge
backup_created_date_seconds{label="${POSTGRES_HOSTNAME}"} ${CREATED_DATE_SECONDS}
EOF

}
@@ -147,7 +143,7 @@ function main() {

	check_constraint_variable

	if [ ${EXISTS_ERROR} -eq 0 ]
	if [ ${NO_ERRORS} -eq 1 ]
	then
		mkdir -p ${POSTGRES_DUMP_PATH}

@@ -159,7 +155,7 @@ function main() {

		dump_all

		if [ ${EXISTS_ERROR} -eq 0 ]
		if [ ${NO_ERRORS} -eq 1 ]
		then
			compress
			upload_s3