Skip to content

Instantly share code, notes, and snippets.

@peterkracik
Last active June 26, 2020 08:00
Show Gist options
  • Select an option

  • Save peterkracik/8e8f59984f492967e1d47f328d64be7b to your computer and use it in GitHub Desktop.

Select an option

Save peterkracik/8e8f59984f492967e1d47f328d64be7b to your computer and use it in GitHub Desktop.
Scheduled Backup Gitlab CI/CD
variables:
HOST_PRODUCTION: "root@ip-address-of-host"
PROJECT_NAME: my_project
DB_EXPORT_PATH: /var/backups/mysql
DB_CONTAINER: project-db
backup db:
image: alpine
stage: build
only:
- schedules
- web
before_script:
## Install ssh-agent if not already installed, it is required by Docker.
- which ssh-agent || ( apk --update add openssh-client )
- eval "$(ssh-agent -s)"
- echo "$SSH_PRIVATE_KEY" | ssh-add - > /dev/null
- mkdir -p ~/.ssh && chmod 700 ~/.ssh
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
script:
## keeping only last 10 backups
- rm -f `ls $DB_EXPORT_PATH/* -t | awk 'NR>10'`
## date variable
- DATE=`date +%Y%m%d-%H%M%S`
## path of bcp file
- FILEPATH=$DB_EXPORT_PATH/$PROJECT_NAME-$DATE.tar.gz
## call mysql and export it to the provided path
- ssh $HOST_PRODUCTION 'docker exec "'$DB_CONTAINER'" mysqldump -u root --password="'$DB_ROOT_PASSWORD'" --all-databases | gzip -c > "'$FILEPATH'"'
deploy_staging:
stage: deploy
except:
- schedules
# job settings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment