Commit 1d615add authored by Ignacio's avatar Ignacio
Browse files

Cambia nombre variable para errores, añade date

parent 17f93f36
Loading
Loading
Loading
Loading
+27 −30
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ 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 +17,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 +55,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 +64,7 @@ function dump_all() {
		fi
	else
		echo "ERROR creating backup"
		EXISTS_ERROR=1
		NO_ERRORS=1
	fi
}

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

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

	COMPRESS_DURATION_SECONDS=$(( SECONDS - start_seconds ))
@@ -96,7 +95,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 +111,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 dump.
# TYPE backup_size gauge
backup_size{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 +144,7 @@ function main() {

	check_constraint_variable

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

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

		dump_all

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