Skip to content

Instantly share code, notes, and snippets.

@KangOl
Last active November 16, 2016 10:11
Show Gist options
  • Save KangOl/5bfc73a03e356f2cd588 to your computer and use it in GitHub Desktop.
Save KangOl/5bfc73a03e356f2cd588 to your computer and use it in GitHub Desktop.
restore-db.sh
#!/usr/bin/env bash
# set -x
set -e
set -o pipefail
if [[ -z "$1" ]];then
echo "Usage: $0 [-k] DB [TO]" >&2
exit 1
fi;
keeppass=""
if [[ "$1" = "-k" ]]; then
shift;
keeppass="--"
fi;
SDB=$(basename "${1%.sql.[gx]z}")
DB=$2
if [[ -z "$DB" ]];then
DB=$SDB
fi;
FILE=$1
GENFILE=N
DIR=~/devel/odoo/dbs/
if [[ ${#FILE} -eq ${#SDB} ]]; then
FILE=${DIR}${SDB}.sql.gz
GENFILE=Y
fi;
if [[ ! -f ${FILE} ]];then
if [[ "$GENFILE" = "Y" ]];then
FILE=${DIR}${SDB}.sql.xz
ERRFILE="${DIR}${SDB}.sql.{gz,xz}"
fi
if [[ ! -f ${FILE} ]];then
echo "ENOTFOUND ${ERRFILE:-$FILE}" >&2
exit 1
fi;
fi;
ext="${FILE##*.}"
# functions to set variables $z and $sz depending of the compressor (extension)
function _gz {
F=${FILE}
if [[ -L ${FILE} ]];then
F=$(readlink "${FILE}")
fi;
sz=$(gzip -ql "$F" | awk '{print $2}')
z=$(command -v gzcat)
if [[ -z "$z" ]];then
z=zcat
fi;
}
function _xz {
sz=$(xz --robot --list "$FILE" | awk '{ if ($1=="totals") { print $5 } }')
z="xz -d -c"
}
dropdb --if-exists "$DB"
createdb "$DB"
"_$ext"
$z "$FILE" | pv -s "$sz" | psql -X -q -o /dev/null -d "$DB"
psql -X -q -d "$DB" -f - <<SQL
UPDATE ir_cron SET active=false;
${keeppass} UPDATE res_users SET password='a';
UPDATE res_users SET login='admin' WHERE id=1;
UPDATE ir_mail_server SET smtp_host='127.0.0.1', smtp_port=1025,
smtp_user=NULL, smtp_pass=NULL,
smtp_encryption='none', smtp_debug=false;
DO \$\$
BEGIN UPDATE auth_oauth_provider SET enabled=false;
EXCEPTION WHEN undefined_table THEN NULL;
END;
\$\$;
\x on
SELECT latest_version FROM ir_module_module WHERE name='base';
SQL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment