2017-01-05 6 views
1

Ich benutze docker-compose, die einen Stapel ups.Wie DB über Cron innerhalb Container entladen?

Relative Code:

db: 
    build: ./dockerfiles/postgres 
    container_name: postgres-container 
    volumes: 
    - ./dockerfiles/postgres/pgdata:/var/lib/postgresql/data 
    - ./dockerfiles/postgres/backups:/pg_backups 

Dockerfile für Postgres:

FROM postgres:latest 

RUN mkdir /pg_backups && > /etc/cron.d/pg_backup-cron && echo "00 22 * * * /backup.sh" >> /etc/cron.d/pg_backup-cron 
ADD ./backup.sh/
RUN chmod +x /backup.sh 

backup.sh

#!/bin/sh 
# Dump DBs 

now=$(date +"%d-%m-%Y_%H-%M") 
pg_dump -h db -U postgres -d postgres > "/pg_backups/db_dump_$now.sql" 

# remove all files (type f) modified longer than 30 days ago under /pg_backups 
find /pg_backups -name "*.sql" -type f -mtime +30 -delete 

exit 0 

Cr on startet das Skript einfach nicht. Wie behebt man das?


FINAL VERSION

Basierend auf @Farhad Farahi Antwort, unter das Endergebnis:

Auf Host habe ich ein Skript:

#!/bin/bash 
# Creates Cron Job which backups DB in Docker everyday at 22:00 host time 
croncmd_backup="docker exec -it postgres-container bash -c '/pg_backups/backup.sh'" 
cronjob_backup="00 22 * * * $croncmd_backup" 

if [[ $# -eq 0 ]] ; then 
    echo -e 'Please provide one of the arguments (example: ./run_after_install.sh add-cron-db-backup): 
    1) add-cron-db-backup 
    2) remove-cron-db-backup' 

# In order to avoid task duplications in cron, the script checks, if there is already back-up job in cron 
elif [[ $1 == add-cron-db-backup ]]; then 
    (crontab -l | grep -v -F "$croncmd_backup" ; echo "$cronjob_backup") | crontab - 
    echo "==>>> Backup task added to Cron" 

# Remove back-up job from cron 
elif [[ $1 == remove-cron-db-backup ]]; then 
    (crontab -l | grep -v -F "$croncmd_backup") | crontab - 
    echo "==>>> Backup task removed from Cron" 

fi 

Dieses Skript fügt cron Aufgabe zu hosten, die das Skript backup.sh (siehe oben) in einem Container startet.

Für diese Implementierung gibt es keine Notwendigkeit Dockerfile für Postgres zu verwenden, so relevante Teil docker-compose.yml sollte wie folgt aussehen:

version: '2' 
services: 
    db: 
    image: postgres:latest 
    container_name: postgres-container 
    volumes: 
     - ./dockerfiles/postgres/pgdata:/var/lib/postgresql/data 
     - ./dockerfiles/postgres/backups:/pg_backups 

Antwort

2

Dinge, die Sie wissen sollten:

  1. cron Dienst nicht gestartet wird Standardmäßig in postgres Bibliotheksbild.

  2. Wenn Sie die Cron-Konfiguration ändern, müssen Sie den Dienst cron erneut laden.

Empfehlung:

Verwenden docker host ‚s cron und verwenden docker exec die periodischen Aufgaben zu starten.

Vorteile dieses Ansatzes:

  1. Einheitliche Konfiguration für alle Behälter.

  2. Vermeidet mehrere cron Dienste in mehreren Behältern (Bessere Nutzung von Systemressourcen aswell als weniger Verwaltungsaufwand ausgeführt werden.

  3. Honors Microservices Philosophie.
1

Basierend auf der Antwort des Farhad habe ich eine Datei postgres_backup.sh auf dem Host mit dem nächsten Inhalt:

#!/bin/bash 
# Creates Cron Job which backups DB in Docker everyday at 22:00 host time 
croncmd_backup="docker exec -it postgres-container bash -c '/db_backups/script/backup.sh'" 
cronjob_backup="00 22 * * * $croncmd_backup" 

if [[ $# -eq 0 ]] ; then 
    echo -e 'Please provide one of the arguments (example: ./postgres_backup.sh add-cron-db-backup): 
    1 > add-cron-db-backup 
    2 > remove-cron-db-backup 

elif [[ $1 == add-cron-db-backup ]]; then 
    (crontab -l | grep -v -F "$croncmd_backup" ; echo "$cronjob_backup") | crontab - 
    echo "==>>> Backup task added to Local (not container) Cron" 

elif [[ $1 == remove-cron-db-backup ]]; then 
    (crontab -l | grep -v -F "$croncmd_backup") | crontab - 
    echo "==>>> Backup task removed from Cron" 

fi 

und fügte ich eine Datei /db_backups/script/backup.sh zu Docker des Postgres Bild mit dem Inhalt:

#!/bin/sh 
# Dump DBs 

now=$(date +"%d-%m-%Y_%H-%M") 
pg_dump -h db -U postgres -d postgres > "/db_backups/backups/db_dump_$now.sql" 

# remove all files (type f) modified longer than 30 days ago under /db_backups/backups 
find /db_backups/backups -name "*.sql" -type f -mtime +30 -delete 

exit 0