0
0
Fork 0
mirror of https://github.com/dani-garcia/vaultwarden synced 2024-09-27 21:08:55 +02:00

Updated migration text so that the password will never be on the cli/history and added query to disable foreign key checking.

Mathijs van Veluw 2020-09-23 20:00:47 +02:00
parent f879aacfeb
commit d06921ea15

@ -3,7 +3,7 @@ To use the MySQL backend, you can either use the [official Docker image](https:/
To run the binary or container ensure the ```DATABASE_URL``` environment variable is set (i.e. ```DATABASE_URL='mysql://<user>:<password>@mysql/bitwarden'```) and ```ENABLE_DB_WAL``` is set to false ```ENABLE_DB_WAL='false'``` . To run the binary or container ensure the ```DATABASE_URL``` environment variable is set (i.e. ```DATABASE_URL='mysql://<user>:<password>@mysql/bitwarden'```) and ```ENABLE_DB_WAL``` is set to false ```ENABLE_DB_WAL='false'``` .
**Connection String Syntax:** **Connection String Syntax:**
``` ```ini
DATABASE_URL=mysql://[[user]:[password]@]host[:port][/database] DATABASE_URL=mysql://[[user]:[password]@]host[:port][/database]
``` ```
If your password contains special characters, you will need to use percentage encoding. If your password contains special characters, you will need to use percentage encoding.
@ -15,7 +15,7 @@ If your password contains special characters, you will need to use percentage en
A complete list of codes can be found on [Wikipedia page for percent encoding](https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters) A complete list of codes can be found on [Wikipedia page for percent encoding](https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters)
**Example using Docker:** **Example using Docker:**
``` ```bash
# Start a mysql container # Start a mysql container
docker run --name mysql --net <some-docker-network>\ docker run --name mysql --net <some-docker-network>\
-e MYSQL_ROOT_PASSWORD=<my-secret-pw>\ -e MYSQL_ROOT_PASSWORD=<my-secret-pw>\
@ -44,26 +44,41 @@ mysql://dbuser:yourpassword@192.168.1.10:3306/bitwarden
An easy way of migrating from SQLite to MySQL has been described in this [issue comment](https://github.com/dani-garcia/bitwarden_rs/issues/497#issuecomment-511827057). The steps are repeated below. Please, note that you are using this at your won risk and you are strongly advised to backup your installation and data! An easy way of migrating from SQLite to MySQL has been described in this [issue comment](https://github.com/dani-garcia/bitwarden_rs/issues/497#issuecomment-511827057). The steps are repeated below. Please, note that you are using this at your won risk and you are strongly advised to backup your installation and data!
1. Create an new (empty) database for bitwarden_rs: 1. Create an new (empty) database for bitwarden_rs:
```CREATE DATABASE bitwarden_rs;``` ```sql
2. Create a new database user and grant rights to database: CREATE DATABASE bitwarden_rs;
``` ```
2. Create a new database user and grant rights to database:
```sql
CREATE USER 'bitwarden_rs'@'localhost' IDENTIFIED BY 'yourpassword'; CREATE USER 'bitwarden_rs'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL ON `bitwarden_rs`.* TO 'bitwarden_rs'@'localhost'; GRANT ALL ON `bitwarden_rs`.* TO 'bitwarden_rs'@'localhost';
FLUSH PRIVILEGES; FLUSH PRIVILEGES;
``` ```
You might want to try a restricted set of grants: You might want to try a restricted set of grants:
``` ```sql
CREATE USER 'bitwarden_rs'@'localhost' IDENTIFIED BY 'yourpassword'; CREATE USER 'bitwarden_rs'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALTER, CREATE, DELETE, DROP, INDEX, INSERT, SELECT, UPDATE ON `bitwarden_rs`.* TO 'bitwarden_rs'@'localhost'; GRANT ALTER, CREATE, DELETE, DROP, INDEX, INSERT, SELECT, UPDATE ON `bitwarden_rs`.* TO 'bitwarden_rs'@'localhost';
FLUSH PRIVILEGES; FLUSH PRIVILEGES;
``` ```
3. Configure bitwarden_rs and start it, so diesel can run migrations and set up the schema properly. Do not do anything else. 3. Configure bitwarden_rs and start it, so diesel can run migrations and set up the schema properly. Do not do anything else.
4. Stop bitwarden_rs. 4. Stop bitwarden_rs.
5. Dump your existing SQLite database: ```sqlite3 db.sqlite3 .dump > sqlitedump.sql``` 5. Dump your existing SQLite database using the following command. Double check the name of your sqlite database, default should be db.sqlite.<br>
NB: On Debian (Buster), you'll need to install sqlite3 for this **Note:** You need the sqlite3 command installed on your Linux system.<br>
6. Drop schema creation and diesel metadata from your dump, leaving only your actual data: ```grep "INSERT INTO" sqlitedump.sql | grep -v "__diesel_schema_migrations" > mysqldump.sql``` We need to remove some queries from the output of the sqlite dump like create table etc.. we will do that here.<br><br>
7. Load your MySQL dump: ```mysql -ubitwarden_rs -pyourpassword < mysqldump.sql``` You either can use this one-liner:
8. Start bitwarden_rs. ```bash
sqlite3 db.sqlite3 .dump | grep "^INSERT INTO" | grep -v "__diesel_schema_migrations" > sqlitedump.sql ; echo -ne "SET FOREIGN_KEY_CHECKS=0;\n$(cat sqlitedump.sql)" > mysqldump.sql
```
Or the following right after each other:
```bash
sqlite3 db.sqlite3 .dump | grep "^INSERT INTO" | grep -v "__diesel_schema_migrations" > sqlitedump.sql
echo "SET FOREIGN_KEY_CHECKS=0;" > mysqldump.sql
cat sqlitedump.sql >> mysqldump.sql
```
6. Load your MySQL dump:
```bash
mysql --force --password --user=bitwarden_rs --database=bitwarden_rs < mysqldump.sql
```
7. Start bitwarden_rs again.
*Note: Loading your MySQL dump with ```--show-warnings``` will highlight that the datetime fields are getting truncated during the import which **seems** to be okay.* *Note: Loading your MySQL dump with ```--show-warnings``` will highlight that the datetime fields are getting truncated during the import which **seems** to be okay.*
``` ```
@ -76,11 +91,11 @@ Note (Code 1265): Data truncated for column 'updated_at' at row 1
error (1064): Syntax error near '"users" VALUES('9b5c2d13-8c4f-47e9-bd94-f0d7036ff581'*********) error (1064): Syntax error near '"users" VALUES('9b5c2d13-8c4f-47e9-bd94-f0d7036ff581'*********)
``` ```
fix: fix:
``` ```bash
sed -i s#\"#\#g mysqldump.sql sed -i s#\"#\#g mysqldump.sql
``` ```
``` ```bash
mysql -ubitwarden_rs pyourpassword mysql --password --user=bitwarden_rs
use bitwarden_rs use bitwarden_rs
source /bw-data/mysqldump.sql source /bw-data/mysqldump.sql
exit exit