From c955d5decaeae20cecac0a27b066d7e2add5506b Mon Sep 17 00:00:00 2001 From: yohan <783b8c87@scimetis.net> Date: Tue, 25 Jun 2019 18:08:17 +0200 Subject: [PATCH] Initial commit. --- README | 17 +++++++++++++++++ crontab-example | 13 +++++++++++++ docker-compose.yml | 35 +++++++++++++++++++++++++++++++++++ nettoyer_quotas.sh | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 README create mode 100644 crontab-example create mode 100644 docker-compose.yml create mode 100755 nettoyer_quotas.sh diff --git a/README b/README new file mode 100644 index 0000000..b33678c --- /dev/null +++ b/README @@ -0,0 +1,17 @@ +Why ? +pour centraliser la sauvegarde et l'administration des BDD + +Si première utilisation : + +docker network create mysqlnet +docker volume create --name mysql-server_data +docker volume create --name mysql-server_dumps +rsync -itrlpgovDHXP var_lib_mysql/* /var/lib/docker-latest/volumes/mysql-server_data/_data/ +chcon -Rt svirt_sandbox_file_t /var/lib/docker-latest/volumes/mysql-server_data/_data/ +chcon -Rt svirt_sandbox_file_t /var/lib/docker-latest/volumes/mysql-server_dumps/_data/ + +Utilisation : + +Les droits des fichiers doivent être corrigés avant de lancer la stack avec docker-compose up -d : +chcon -u system_u -r object_r -t svirt_sandbox_file_t crontab +chcon -u system_u -r object_r -t svirt_sandbox_file_t nettoyer_quotas.sh diff --git a/crontab-example b/crontab-example new file mode 100644 index 0000000..71130a0 --- /dev/null +++ b/crontab-example @@ -0,0 +1,13 @@ +# /etc/crontab: system-wide crontab +# Unlike any other crontab you don't have to run the `crontab' +# command to install the new version when you edit this file +# and files in /etc/cron.d. These files also have username fields, +# that none of the other crontabs do. + +SHELL=/bin/sh +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin + +# m | h | day of month | month | day of week | user | command +3 0 * * * root docker -H tcp://127.0.0.1:2375 exec mysql-server /root/nettoyer_quotas.sh > /proc/1/fd/2 2>&1 +3 1 * * * root docker -H tcp://127.0.0.1:2375 exec mysql-server sh -c "mysqldump --databases mysql -uroot -pXXXXXXXX > /mnt/dumps/mysql_dump-mysql_`date +\%d-\%m-\%Y`" > /proc/1/fd/2 2>&1 +13 1 * * * root docker -H tcp://127.0.0.1:2375 exec mysql-server sh -c "mysqldump --databases gogs owncloud semanticscuttle -uroot -pXXXXXXXX > /mnt/dumps/mysql_dump_`date +\%d-\%m-\%Y`" > /proc/1/fd/2 2>&1 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..007a6e1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,35 @@ +version: "2.1" + +services: + + mysql-server: + image: mysql-server + container_name: mysql-server + restart: always + networks: + - mysqlnet + volumes: + - mysql-server_data:/var/lib/mysql:Z + - mysql-server_dumps:/mnt/dumps:Z + - ./nettoyer_quotas.sh:/root/nettoyer_quotas.sh:Z + + cron: + image: cron + restart: always + network_mode: "host" + volumes: + - ./crontab:/etc/crontab:Z + +networks: + + mysqlnet: + external: true + +volumes: + + mysql-server_data: + external: true + + mysql-server_dumps: + external: true + diff --git a/nettoyer_quotas.sh b/nettoyer_quotas.sh new file mode 100755 index 0000000..3eba79e --- /dev/null +++ b/nettoyer_quotas.sh @@ -0,0 +1,34 @@ +#!/bin/bash +#find /mnt/dumps -mtime +30 -print +# ce scrip efface les vieux dumps de la base de données. +PURGEPATH=/mnt/dumps +cd $PURGEPATH +OLD_IFS="$IFS" + +IFS=$(echo -en "\n\b") +for file in `ls -rt` +do + IFS="$OLD_IFS" + # en Ko + espace_libre=`df -P "$PURGEPATH" | grep "$PURGEPATH" | head -n 1 | awk 'BEGIN{FS=" "} {print $4}'` + # We want to keep 5 Go free + if [ "$espace_libre" -lt 5000000 ] + then + rm -rf -- "$file" && echo "Removed $file" + else + echo "Enough free space retrieved" + break + fi + # etc + # il faut faire attention, la valeur de l'IFS n'étant pas celle par défaut, certaines choses + # ne fonctionneront pas si tu fais des choses compliquées dans ta boucle, tu seras probablement + # obligé de restaurer/effacer la valeur d'IFS à chaque itération. + sleep 1 + IFS=$(echo -en "\n\b") +done +echo "$espace_libre Ko free on $PURGEPATH" + +IFS="$OLD_IFS" + +#suite du script +