Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backslashes in text fields are lost after backup and restore #25

Open
sierracc opened this issue Jun 2, 2022 · 3 comments
Open

Backslashes in text fields are lost after backup and restore #25

sierracc opened this issue Jun 2, 2022 · 3 comments

Comments

@sierracc
Copy link

sierracc commented Jun 2, 2022

Backslashes in String fields are lost after backup and restore.

Original text field content: "data\with\backslash";

mysql-backup4j produces an insert statement like this:
INSERT INTO [...] VALUES ('data\with\backslash')

After restore, the field contains the value "datawithbackslash". This is because MySQL requires backslashes in SQL to be escaped.

@Josel099
Copy link

Josel099 commented Jul 17, 2024

@SeunMatt
When exporting data using this library, I'm encountering the same issue with the handling of backslashes in strings.
This is what I'm getting when I try to export data using this library.

INSERT INTO `backup`(`id`, `saas_client_id`, `created_at`, `created_by`, `last_modified_by`, `modified_at`, `deleted_at`, `deleted_by`, `is_deleted`, `name`, `destination_path`, `filename`, `fs_backup_status`, `db_backup_status`) VALUES 
('4', '1', '2024-07-17 05:06:20.846334', '1', null, null, null, null, 0, 'db Backup test_1', 'C:\Users\Josel\Desktop\backup_test', '20240717-103620.zip', 0, 2);

But the strings with slashes are not being escaped properly. Please note the value of destination_path column "C:\Users\Josel\Desktop\backup_test".
When I try to import it back to DB it doesn't get inserted as the original value. For example it's corrupted like this: "C:UsersJoselDesktop�ackup_test"
While exporting I want to get slashes escaped like the following: "C:\\Users\\Josel\\Desktop\\backup_test".
when I manually export the dump file, the strings with slashes are escaped correctly .

@osteffl
Copy link

osteffl commented Aug 23, 2024

To export all types of characters properly in String type, I needed to do more escaping work - here is my code:

private String getDataInsertStatement(String table) throws SQLException {
...
// escape the single quotes that might be in the value
val = val
.replace("\", "\\") // Escape backslashes first
.replace("'", "\'") // Escape single quotes
.replace(""", "\"") // Escape double quotes, if necessary
.replace("\n", "\n") // Escape newlines
.replace("\r", "\r") // Escape carriage returns
.replace("\t", "\t"); // Escape tabs

@Josel099
Copy link

@osteffl Thanks for sharing this! Your escaping approach is super helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants