Created
April 11, 2016 21:55
-
-
Save apsega/5c914286cb5caad853a64348ef0b6726 to your computer and use it in GitHub Desktop.
Docker containers backup
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
#!/bin/bash | |
# Variables | |
CONTAINER_NAME=$1 | |
IMAGE_NAME=$(docker ps -f name=$CONTAINER_NAME --format {{.Image}}) | |
TMP_DIR="/tmp" | |
DATE=$(date +"%Y-%m-%d") | |
DATA_VOLUMES=$(docker inspect --format '{{range .Mounts}}{{.Source}}{{" "}}{{end}}' $CONTAINER_NAME) | |
BKP_FILE="$TMP_DIR"/"$CONTAINER_NAME"_"$DATE" | |
# To which Dropbox to upload | |
if [[ "$2" == "EA" ]]; then | |
DROPBOX_UPLOADER=/home/Backups/gitlab.sh | |
APP_KEY=/home/Backups/.gitlab_dropbox_uploader | |
elif [[ "$2" == "IGC" ]]; then | |
DROPBOX_UPLOADER=/home/Backups/ignasc.sh | |
APP_KEY=/home/Backups/.ignasc_dropbox_uploader | |
else | |
DROPBOX_UPLOADER=/home/Backups/gitlab.sh | |
APP_KEY=/home/Backups/.gitlab_dropbox_uploader | |
fi | |
# Functions | |
function database_backup { | |
docker exec $CONTAINER_NAME sh -c 'exec mysqldump -uroot -p"$MYSQL_ROOT_PASSWORD" --all-databases' > "$BKP_FILE".sql | tee -a /var/log/backup.log | |
} | |
function data_volume_backup { | |
tar zcf "$BKP_FILE".tar.gz $DATA_VOLUMES | tee -a /var/log/backup.log | |
} | |
function gitlab_backup { | |
docker exec -i gitlab sh -c "gitlab-rake gitlab:backup:create" | tee -a /var/log/backup.log | |
$DROPBOX_UPLOADER -f $APP_KEY upload /var/lib/docker/volumes/2d491b8959dae66554b18406ae9f205a598f2cf9e9ec190671550a57131cf320/_data/backups/*.tar /"$CONTAINER_NAME"/ | tee -a /var/log/backup.log | |
rm -rfv /var/lib/docker/volumes/2d491b8959dae66554b18406ae9f205a598f2cf9e9ec190671550a57131cf320/_data/backups/* | tee -a /var/log/backup.log | |
exit | |
} | |
# Determine how to backup certain container (based on image) | |
case $IMAGE_NAME in | |
*mysql*) | |
database_backup | |
;; | |
*mariadb*) | |
database_backup | |
;; | |
*gitlab*) | |
gitlab_backup | |
;; | |
*) | |
data_volume_backup | |
;; | |
esac | |
# Upload archives to Dropbox | |
$DROPBOX_UPLOADER -f $APP_KEY upload "$BKP_FILE".* /"$CONTAINER_NAME"/ | tee -a /var/log/backup.log | |
# Cleanup | |
DELDATE=`date --date="-14 day" +%Y-%m-%d` | |
$DROPBOX_UPLOADER -f $APP_KEY delete /"$CONTAINER_NAME"/"$CONTAINER_NAME"_"$DELDATE".tar.gz | tee -a /var/log/backup.log | |
rm -rfv "$BKP_FILE".* | tee -a /var/log/backup.log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment