[ADD]Odoo image backup service

This commit is contained in:
Fabien Bourgeois 2016-10-05 08:56:58 +02:00
parent 9c82fca298
commit f8bc904153
7 changed files with 47 additions and 14 deletions

16
odoo/backups/Dockerfile Normal file
View File

@ -0,0 +1,16 @@
FROM odoocmd:8.0
MAINTAINER Yaltik - Fabien Bourgeois <fabien@yaltik.com>
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

View File

@ -0,0 +1,3 @@
#!/bin/bash
python backup.py -oh golem -pw ${ODOO_ADMIN_PASSWORD} -D backups/ -n ${ODOO_DATABASES}

3
odoo/backups/crontab Normal file
View File

@ -0,0 +1,3 @@
# daily odoo backup
# m h dom mon dow user command
0 0 * * * odoo /bin/bash /opt/odoo/backup.sh

5
odoo/backups/launch.sh Normal file
View File

@ -0,0 +1,5 @@
#!/bin/bash
envsubst < /home/odoo/backup.template > /home/odoo/backup.sh
chown odoo:odoo /home/odoo/backup.sh
cron -f

View File

@ -23,3 +23,7 @@ services:
odoocmd:
build: ./scripts
image: odoocmd:8.0
odoobackup:
build: ./backups
image: odoobackup:8.0

View File

@ -9,3 +9,4 @@ RUN useradd odoo -m -s /bin/bash
USER odoo
COPY *.py /home/odoo/
WORKDIR /home/odoo
CMD /bin/true

View File

@ -11,20 +11,21 @@ def datefmt():
return today.strftime('%Y-%m-%d_%Hh%Mm%Ss')
def dump(odoo, bakpath, password, dbname):
""" Dumps a database """
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 fpath
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()