This is a post is a very condensed version of my last 24hrs, although it hasn’t actually worked yet so i’m also hoping someone will post where it went wrong below this.
My Discourse update failed due to a duplicate key, one of my tags is doubled up. To fix the update issue i needed to do a fresh Discourse install and then load my latest backup, but reloading fails as it gets angry about the duplicate key. So i needed to go inside the backup to edit the offending tag to something different.
For some reason the re-zipped backup with the duplicate tag issue fixed is significantly smaller than the backup it came from, and fails when i try to restore it so something went a bit wrong with the rezipping process.
1) Locating Backups: To locate your Discourse backups, you can use the following command:
sudo find / -name "*.tar.gz"
This will search your system for all backup files with the “.tar.gz” extension. By default it should be inside your container at: shared/backups/default
2) Making a Copy: Once you’ve found the backup you want to work with, create a copy of it to ensure you have a backup of the original file. Use the “cp” command:
bash
sudo cp /path/to/original_backup.tar.gz /path/to/copy_backup.tar.gz
3) Extracting the Copy: Extract the content of the copied backup file using the “tar” command:
bash
tar -xzvf /path/to/copy_backup.tar.gz
This will extract the backup files to a temporary directory.
4) Editing the Tags in the Database: Navigate to the extracted backup files and open the relevant database file using a text editor. I encountered an issue with duplicate “socialmedia” tags, which prevented successful restoration. In a big database there’s loads of instances of tags, and likely for the specific tag you’re searching for so i searched for ‘immutable socialmedia’ using Ctrl W in Nano which took me right there.
sudo nano /path/to/extracted_database.sql
I edited one instance of the “socialmedia” tag to “socialmedia2”, then did a quick search to check it only appears only once now. I can fix those tags from the admin section once the restore succeeds.
5) Rezipping: After editing the backup files, create a new backup file with the corrected content. Use the following command to compress the modified files:
tar -czvf /path/to/new_modified_backup.tar.gz /path/to/modified_files_directory
6) Moving to the Correct File: Move the new modified backup file to the appropriate directory where backups are stored. The default location is usually “/shared/backups/default”:
sudo mv /path/to/new_modified_backup.tar.gz /shared/backups/default/
7) Stopping and Starting Services: Before restoring the modified backup, making sure to stop the relevant services to avoid potential conflicts during the restoration process. Use the “./launcher stop app” command to stop the Discourse application:
./launcher stop app
8) Restoring the Backup: To restore from the modified backup, use the “discourse restore” command with the path to the new backup file:
discourse restore /shared/backups/default/new_modified_backup.tar.gz
Or you can do it via /admin on your site as it should now appear in the backups section.
9) Verifying Restoration: After the restoration process completed, I verified that the changes were successful by checking the Discourse application and database to ensure that the duplicate “socialmedia” tags were removed.
10) Starting Services: I restarted the services that were stopped earlier to bring the Discourse application back online. I used the “./launcher start app” command to start the Discourse application:
./launcher start app
11) Deleting Temporary Files and Extra Backups: After successfully restoring the backup, I deleted any temporary files and extra backups that were created during the process to free up disk space. Use the “rm” command to remove the files:
sudo rm -r /path/to/temporary_directory
sudo rm /path/to/copy_backup.tar.gz