Bug 15664 - koha-restore: Allow database dump to be restored to another Koha instance
Summary: koha-restore: Allow database dump to be restored to another Koha instance
Status: NEW
Alias: None
Product: Koha
Classification: Unclassified
Component: Command-line Utilities (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement
Assignee: Galen Charlton
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks: 35502
  Show dependency treegraph
 
Reported: 2016-01-26 04:31 UTC by Mason James
Modified: 2023-12-06 09:40 UTC (History)
3 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mason James 2016-01-26 04:31:46 UTC
Allow database dump to be restored to another Koha instance

more info to come...
Comment 1 Rudolf Byker 2019-05-10 10:12:57 UTC
Related: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=4875
Comment 2 Marcel de Rooy 2019-07-19 10:02:01 UTC
Problem is the hardcoded database name in the sql dump coming from the --databases parameter of mysqldump.
We could make this an option ?
Comment 3 Marcel de Rooy 2019-07-19 10:07:40 UTC
(In reply to Marcel de Rooy from comment #2)
> Problem is the hardcoded database name in the sql dump coming from the
> --databases parameter of mysqldump.
> We could make this an option ?

And it would need passing the instance name to mysql in koha-restore.
Comment 4 Marcel de Rooy 2019-07-19 10:22:22 UTC
diff without.sql  with.sql
18a19,26
> -- Current Database: `koha_master`
> --
>
> CREATE DATABASE /*!32312 IF NOT EXISTS*/ `koha_master` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
>
> USE `koha_master`;
>
> --
6743c6751
< -- Dump completed on 2019-07-19 10:20:42
---
> -- Dump completed on 2019-07-19 10:16:49
Comment 5 Marcel de Rooy 2019-07-19 10:27:50 UTC
Bug 23346 does the first part of the job.
Comment 6 Rudolf Byker 2019-07-19 11:48:56 UTC
Another option is to use sed to replace the USE and CREATE DATABASE commands using regular expressions. This can be done using pipes, without creating a new dump. I wrote this bash function a while ago, which imports a gzipped Koha sql-dump. The first argument is the name of the database you want to create, and the second argument is the file name to import from.

function import_db {
    DB="$1"
    F="$2"

    # Change the database names when importing.
    REPLACE1="s/^CREATE DATABASE.*/CREATE DATABASE ${DB};/"
    REPLACE2='s/USE `koha_rsc`;/USE `'"${DB}"'`;/'

    # Can't import if it exists, so drop it.
    drop_db "$DB"

    echo "Importing database $DB from $F …"
    zcat "${F}" | sed "${REPLACE1}; ${REPLACE2}" | mysql -u "$MYSQL_USER" -p"$MYSQL_PASS"
}
Comment 7 Rudolf Byker 2019-07-19 11:50:13 UTC
Sorry, you also need this function:

function drop_db {
    DB="$1"
    echo "Dropping $DB …"
    mysql -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "DROP DATABASE IF EXISTS $DB;"
}
Comment 8 Marcel de Rooy 2019-07-19 12:46:28 UTC
(In reply to Rudolf Byker from comment #6)
> Another option is to use sed to replace the USE and CREATE DATABASE commands
> using regular expressions. This can be done using pipes, without creating a
> new dump. I wrote this bash function a while ago, which imports a gzipped
> Koha sql-dump. The first argument is the name of the database you want to
> create, and the second argument is the file name to import from.

Thx for sharing.
Personally I would opt for creating a dump without db-name instead of search/replace. With a bad databasename you will be in trouble..
But sure, it can be done.