RestoreSnapshot restores a MariaDB snapshot of the db to be loaded The project must be stopped and Docker volume removed and recreated for this to work.
(snapshotName string)
| 141 | // RestoreSnapshot restores a MariaDB snapshot of the db to be loaded |
| 142 | // The project must be stopped and Docker volume removed and recreated for this to work. |
| 143 | func (app *DdevApp) RestoreSnapshot(snapshotName string) error { |
| 144 | var err error |
| 145 | err = app.ProcessHooks("pre-restore-snapshot") |
| 146 | if err != nil { |
| 147 | return fmt.Errorf("failed to process pre-restore-snapshot hooks: %v", err) |
| 148 | } |
| 149 | |
| 150 | currentDBVersion := app.Database.Type + "_" + app.Database.Version |
| 151 | |
| 152 | snapshotFile, err := GetSnapshotFileFromName(snapshotName, app) |
| 153 | if err != nil { |
| 154 | return fmt.Errorf("no snapshot found for name %s: %v", snapshotName, err) |
| 155 | } |
| 156 | snapshotFileOrDir := filepath.Join("db_snapshots", snapshotFile) |
| 157 | |
| 158 | hostSnapshotFileOrDir := app.GetConfigPath(snapshotFileOrDir) |
| 159 | |
| 160 | if !fileutil.FileExists(hostSnapshotFileOrDir) { |
| 161 | return fmt.Errorf("failed to find a snapshot at %s", hostSnapshotFileOrDir) |
| 162 | } |
| 163 | |
| 164 | snapshotDBVersion := "" |
| 165 | |
| 166 | // If the snapshot is a directory, (old obsolete style) then |
| 167 | // look for db_mariadb_version.txt in the directory to get the version. |
| 168 | if fileutil.IsDirectory(hostSnapshotFileOrDir) { |
| 169 | // Find out the MariaDB version that correlates to the snapshot. |
| 170 | versionFile := filepath.Join(hostSnapshotFileOrDir, "db_mariadb_version.txt") |
| 171 | if fileutil.FileExists(versionFile) { |
| 172 | snapshotDBVersion, err = fileutil.ReadFileIntoString(versionFile) |
| 173 | if err != nil { |
| 174 | return fmt.Errorf("unable to read the version file in the snapshot (%s): %v", versionFile, err) |
| 175 | } |
| 176 | snapshotDBVersion = strings.Trim(snapshotDBVersion, "\r\n\t ") |
| 177 | snapshotDBVersion = fullDBFromVersion(snapshotDBVersion) |
| 178 | } else { |
| 179 | snapshotDBVersion = "unknown" |
| 180 | } |
| 181 | } else { |
| 182 | // Extract the DB type/version from the filename, supporting both .gz and .zst |
| 183 | m1 := regexp.MustCompile(`((mysql|mariadb|postgres)_[0-9.]+)\.(gz|zst)$`) |
| 184 | matches := m1.FindStringSubmatch(snapshotFile) |
| 185 | if len(matches) > 2 { |
| 186 | snapshotDBVersion = matches[1] |
| 187 | } else { |
| 188 | return fmt.Errorf("unable to determine database type/version from snapshot %s", snapshotFile) |
| 189 | } |
| 190 | |
| 191 | if !(strings.HasPrefix(snapshotDBVersion, "mariadb_") || strings.HasPrefix(snapshotDBVersion, "mysql_") || strings.HasPrefix(snapshotDBVersion, "postgres_")) { |
| 192 | return fmt.Errorf("unable to determine database type/version from snapshot name %s", snapshotFile) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if snapshotDBVersion != currentDBVersion { |
| 197 | return fmt.Errorf("snapshot '%s' is a DB server '%s' snapshot and is not compatible with the configured DDEV DB server version (%s). Please restore it using the DB version it was created with, and then you can try upgrading the DDEV DB version", snapshotName, snapshotDBVersion, currentDBVersion) |
| 198 | } |
| 199 | |
| 200 | status, _ := app.SiteStatus() |