Skip to content

Commit

Permalink
Improve password handling and Slack messages
Browse files Browse the repository at this point in the history
  • Loading branch information
whereisaaron committed Feb 9, 2017
1 parent 5aacf89 commit 824b41a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,34 @@ $CMD $(run_name) -- $EXTRA_OPTS \
--backup-name=assets
```

Or below is the same thing in Powershell.
```
#!/bin/powershell
$ErrorActionPreference = "Stop"
$WarningPreference = "SilentlyContinue"
#
# Backup MySQL database and website files
# - Use a synchronised timestamp so backups go into the same S3 directory
# - Use randomised deployment names in case any old/stuck deployments exist
#
$Timestamp = $(Get-Date -f yyyyMMdd-hhmm)
function Run-Name () {
'kb-task-' + -join (1..4 | %{ [char[]](0..127) -cmatch '[a-z0-9]' | Get-Random })
}
#$ExtraOpts = '--dry-run'
# The '--attach --rm' allows us to block until completion, you could remove that not wait for completion
$Command = 'kubectl run --attach --rm --quiet --restart=Never --image=whereisaaron/kube-backup:0.1.2 --namespace=kube-backup'
Invoke-Expression "$Command $(Run-Name) -- $ExtraOpts --task=backup-mysql-exec --timestamp=$Timestamp --namespace=default '--selector=app=myapp,env=dev,component=mysql'"
if ($LASTEXITCODE -ne 0) { Exit $LASTEXITCODE }
Invoke-Expression "$Command $(Run-Name) -- $ExtraOpts --task=backup-files-exec --timestamp=$Timestamp --namespace=default '--selector=app=myapp,env=dev,component=website' --files-path=/var/www/assets --backup-name=assets"
if ($LASTEXITCODE -ne 0) { Exit $LASTEXITCODE }
```

## Create backup task using a YAML file

You could also create backup jobs using a YAML or JSON file.
Expand Down
25 changes: 13 additions & 12 deletions bin/kube-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ SLACKEND
fi
}

send_slack_message_and_echo ()
{
send_slack_message "$@"
echo $1
}


#======================================================================
# Filenames
#
Expand Down Expand Up @@ -367,7 +374,7 @@ backup_mysql_exec ()
fi

local backup_filename=$(create_filename "${POD}" "${CONTAINER}" "${BACKUP_NAME:-$DATABASE}" "${TIMESTAMP}" ".gz")
local backup_cmd="mysqldump '${DATABASE}' --user=\"\${MYSQL_USER}\" --password=\"\${MYSQL_PASSWORD}\" --single-transaction | gzip"
local backup_cmd="MYSQL_PWD=\"\${MYSQL_PASSWORD}\" mysqldump '${DATABASE}' --user=\"\${MYSQL_USER}\" --single-transaction | gzip"

BACKUP_PATH="${NAMESPACE-default}/${TIMESTAMP}"
if [[ -n "${S3_BUCKET}" ]]; then
Expand All @@ -377,13 +384,9 @@ backup_mysql_exec ()
if [[ "${DRY_RUN}" != "true" ]]; then
$cmd bash -c "${backup_cmd}" | ${AWSCLI} s3 cp - "${target}"
if [[ "$?" -eq 0 ]];then
local msg="Backed up MySQL database '${DATABASE}' from container '${CONTAINER}' in pod '${POD}' to '${target}'"
send_slack_message "$msg"
echo "$msg"
send_slack_message_and_echo "Backed up MySQL database '${DATABASE}' from container '${CONTAINER}' in pod '${POD}' to '${target}'"
else
local msg="Error: Failed to back up MySQL database '${DATABASE}' from container '${CONTAINER}' in pod '${POD}' to '${target}'"
send_slack_message "$msg" danger
echo "$msg"
send_slack_message_and_echo "Error: Failed to back up MySQL database '${DATABASE}' from container '${CONTAINER}' in pod '${POD}' to '${target}'" danger
fi
else
echo "Skipping backup, dry run delected"
Expand Down Expand Up @@ -434,11 +437,9 @@ backup_files_exec ()
if [[ "${DRY_RUN}" != "true" ]]; then
$cmd bash -c "${backup_cmd}" | ${AWSCLI} s3 cp - "${target}"
if [[ "$?" -eq 0 ]];then
send_slack_message "Backed up files in '${FILES_PATH}' from container '${CONTAINER}' in pod '${POD}' to '${target}'"
send_slack_message_and_echo "Backed up files in '${FILES_PATH}' from container '${CONTAINER}' in pod '${POD}' to '${target}'"
else
local msg="Error: Failed to back up files in '${FILES_PATH}' from container '${CONTAINER}' in pod '${POD}' to '${target}'"
send_slack_message "$msg" danger
echo "$msg"
send_slack_message_and_echo "Error: Failed to back up files in '${FILES_PATH}' from container '${CONTAINER}' in pod '${POD}' to '${target}'" danger
fi
else
echo "Skipping backup, dry run delected"
Expand Down Expand Up @@ -642,7 +643,7 @@ case $TASK in
backup_files_exec
;;
test-slack)
send_slack_message "Hello world" warning
send_slack_message_and_echo "Hello world" warning
;;
dump-env)
env
Expand Down

0 comments on commit 824b41a

Please sign in to comment.