Skip to content
Snippets Groups Projects
Commit ad201d26 authored by Quentin Duchemin's avatar Quentin Duchemin
Browse files

Merge branch 'dev-ci' into 'master'

Improve db-backup image

See merge request !46
parents cf5930af f3e39fc2
No related branches found
No related tags found
1 merge request!46Improve db-backup image
Pipeline #52626 failed
File moved
# Configuration
# Backup des bases de données des services
Le fichier backup_data.json recense les informations sur les différents services mis en place sur la machine et les informations nécessaires pour lancer les rotations sur ces services.
Cet outil orchestre le backup des différents services de Picasoft.
## Structure
Il est capable de gérer les bases de données `postgres`, `mysql` ou `mongo` pour le moment.
Il est flexible et se configure via des fichiers JSON.
<!-- TOC depthFrom:2 depthTo:6 withLinks:1 updateOnSave:1 orderedList:0 -->
- [Lancement](#lancement)
- [Configuration](#configuration)
- [Structure](#structure)
- [Gestion des secrets](#gestion-des-secrets)
- [Exemple](#exemple)
<!-- /TOC -->
## Lancement
On se synchronise simplement avec le dépôt et on lance le Docker Compose.
On copie `secrets/db.secrets.example` dans `secrets/db.secrets` :
* On assigne le nom de la VM à `HOSTNAME` (*e.g.* `pica02`),
* On renseigne les secrets (voir [plus bas]((#gestion-des-secrets)))
L'outil sélectionne automatiquement le bon fichier de configuration en fonction du nom d'hôte de la machine virtuelle, il n'y a donc rien à modifier :
```
$ docker-compose up -d
```
## Configuration
Les fichiers dans [`.config`](./config) recensent les informations sur les différents services mis en place sur les machines et les informations nécessaires pour lancer les rotations sur ces services.
Ces fichiers **sont** les fichiers utilisés **en production**.
Ils ne doivent pas être modifiés sur les machines sans être synchronisés avec le dépôt Git.
On veillera à ce que les noms d'hôte correspondent bien aux noms des conteneurs (gérés par d'autres Docker Compose), et que le dossier `/DATA/BACKUP` existe.
### Structure
- Nom du service : Contient une structure de données contenant les informations relatives au service telles qu'indiquées ci-dessous
- "Host" : Indique l'hôte de la base de données
- "Port" : Indique le port pour se connecter au service
- "Database" : Indique le nom de la base de données
- "User" : Nom d'utilisateur ou nom d'une variable d'environnement (optionnel)
- "Password" : Mot de passe ou nom d'une variable d'environnement (optionnel)
- "Folder" : Indique le nom du dossier de backup utilisé par le script de backup et de rotation
- "Cron" : Indique la fréquence de temps à laquelle les backups sont effectués par le script de rotation au format cron
- "Init-Backup" : Indique si un backup doit être effectué au démarrage du service, en plus de la programmation du cron
- "Backup-Rota" : Contient les paramètres pour la rotation des backups dans une structure de données comme indiqué ci-dessous
- "Hour" : nombre de backups horaires à conserver
- "Day" : nombre de backups quotidiens à conserver
- "Week" : nombre de backups hebdomadaires à conserver
- "Month": nombre de backups mensuels à conserver
## Exemple
### Gestion des secrets
Afin de pouvoir versionner les fichiers de configuration sans exposer les identifiants aux bases de données, on utilise le système suivant :
* Dans le JSON, on utilise un nom de variable d'environnement à la place de l'identifant, **sans le $**, *e.g.* `ETHERPAD_DB_USER`.
* Dans le fichier `db.secrets`, on renseigne la valeur de cette variable d'environnement.
La substitution est effectué automatiquement par l'outil.
### Exemple
```json
{
......@@ -41,8 +90,8 @@ Le fichier backup_data.json recense les informations sur les différents service
{
"Host": "etherpad-db",
"Port": "3306",
"User": "root",
"Password": "lolilolilol",
"User": "ETHERPAD_DB_USER",
"Password": "ETHERPAD_DB_PASSWORD",
"Database": "--all-databases",
"Type": "mysql",
"Folder": "etherpad",
......@@ -59,22 +108,3 @@ Le fichier backup_data.json recense les informations sur les différents service
}
}
```
## Exemple d'implémentation dans le docker-compose
```yaml
##############################
####### Backup rotation ######
##############################
pica-backup-rotation:
image: backup-rotation
container_name: pica-backup-rotation
volumes:
- /DATA/BACKUP/:/backup
- /DATA/CONFIG:/config
```
Avec:
- `/DATA/BACKUP` : Dossier contenant les backups à effecter
- `/DATA/CONFIG` : Dossier contenant le fichier .json de données
......@@ -22,8 +22,8 @@ def add_value_to_key(value, key):
os.environ[key] = value
CONFIG_FOLDER = '/config'
CONFIG_FILE = 'backup_data.json'
PATH_TO_CONFIG = CONFIG_FOLDER + '/' + CONFIG_FILE
CONFIG_FILE = os.environ['HOSTNAME']
PATH_TO_CONFIG = CONFIG_FOLDER + '/' + CONFIG_FILE + '.json'
# Check if config file exists
if not(os.path.exists(CONFIG_FOLDER)):
......@@ -44,7 +44,7 @@ flag_postgres = False
for service in services_list:
if services_list[service]["Type"] == "mongo":
# Mongo DB handling
# Mongo DB handling
add_value_to_key(service,"MONGO_SERVICE_NAME_LIST")
add_value_to_key(services_list[service]["Host"],"MONGO_HOST_LIST")
add_value_to_key(services_list[service]["Port"],"MONGO_PORT_LIST")
......@@ -62,8 +62,14 @@ for service in services_list:
add_value_to_key(service,"MYSQL_SERVICE_NAME_LIST")
add_value_to_key(services_list[service]["Host"],"MYSQL_HOST_LIST")
add_value_to_key(services_list[service]["Port"],"MYSQL_PORT_LIST")
add_value_to_key(services_list[service]["User"],"MYSQL_USER_LIST")
add_value_to_key(services_list[service]["Password"],"MYSQL_PASS_LIST")
user = services_list[service]["User"]
if user in os.environ:
user = os.environ[user]
add_value_to_key(user,"MYSQL_USER_LIST")
password = services_list[service]["Password"]
if password in os.environ:
password = os.environ[password]
add_value_to_key(password,"MYSQL_PASS_LIST")
add_value_to_key(services_list[service]["Database"],"MYSQL_DB_LIST")
add_value_to_key(services_list[service]["Init-Backup"],"MYSQL_INIT_BACKUP_LIST")
add_value_to_key(services_list[service]["Cron"],"MYSQL_CRON_TIME_LIST")
......@@ -79,8 +85,14 @@ for service in services_list:
add_value_to_key(service,"POSTGRES_SERVICE_NAME_LIST")
add_value_to_key(services_list[service]["Host"],"POSTGRES_HOST_LIST")
add_value_to_key(services_list[service]["Port"],"POSTGRES_PORT_LIST")
add_value_to_key(services_list[service]["User"],"POSTGRES_USER_LIST")
add_value_to_key(services_list[service]["Password"],"POSTGRES_PASS_LIST")
user = services_list[service]["User"]
if user in os.environ:
user = os.environ[user]
add_value_to_key(user,"POSTGRES_USER_LIST")
password = services_list[service]["Password"]
if password in os.environ:
password = os.environ[password]
add_value_to_key(password,"POSTGRES_PASS_LIST")
add_value_to_key(services_list[service]["Database"],"POSTGRES_DB_LIST")
add_value_to_key(services_list[service]["Init-Backup"],"POSTGRES_INIT_BACKUP_LIST")
add_value_to_key(services_list[service]["Cron"],"POSTGRES_CRON_TIME_LIST")
......
{
"wekan":
{
"Host": "wekan-db",
"Port": "27017",
"Database": "wekan",
"Type": "mongo",
"Folder": "wekan",
"Cron" : "0 */12 * * *",
"Init-Backup" : "1",
"Backup-Rota":
{
"Hour" : 24,
"Day" : 7,
"Week" : 4,
"Month" : 12
}
}
}
version: "3.7"
networks:
docker_default:
external: true
name: "docker_default"
services:
db-backup:
image: registry.picasoft.net/pica-db-backup:1.1
container_name: db-backup
volumes:
- /DATA/BACKUP/:/backup/
- ./config:/config
- /etc/localtime:/etc/localtime:ro
env_file:
- ./secrets/db.secrets
networks:
- docker_default
restart: always
File moved
File moved
File moved
......@@ -2,18 +2,27 @@
# DB_TYPE : MYSQL|POSTGRES
# MYSQL POSTGRESQL MONGODB
# MYSQL_SERVICE_NAME_LIST POSTGRES_SERVICE_NAME_LIST MONGO_SERVICE_NAME_LIST
# MYSQL_HOST_LIST POSTGRES_HOST_LIST MONGO_HOST_LIST
# MYSQL_PORT_LIST POSTGRES_PORT_LIST MONGO_PORT_LIST
# MYSQL_USER_LIST POSTGRES_USER_LIST MONGO_USER_LIST
# MYSQL_PASS_LIST POSTGRES_PASS_LIST MONGO_PASS_LIST
# MYSQL_DB_LIST POSTGRES_DB_LIST MONGO_DB_LIST
# MYSQL_INIT_BACKUP_LIST POSTGRES_INIT_BACKUP_LIST MONGO_INIT_BACKUP_LIST
# MYSQL_CRON_TIME_LIST POSTGRES_CRON_TIME_LIST MONGO_CRON_TIME_LIST
# MYSQL_EXTRA_OPTS_LIST POSTGRES_EXTRA_OPTS-LIST MONGO_BACKUP_FOLDER_LIST
###############################
# Needed env for each DB type #
###############################
# MYSQL POSTGRESQL MONGODB
# MYSQL_SERVICE_NAME_LIST POSTGRES_SERVICE_NAME_LIST MONGO_SERVICE_NAME_LIST
# MYSQL_HOST_LIST POSTGRES_HOST_LIST MONGO_HOST_LIST
# MYSQL_PORT_LIST POSTGRES_PORT_LIST MONGO_PORT_LIST
# MYSQL_USER_LIST POSTGRES_USER_LIST MONGO_USER_LIST
# MYSQL_PASS_LIST POSTGRES_PASS_LIST MONGO_PASS_LIST
# MYSQL_DB_LIST POSTGRES_DB_LIST MONGO_DB_LIST
# MYSQL_INIT_BACKUP_LIST POSTGRES_INIT_BACKUP_LIST MONGO_INIT_BACKUP_LIST
# MYSQL_CRON_TIME_LIST POSTGRES_CRON_TIME_LIST MONGO_CRON_TIME_LIST
# MYSQL_EXTRA_OPTS_LIST POSTGRES_EXTRA_OPTS-LIST MONGO_BACKUP_FOLDER_LIST
# MYSQL_BACKUP_FOLDER_LIST POSTGRESQL_BACKUP_FOLDER_LIST
#################
# Reset crontab #
#################
echo "" > /crontab.conf
#####################
# Create the arrays #
#####################
......
HOSTNAME=pica02
ETHERPAD_DB_USER=test
ETHERPAD_DB_PASSWORD=test
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment