如何通过cron在容器内转储DB?

时间:2017-01-05 02:34:26

标签: postgresql docker cron docker-compose

我使用docker-compose来升级堆栈。

相关代码:

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

Postgres的Dockerfile:

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

Cron根本不启动脚本。如何解决?

最终版本

基于@Farhad Farahi回答,以下是最终结果:

在主持人上我制作了一个剧本:

#!/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

此脚本将cron任务添加到主机,从而在容器中启动脚本backup.sh(参见上文)。

对于此实现,Dockerfile无需使用Postgres,因此docker-compose.yml的相关部分应如下所示:

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

2 个答案:

答案 0 :(得分:2)

你应该知道的事情:

  1. 默认情况下,cron库图片中未启动postgres服务。

  2. 更改cron配置时,需要重新加载cron服务。

  3. 建议:

    使用docker host的cron并使用docker exec启动定期任务。

    这种方法的优点:

    1. 所有容器的统一配置。

    2. 避免在多个容器中运行多个cron服务(更好地利用系统资源以及减少管理开销。

    3. 荣誉Microservices哲学。

答案 1 :(得分:1)

根据Farhad的回答,我在主机上创建了一个文件df <- structure(list(ID = 1:10, Data = c(20L, 30L, 25L, 26L, 88L, 65L, 70L, 30L, 15L, 22L)), .Names = c("ID", "Data"), row.names = c(NA, -10L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000000063e0788>) ,其中包含下一个内容:

postgres_backup.sh

我向Docker的Postgres图像添加了一个文件#!/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 ,内容为:

/db_backups/script/backup.sh