diff --git a/odoo/backups/Dockerfile b/odoo/backups/Dockerfile new file mode 100644 index 0000000..93dcc18 --- /dev/null +++ b/odoo/backups/Dockerfile @@ -0,0 +1,16 @@ +FROM odoocmd:8.0 +MAINTAINER Yaltik - Fabien Bourgeois + +ENV ODOO_ADMIN_PASSWORD admin +ENV ODOO_DATABASES demo +WORKDIR /home/odoo +USER root +RUN mkdir backups +RUN apt-get install -y --no-install-recommends cron gettext-base +COPY crontab /etc/crontab +RUN chmod 644 /etc/crontab +COPY backup.template . +COPY launch.sh . +RUN chown -R odoo:odoo . +VOLUME /home/odoo/backups +CMD bash launch.sh diff --git a/odoo/backups/backup.template b/odoo/backups/backup.template new file mode 100644 index 0000000..5e83ad6 --- /dev/null +++ b/odoo/backups/backup.template @@ -0,0 +1,3 @@ +#!/bin/bash + +python backup.py -oh golem -pw ${ODOO_ADMIN_PASSWORD} -D backups/ -n ${ODOO_DATABASES} diff --git a/odoo/backups/crontab b/odoo/backups/crontab new file mode 100644 index 0000000..c1a78f4 --- /dev/null +++ b/odoo/backups/crontab @@ -0,0 +1,3 @@ +# daily odoo backup +# m h dom mon dow user command +0 0 * * * odoo /bin/bash /opt/odoo/backup.sh diff --git a/odoo/backups/launch.sh b/odoo/backups/launch.sh new file mode 100644 index 0000000..558e464 --- /dev/null +++ b/odoo/backups/launch.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +envsubst < /home/odoo/backup.template > /home/odoo/backup.sh +chown odoo:odoo /home/odoo/backup.sh +cron -f diff --git a/odoo/base.yml b/odoo/base.yml index f09078e..2ca0802 100644 --- a/odoo/base.yml +++ b/odoo/base.yml @@ -23,3 +23,7 @@ services: odoocmd: build: ./scripts image: odoocmd:8.0 + + odoobackup: + build: ./backups + image: odoobackup:8.0 diff --git a/odoo/scripts/Dockerfile b/odoo/scripts/Dockerfile index 1c563b4..71ae8cc 100644 --- a/odoo/scripts/Dockerfile +++ b/odoo/scripts/Dockerfile @@ -9,3 +9,4 @@ RUN useradd odoo -m -s /bin/bash USER odoo COPY *.py /home/odoo/ WORKDIR /home/odoo +CMD /bin/true diff --git a/odoo/scripts/backup.py b/odoo/scripts/backup.py index c73149b..aba50d7 100644 --- a/odoo/scripts/backup.py +++ b/odoo/scripts/backup.py @@ -11,20 +11,21 @@ def datefmt(): return today.strftime('%Y-%m-%d_%Hh%Mm%Ss') -def dump(odoo, bakpath, password, dbname): - """ Dumps a database """ - dump = odoo.db.dump(password, dbname) - fpath = '{bakpath}/{date}_{dbname}.zip'.format(bakpath=bakpath, - dbname=dbname, - date=datefmt()) - with open(fpath, 'wb') as zipf: - zipf.write(dump.read()) - return fpath +def dump(odoo, bakpath, password, dbnames): + """ Dumps a database list """ + for dbname in dbnames: + dump = odoo.db.dump(password, dbname) + fpath = '{bakpath}/{date}_{dbname}.zip'.format(bakpath=bakpath, + dbname=dbname, + date=datefmt()) + with open(fpath, 'wb') as zipf: + zipf.write(dump.read()) + return True def dump_all(odoo, bakpath, password): """ Dump all databases """ - return [dump(bakpath, password, db) for db in odoo.db.list()] + return dump(odoo, bakpath, password, odoo.db.list()) def main(): @@ -35,18 +36,18 @@ def main(): help='the odoo master password') parser.add_argument('-D', '--directory', type=str, required=True, help='the directory path where to save backups') - parser.add_argument('-n', '--name', type=str, - help='optional database name (default:all)') + parser.add_argument('-n', '--names', type=str, + help='optional database names (default:all)') args = parser.parse_args() # Connection odoo = common.connection(args) # Backup actions - if not args.name: + if not args.names: dump_all(odoo, args.directory, args.password) else: - dump(odoo, args.directory, args.password, args.name) + dump(odoo, args.directory, args.password, args.names.split(',')) if __name__ == "__main__": main()