Skip to content

Instantly share code, notes, and snippets.

@kryskool
Forked from matrixise/backup_db.sh
Created September 26, 2012 07:29
Show Gist options
  • Save kryskool/3786591 to your computer and use it in GitHub Desktop.
Save kryskool/3786591 to your computer and use it in GitHub Desktop.
Backup all the OpenERP databases and the associated filestores
#!/usr/bin/env bash
ROOT_BACKUP_PATH=/tmp/backup
echo "backup path: ${ROOT_BACKUP_PATH}"
# Create the backup directory
[ -d "$ROOT_BACKUP_PATH" ] || mkdir -p $ROOT_BACKUP_PATH
DATABASES=`psql -ltA -d postgres -c "SELECT datname FROM pg_database WHERE datname NOT IN ('postgres', 'template0', 'template1')"`
for database in $DATABASES; do
DB_CONNECT="psql -ltA -d ${database}"
is_openerp_database=`$DB_CONNECT -c "SELECT COUNT(1) FROM pg_tables WHERE tablename = 'ir_module_module';"`
if [ $is_openerp_database == 1 ]; then
echo "openerp database: $database"
BACKUP_PATH=${ROOT_BACKUP_PATH}/$database
echo " path: ${BACKUP_PATH}"
[ -d "$BACKUP_PATH" ] || mkdir -p $BACKUP_PATH/{dump,filestore}
pg_dump --no-owner -Fc $database > $BACKUP_PATH/dump/database.dump
echo " dump: ${BACKUP_PATH}/dump/database.dump"
document_is_installed=`${DB_CONNECT} -c "SELECT COUNT(1) FROM ir_module_module WHERE name='document' AND state IN ('installed', 'to upgrade')"`
if [ $document_is_installed == 1 ]; then
for abs_path in `${DB_CONNECT} -c "SELECT path FROM document_storage WHERE type not in ('db');"`; do
if [ -d $abs_path ]; then
echo "backup of filestore"
rsync $abs_path $BACKUP_PATH/filestore -avP
fi
done
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment