From b772cdf5be064a359bd9a5b5f5b964f8aaba9ed0 Mon Sep 17 00:00:00 2001 From: Ryan Noelk Date: Wed, 24 Jan 2018 12:25:32 -0500 Subject: [PATCH] Feature/image backup (#320) * Update sample_env_file_for_docker.list (#314) * Create backups.md * adding healthcheck to docker compose files and adding docs on how to take image backups * updating quickstart with image backup * updating gitignore --- .gitignore | 2 ++ docker-compose.yml | 2 +- docker-prod.yml | 9 +++++++-- docker-stage.yml | 9 +++++++-- ...d => Creating_a_proxy_server_for_docker.md} | 0 docs/Running_the_App.md | 4 +--- docs/Taking_Backups.md | 18 ++++++++++++++++++ docs/sample_env_file_for_docker.list | 2 +- quick-start.py | 17 ++++++++++++++++- 9 files changed, 53 insertions(+), 10 deletions(-) rename docs/{creating_a_proxy_server_for_docker.md => Creating_a_proxy_server_for_docker.md} (100%) create mode 100644 docs/Taking_Backups.md diff --git a/.gitignore b/.gitignore index 9ec207bc..d9adadca 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,8 @@ static-files/ database/ frontend/build/ frontend/jest/ +site-media/ +openeats.sql servies/static-files/ servies/side-media/ diff --git a/docker-compose.yml b/docker-compose.yml index d5b07c25..989201dc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: '2' +version: '2.1' services: api: build: api/ diff --git a/docker-prod.yml b/docker-prod.yml index 4dd504e6..bdd84b7d 100644 --- a/docker-prod.yml +++ b/docker-prod.yml @@ -1,4 +1,4 @@ -version: '2' +version: '2.1' services: api: image: openeats/api @@ -8,7 +8,8 @@ services: - static-files:/code/static-files - site-media:/code/site-media depends_on: - - db + db: + condition: service_healthy env_file: env_prod.list node: @@ -20,6 +21,10 @@ services: env_prod.list db: image: mariadb + healthcheck: + test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] + timeout: 20s + retries: 20 env_file: env_prod.list nginx: diff --git a/docker-stage.yml b/docker-stage.yml index 0e77e7d3..a53f564d 100644 --- a/docker-stage.yml +++ b/docker-stage.yml @@ -1,4 +1,4 @@ -version: '2' +version: '2.1' services: api: build: api/ @@ -8,7 +8,8 @@ services: - static-files:/code/static-files - site-media:/code/site-media depends_on: - - db + db: + condition: service_healthy env_file: env_stg.list node: @@ -22,6 +23,10 @@ services: env_stg.list db: image: mariadb + healthcheck: + test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] + timeout: 20s + retries: 20 env_file: env_stg.list nginx: diff --git a/docs/creating_a_proxy_server_for_docker.md b/docs/Creating_a_proxy_server_for_docker.md similarity index 100% rename from docs/creating_a_proxy_server_for_docker.md rename to docs/Creating_a_proxy_server_for_docker.md diff --git a/docs/Running_the_App.md b/docs/Running_the_App.md index 9fb04991..b951b669 100644 --- a/docs/Running_the_App.md +++ b/docs/Running_the_App.md @@ -2,8 +2,6 @@ The recommended way to run this app is with docker. You can install docker [here](https://www.docker.com/products/overview). If you are not familiar with docker you can read more about it on [their website](https://www.docker.com/what-docker). -If you are looking to run the app without docker, see the instructions [here](Running_without_Docker.md) - ### Running the app with docker for production If you are looking to run this in production, there is no need to clone the repo. @@ -12,7 +10,7 @@ First, create two files: - docker-prod.yml - This file can be found in the in the root directory of the repo. - env_prod.list - The settings file [sample_env_file_for_docker.list](sample_env_file_for_docker.list) can be used as an example. -The `docker-prod.yml` contains the list of images and commands to run the app. It come with an nginx reverse proxy that by default will run on port 80. You will most likely want to change the port that nginx runs on as well as use a fix tag for the image. By default all are set to latest. +The `docker-prod.yml` contains the list of images and commands to run the app. It comes with an nginx reverse proxy that by default will run on port 80. You will most likely want to change the port that nginx runs on as well as use a fix tag for the image. By default, all are set to latest. #### Configure the environment file Most of the settings in your `env_prod.list` can stay the same as `env_stg.list` that is in this repo. There are a few config settings that need to be changed for most configurations. See [Setting_up_env_file.md](Setting_up_env_file.md) for a complete description of the environment variables. diff --git a/docs/Taking_Backups.md b/docs/Taking_Backups.md new file mode 100644 index 00000000..36fffc96 --- /dev/null +++ b/docs/Taking_Backups.md @@ -0,0 +1,18 @@ +# Backing up Your Data + +The following commands can be used to take backups of your data. These commands are automatically run when you upgrade to a newer version as well. + + +#### Backing up recipes images: + +Replace `/dir/on/local/system/` with the location where you would like your images. +```sh +docker cp openeats_api_1:/code/site-media/ /dir/on/local/system/ +``` + +#### Backing up database: + +Places a sql dump of the database on your current working directory. +```sh +docker exec openeats_db_1 sh -c 'exec mysqldump openeats -uroot -p"$MYSQL_ROOT_PASSWORD"' > openeats.sql +``` diff --git a/docs/sample_env_file_for_docker.list b/docs/sample_env_file_for_docker.list index 4d52cad1..6e9967e6 100644 --- a/docs/sample_env_file_for_docker.list +++ b/docs/sample_env_file_for_docker.list @@ -8,7 +8,7 @@ API_PORT=8000 DJANGO_SECRET_KEY=sdfsadfas32e98zsdvhhsnz6udvbksjdhfi4galshjfg DJANGO_SETTINGS_MODULE=base.settings DJANGO_DEBUG=False -ALLOWED_HOST='my.domain.com' +ALLOWED_HOST=my.domain.com HTTP_X_FORWARDED_PROTO=true # Node config diff --git a/quick-start.py b/quick-start.py index af2009e4..ab502b23 100755 --- a/quick-start.py +++ b/quick-start.py @@ -2,6 +2,7 @@ # encoding: utf-8 from time import sleep +from os import getcwd from subprocess import call, Popen, PIPE @@ -28,7 +29,7 @@ def start_containers(): ) output, err = p.communicate(b"input data that is passed to subprocess' stdin") if output and not err: - print("Take a database backup (saving as openeats.sql)...") + print("Taking a database backup (saving as openeats.sql)...") call( 'docker exec openeats_db_1 sh -c ' + '\'exec mysqldump openeats -uroot -p"$MYSQL_ROOT_PASSWORD"\'' + @@ -40,6 +41,20 @@ def start_containers(): call(['docker-compose', '-f', 'docker-prod.yml', 'up', '-d', 'db']) sleep(45) + p = Popen( + ['docker', 'ps', '-q', '-f', 'name=openeats_api_1'], + stdin=PIPE, + stdout=PIPE, + stderr=PIPE + ) + output, err = p.communicate(b"input data that is passed to subprocess' stdin") + if output and not err: + print("Taking a image backup save to 'site-media'...") + call( + 'docker cp openeats_api_1:/code/site-media/ ' + getcwd(), + shell=True + ) + call(['docker-compose', '-f', 'docker-prod.yml', 'stop', 'nginx']) call(['docker-compose', '-f', 'docker-prod.yml', 'stop', 'api']) call(['docker-compose', '-f', 'docker-prod.yml', 'stop', 'node'])