Last active
June 26, 2020 08:00
-
-
Save peterkracik/8e8f59984f492967e1d47f328d64be7b to your computer and use it in GitHub Desktop.
Scheduled Backup Gitlab CI/CD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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