SeunMatt/mysql-backup4j

Backslashes in text fields are lost after backup and restore

sierracc opened this issue · 3 comments

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.

@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 .

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

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