Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagebash
#!/usr/bin/env bash
readonly Server="https://teamcity.example.com" # teamcity url
readonly TeamcityBaseBackupPath="/docker-volumes/teamcity.example.com/data/backup" # Default backup path
readonly BaseBackupPath="/mnt/dbbackup/teamcity/teamcity.example.com" # Where to store backups.
readonly Token=eyJ0eXAiOiAiVENWMiJ9.UzZ2TGZSN2GMtZjI3MS00ZTQyLgzMmtBUkFaX2RoWnhZ.MWM5YzI1ZWIxNWMtYTc0TJvazNQVMGFmMzRhMjcz # teamcity user token
readonly SLACKPOST="/usr/local/bin/slackpost"
readonly SLACK_CHANNEL="alerts"
readonly SLACK_WEBHOOK="https://hooks.slack.com/services/TV37PNA3P/B01/nKYt6uytxZ22LFpxAW4ThP0K16PKU2YW"

readonly AddTimestamp=true # backup parameter
readonly IncludeConfigs=true # backup parameter
readonly IncludeDatabase=true # backup parameter
readonly IncludeBuildLogs=false # backup parameter
readonly IncludePersonalChanges=true # backup parameter
readonly FileName="TeamCity_Backup_" # backup parameter
readonly TeamCityURL="$Server/app/rest/server/backup?addTimestamp=$AddTimestamp&includeConfigs=$IncludeConfigs&includeDatabase=$IncludeDatabase&includeBuildLogs=$IncludeBuildLogs&includePersonalChanges=$IncludePersonalChanges&fileName=$FileName"
readonly Timeout=30 #minutes

SleepTime=$((60 * $Timeout / 5))
backupFileName=$(curl --location --request POST "$TeamCityURL" --header "Authorization: Bearer $Token")

if ! [[ $backupFileName =~ ^$FileName ]]
then
message="Backup aborted: invalid operation."
$SLACKPOST -c $SLACK_CHANNEL -h $SLACK_WEBHOOK -i teamcity -p disable -u Teamcity -m "$message"
exit 1
fi

st=0
until [ -f $TeamcityBaseBackupPath/$backupFileName ]
do
((st++))
echo "working $st"
if [ "${st}" -gt "${SleepTime}" ]; then
message="Backup aborted on timeout!"
fi
sleep 5
done
mv $TeamcityBaseBackupPath/$backupFileName $BaseBackupPath
backupSize=$(stat --printf="%s" $BaseBackupPath/$backupFileName | numfmt --to=iec)
message="Backup successful. File size: $backupSize"
$SLACKPOST -c $SLACK_CHANNEL -h $SLACK_WEBHOOK -i teamcity -p disable -u Teamcity -m "$message"

...