|
1 | | -import com.bmuschko.gradle.docker.tasks.container.DockerCopyFileFromContainer |
2 | | - |
3 | 1 | import com.liferay.docker.workspace.environments.Util |
4 | 2 |
|
5 | 3 | import groovy.json.JsonSlurper |
@@ -32,26 +30,76 @@ withZone(ZoneId.systemDefault()) |
32 | 30 |
|
33 | 31 | String timestamp = formatter.format(now) |
34 | 32 |
|
35 | | -tasks.register("exportContainerData", DockerCopyFileFromContainer) { |
36 | | - containerId = "${config.namespace}-data-helper" |
37 | | - |
| 33 | +tasks.register("exportContainerData") { |
38 | 34 | doFirst { |
39 | | - mkdir hostPath.get() |
| 35 | + String hostPath = file("exported_data/data_${config.namespace}_${timestamp}").absolutePath |
40 | 36 |
|
41 | | - waitForCommand("docker compose create data-helper") |
42 | | - } |
| 37 | + getExistingVolumeNames().each { |
| 38 | + String volumeName -> |
43 | 39 |
|
44 | | - doLast { |
45 | | - updateGradleLocalProperties(["lr.docker.environment.data.directory" : hostPath.get()]) |
| 40 | + String backupFileName = "${volumeName.substring(config.namespace.length() + 1)}.tar" |
46 | 41 |
|
47 | | - logger.lifecycle("\nUpdated gradle-local.properties with the new value:\nlr.docker.environment.data.directory=${hostPath.get()}") |
48 | | - } |
| 42 | + if (backupFileName.equals("dumps.tar")) { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + println "Creating backup of volume ${volumeName} in ${hostPath}/${backupFileName}" |
| 47 | + waitForCommand("docker run --rm -v ${volumeName}:/source -v ${hostPath}:/target busybox:latest tar --create --file=/target/${backupFileName} --directory=/source .") |
| 48 | + println "Created backup of volume ${volumeName} in ${hostPath}/${backupFileName}" |
| 49 | + } |
49 | 50 |
|
50 | | - hostPath = provider { |
51 | | - "exported_data/data_${config.namespace}_${timestamp}" |
| 51 | + updateGradleLocalProperties(["lr.docker.environment.data.directory" : hostPath]) |
| 52 | + |
| 53 | + logger.lifecycle("\nUpdated gradle-local.properties with the new value:\nlr.docker.environment.data.directory=${hostPath}") |
52 | 54 | } |
| 55 | +} |
| 56 | + |
| 57 | +tasks.register("importContainerData") { |
| 58 | + mustRunAfter ":buildDockerImage" |
| 59 | + |
| 60 | + doFirst { |
| 61 | + List<String> previousVolumeNames = getExistingVolumeNames() |
| 62 | + |
| 63 | + waitForCommand("docker compose build") |
| 64 | + waitForCommand("docker compose create") |
| 65 | + |
| 66 | + List<String> currentVolumeNames = getExistingVolumeNames() |
| 67 | + |
| 68 | + project.file(config.dataDirectory).listFiles().each { |
| 69 | + File backupFile -> |
| 70 | + |
| 71 | + if (backupFile.isFile() && !backupFile.name.endsWith(".tar")) { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + String volumeName = "${config.namespace}_${backupFile.name}" |
| 76 | + |
| 77 | + if (backupFile.isFile()) { |
| 78 | + volumeName = volumeName.substring(0, volumeName.length() - 4) |
| 79 | + } |
| 80 | + |
| 81 | + if (!currentVolumeNames.contains(volumeName)) { |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + if (previousVolumeNames.contains(volumeName)) { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + println "Restoring backup of volume ${volumeName} using ${backupFile.absolutePath}" |
53 | 90 |
|
54 | | - remotePath = "/container-data" |
| 91 | + if (backupFile.isFile()) { |
| 92 | + waitForCommand("docker run --rm -v ${volumeName}:/target -v ${backupFile.absolutePath}:/source.tar busybox:latest tar --extract --file=/source.tar --directory=/target/") |
| 93 | + } |
| 94 | + else { |
| 95 | + waitForCommand("docker run --rm -v ${volumeName}:/target -v ${backupFile.absolutePath}:/source busybox:latest sh -c 'cp --update --recursive /source/* /target/'") |
| 96 | + } |
| 97 | + |
| 98 | + println "Restored backup of volume ${volumeName} using ${backupFile.absolutePath}" |
| 99 | + |
| 100 | + println waitForCommand("docker run --rm -v ${volumeName}:/${volumeName} busybox:latest du -sh /${volumeName}/") |
| 101 | + } |
| 102 | + } |
55 | 103 | } |
56 | 104 |
|
57 | 105 | tasks.register("importDocumentLibraryStructure") { |
@@ -160,6 +208,7 @@ composeUp { |
160 | 208 | } |
161 | 209 |
|
162 | 210 | dependsOn ":buildDockerImage" |
| 211 | + dependsOn ":importContainerData" |
163 | 212 | dependsOn ":importDatabaseDumps" |
164 | 213 |
|
165 | 214 | doFirst { |
|
0 commit comments