From 423e6e63b1871c94a151541b2ab12b22f6d6c4ff Mon Sep 17 00:00:00 2001 From: tinpee Date: Wed, 19 Oct 2016 06:54:54 +0700 Subject: [PATCH] docker backup and restore db --- Dockerfile | 5 +++-- README.md | 37 +++++++++++++++++++++++++++++++------ entrypoint.sh | 5 +++++ flash_cards.py | 2 +- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 26c73d1d..b5b08f38 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,12 +3,13 @@ MAINTAINER Tinpee ADD . /src WORKDIR /src -RUN pip install flask gunicorn \ - && cp cards-jwasham.db cards.db +RUN pip install flask gunicorn COPY entrypoint.sh / RUN chmod +x /entrypoint.sh +VOLUME /src/db + EXPOSE 8000 CMD ["/entrypoint.sh"] diff --git a/README.md b/README.md index 1def8204..41cc4471 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,10 @@ It uses: --- -## How to run it +## How to run it on a server 1. Clone project to a directory on your web server. -1. Edit the config.txt file. The username and password will be the login +1. Edit the config.txt file. Change the secret key, username and password. The username and password will be the login for your site. There is only one user - you. 1. Follow this long tutorial to get Flask running. It was way more work than it should be: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04 @@ -53,10 +53,35 @@ It uses: in the top menu. ## How to run with Docker + +*Provided by [@Tinpee](https://github.com/tinpee) - tinpee.dev@gmail.com* + __Make sure you already installed [docker](https://www.docker.com)__ -- Clone project to any where you want and go to source folder -- Build image: `docker build . -t cs-flash-cards` -- Run container: `docker run -d -p 8000:8000 --name cs-flash-cards cs-flash-cards` -- Go your browser and type `http://localhost:8000` + +1. Clone project to any where you want and go to source folder. +1. Edit the config.txt file. Change the secret key, username and password. The username and password will be the login + for your site. There is only one user - you. +1. Build image: `docker build . -t cs-flash-cards` +1. Run container: `docker run -d -p 8000:8000 --name cs-flash-cards cs-flash-cards` +1. Go your browser and type `http://localhost:8000` + +__If you already had a backup file `cards.db`. Run following command:__ +*Note: We don't need to rebuild image, just delete old container if you already built.* +`docker run -d -p 8000:8000 --name cs-flash-cards -v ::/src/db cs-flash-cards` +``: is the full path contains `cards.db` +Example: `/home/tinpee/cs-flash-cards/db`, and `cards.db` is inside this folder. + +For convenient, if you don't have `cards.db`, this container will auto copy a new one from `cards-jwasham.db`. So you don't need to `initdb` + +__How to backup data ?__ +We just need store `cards.db` file, and don't need any sql command. +- If you run container with `-v :/src/db` just go to `folder_db` and store `cards.db` anywhere you want. +- Without `-v flag`. Type: `docker cp :/src/db/cards.db /path/to/save` + +__How to restore data ?__ +- Delete old container (not image): `docker rm cs-flash-cards` +- Build a new one with `-v flag`: +`docker run -d -p 8000:8000 --name cs-flash-cards -v ::/src/db cs-flash-cards` +- Voila :) *Happy learning!* diff --git a/entrypoint.sh b/entrypoint.sh index 05fa542a..6376c182 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,4 +1,9 @@ #!/bin/bash +if [ ! -f /src/db/cards.db ]; then + mkdir -p /src/db + cp cards-jwasham.db /src/db/cards.db +fi + export CARDS_SETTINGS=/src/config.txt gunicorn --bind 0.0.0.0:8000 flash_cards:app \ No newline at end of file diff --git a/flash_cards.py b/flash_cards.py index 166c6796..12bf29c0 100644 --- a/flash_cards.py +++ b/flash_cards.py @@ -8,7 +8,7 @@ # Load default config and override config from an environment variable app.config.update(dict( - DATABASE=os.path.join(app.root_path, 'cards.db'), + DATABASE=os.path.join(app.root_path, 'db', 'cards.db'), SECRET_KEY='development key', USERNAME='admin', PASSWORD='default'