Skip to content Skip to sidebar Skip to footer

Backup Odoo Automatically Using Crontab in Linux Debian

After explaining on basic how-to backup here, now i will write down how to do a backup odoo database automatically. What are we gonna achieve is backing up a database with a timestamp on it. As Odoo documentation, we can also do a backup of database with some file format either .sql, .dump or etc. We also gonna achieve it so we can have a multiple format file.

Odoo Cron Job - Source : learnodooerp 

How To Backup Odoo Database Automatically Using Crontab

  1. So first, what are we going to do is login to linux as root
  2. After logged in, switch user as postgres user by typing su - postgres. We did this because root user may not have accessibility to do something to the database so it's important to use postgres user to do a backup since it has privilege for doing so.
  3. Type in crontab -e. If you don't know what crontab is, please refer to this link
  4. And here, we gonna put a command and specific time to do a backup. In this case, we are willing to do a backup everyday at 19:30 (07:30PM) and also, every backup files should have a timestamp for it.

    To backup as .sql.gz format :

    30 19 * * * pg_dump yourDatabaseName | gzip > yourDatabaseName_$(date +\%Y\%m-\%d).sql.gz

    To backup as .dump format : 

    30 19 * * * pg_dump --format=c yourDatabaseName > yourDatabaseName_$(date +\%Y\%m-\%d).dump

    To backup as .tar.gz format : 

    30 19 * * * pg_dump --format=t yourDatabaseName | gzip > yourDatabaseName_$(date +\%Y\%m-\%d).dump

    To backup as .sql format : 

    30 19 * * * pg_dump -O -x yourDatabaseName > yourDatabaseName_$(date +\%Y\%m-\%d).dump

  5. Save your crontab by pressing ctrl + x and press y.
  6. Now, every night at 19:30 your server will do a backup. Files will be saved in /var/lib/postgresql
  7. You can visit here for a detail about crontab.

How To Backup Odoo Database (With Filestore) Automatically Using Crontab

Odoo saving information inside postgresql but not with the images or files. It's because all files like pdf and images are saved separately inside a filestore. Filestore is located inside the Odoo host in /odoo/.local/share/Odoo/filestore/yourDatabaseName. I wrote about filestore in my other blogpost here.

We are also able to backup a database from a web interface that odoo provided (localhost:8069). If you are backing up database using this method, filestore is also copied into your backup. When you are restore it, it will also be restored too, which means backing up from web is very practical and way more easy.

To do a backup automatically, you need to do few steps below :

  1. login to linux as root
  2. We also be using crontab -e again to be able to use a cronjob
  3. Then, type in the command below : 

    30 19 * * * wget --post-data 'master_pwd=yourOdooPassword&name=yourDatabaseName&backup_format=zip'-O /yourLinuxDirectoryForSavingBackup_$(date +\%Y\%m-\%d).zip http://localhost:8069/web/database/backup

  4. Save your crontab by pressing ctrl + x and press y.