Skip to content

Instantly share code, notes, and snippets.

@AlmostGosu
Last active January 3, 2023 19:16
Show Gist options
  • Save AlmostGosu/6220fbf8adb7f46044e4578f67f3a28e to your computer and use it in GitHub Desktop.
Save AlmostGosu/6220fbf8adb7f46044e4578f67f3a28e to your computer and use it in GitHub Desktop.
Shell script to recover corrupted Confluence h2 database
#!/bin/bash
# This script has to be run as your confluence user or root
# Create a backup of, and attempt to restore a confluence h2 internal database
# Go home.
cd ~
# Stop confluence
/opt/atlassian/confluence/bin/stop-confluence.sh
# Really though...
pgrep java | xargs kill
#create a good backup of your database files
mkdir -p ~/confluence-db-restore/originals
cp /var/atlassian/application-data/confluence/database/*.db ~/confluence-db-restore/originals
# create another good backup of your database files
tar -cvzf ~/confluence-db-restore/"$(date '+%Y-%m-%d')_h2db_backup.tar.gz" ~/confluence-db-restore/originals
# create a copy of your db to work from
cp ~/confluence-db-restore/originals/*.db ~/confluence-db-restore
# Create recovery SQL script
/opt/atlassian/confluence/jre/bin/java -cp \
/opt/atlassian/confluence/confluence/WEB-INF/lib/h2-*.jar \
org.h2.tools.Recover
# move working files and restore database
mkdir -p ~/confluence-db-restore/recovery
mv *.sql ~/confluence-db-restore/recovery
cd ~/confluence-db-restore/recovery
/opt/atlassian/confluence/jre/bin/java -cp \
/opt/atlassian/confluence/confluence/WEB-INF/lib/h2-*.jar \
org.h2.tools.RunScript -url jdbc:h2:./h2db -user sa -script *.sql
################################################################################
# IMPORTANT THAT YOU MOVE THE RECOVERED DATABASE IF THE RESTORE WAS SUCCESSFUL #
# Uncomment this line to copy the restored database over your existing #
################################################################################
# cp -f ~/confluence-db-restore/recovery/*.db /var/atlassian/application-data/confluence/database/
# chown -R confluence:confluence /var/atlassian/application-data/confluence
systemctl restart confluence
@ramkumarrreddychalla
Copy link

hi Team, java -cp /lib/h2-*.jar org.h2.tools.Recover step is creaing a .sql file with table names O_Number instead of actual table names.

INSERT INTO O_87 VALUES(10606544, TIMESTAMP '2022-11-28 13:44:14.143', 1, 'comments ', NULL, 635, 188, NULL);

please let me know how to fix this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment