OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Docs: restore deleted project (#14640)
Markus Kahl97bd4e2c8284
diff --git a/docs/installation-and-operations/operation/restoring/README.md b/docs/installation-and-operations/operation/restoring/README.mdindex f35ec5c1b1e..2ceb1a67a8c 100644--- a/docs/installation-and-operations/operation/restoring/README.md+++ b/docs/installation-and-operations/operation/restoring/README.md@@ -6,6 +6,10 @@ sidebar_navigation:# Restoring an OpenProject backup+This document describes how to restore a complete backup of OpenProject.++Please look [here](./restoring-a-deleted-project/README.md) if you want to restore a deleted project from a backup.+## Package-based installation (DEB/RPM)Assuming you have a backup of all the OpenProject files at hand (see the [Backing up](../backing-up) guide), here is how you would restore your OpenProject installation from that backup.@@ -73,7 +77,7 @@ sudo openproject config:get DATABASE_URL#=> e.g.: postgres://<dbusername>:<dbpassword>@<dbhost>:<dbport>/<dbname>```-Then, to restore the PostgreSQL dump please use the `pg_restore` command utility. **WARNING:** The command `--clean --if-exists` is used and it will drop objects in the database and you will lose all changes in this database! Double-check that the database URL above is the database you want to restore to.+Then, to restore the PostgreSQL dump please use the `pg_restore` command utility. **WARNING:** The command `--clean --if-exists` is used and it will drop objects in the database and you will lose all changes in this database! Double-check that the database URL above is the database you want to restore to.This is necessary since the backups of OpenProject does not clean statements to remove existing options and will lead to duplicate index errors when trying to restore to an existing database. The alternative is to drop/recreate the database manually (see below), if you have the permissions to do so.@@ -129,7 +133,7 @@ If you are using docker-compose this is what you do after you started everything1. Stop the OpenProject container using `docker-compose stop web worker`.2. Drop the existing, seeded database using `docker exec -it compose_db_1 psql -U postgres -c 'drop database openproject;'`-3. If your database doesn't have an openproject user yet, create it with this command: `docker exec -it compose_db_1 psql -U postgres -c 'create user openproject;'`+3. If your database doesn't have an openproject user yet, create it with this command: `docker exec -it compose_db_1 psql -U postgres -c 'create user openproject;'`4. Recreate the database using `docker exec -it compose_db_1 psql -U postgres -c 'create database openproject owner openproject;'`5. Copy the dump onto the container: `docker cp openproject.sql compose_db_1:/`6. Source the dump with psql on the container: `docker exec -it compose_db_1 psql -U postgres` followed first by `\c openproject` and then by `\i openproject.sql`. You can leave this console by entering `\q` once it's done.@@ -207,7 +211,7 @@ but then you use `pg_restore` instead to restore it.# 1. copy .pgdump file into containerdocker cp postgresql-dump-20211119210038.pgdump postgres:/-# 2. delete existing database created in step 2) above+# 2. delete existing database created in step 2) abovedocker exec -it postgres dropdb -U postgres openproject# 3. import the dumpdiff --git a/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/README.md b/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/README.mdnew file mode 100644index 00000000000..7f058d9ed07--- /dev/null+++ b/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/README.md@@ -0,0 +1,70 @@+# Restoring a deleted project++Sometimes it may happen that you delete a project on accident.+Perhaps it would be too much trouble or more recent data lost to restore a complete backup.++For these kinds of cases we describe here how to restore a single project from a backup.+The following files will be used in the examples.++* [dump.sql](./dump.sql)+* [restore.sql](./restore.sql)++There is also a script ([restore.sh](./restore.sh)) that shows how to use everything together.++## 1. Dump project data from backup++First we copy the data from the backup, that was deleted later.+For each relevant table, a `missing_<table-name>` table is created+that only contains the missing data.++This is then saved to `missing_data.sql` which will be used to import+the missing data into the current database.++Before performing the next step, edit the `dump.sql` file and change the missing project ID+to the correct value in the head of the file where it says 'DEFINE MISSING PROJECT ID HERE'.++```+cat dump.sql | psql -d openproject_backup++pg_dump -d openproject_backup -t 'missing_*' -f missing_data.sql+```++## 2. Restore missing data++Now that we have the missing data, we can restore it in the current database.++```+cat missing_data.sql | psql -d openproject+cat restore.sql | psql -d openproject+```++First we create the tables with the missing data in the current database.+This has no effect on the actual data of OpenProject yet.+Only with executing `restore.sql` will the data be copied from the `missing_<table-name>` tables+into the corresponding actual `<table-name>` tables.++After this is done, the `missing_*` tables are dropped.+This all happens within a transaction.++**Project hierarchy**++Now it can happen that the project hierarchy does not look correct in the projects dropdown after+you restored a deleted project. To fix this, delete the restored project's `lft` and `rgt` columns+and rebuild the hierarchy.++To do this, start an OpenProject console (e.g. `sudo openproject run console`) and execute the following.++```+p = Project.find_by(name: "Restored project")++p.update_column :lft, nil+p.update_column :rgt, nil++Project.rebuild!+```++This may take a moment depending on the number of projects you have.+After this is finished, the project hierarchy will look correct again.++This operation is perfectly safe and will not affect any actual data.+It just affects the display of projects in the projects dropdown and projects list.diff --git a/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/dump.sql b/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/dump.sqlnew file mode 100644index 00000000000..5f32806030d--- /dev/null+++ b/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/dump.sql@@ -0,0 +1,169 @@+SET search_path="public";+SET client_min_messages TO WARNING;++BEGIN;++CREATE SCHEMA helpers;++CREATE OR REPLACE FUNCTION helpers.missing_project_id()+RETURNS integer+AS+$$+SELECT 773 -- DEFINE MISSING PROJECT ID HERE+$$+LANGUAGE SQL IMMUTABLE STRICT;++CREATE OR REPLACE FUNCTION helpers.camel_to_down_case(identifier TEXT)+RETURNS text+AS+$$+SELECT regexp_replace(lower(regexp_replace(identifier, E'([A-Z])', E'\_\\1','g')), E'^_', '', 'g');+$$+LANGUAGE SQL IMMUTABLE STRICT;++CREATE OR REPLACE FUNCTION helpers.pluralize(table_name TEXT)+RETURNS text+AS+$$+SELECT regexp_replace(lower(regexp_replace(table_name, E'y$', E'ie','g')), E'([^s])$', E'\\1s', 'g');+$$+LANGUAGE SQL IMMUTABLE STRICT;++-- remove all the missing_* tables we created during the last try if there are any+DO+$$+DECLARE table_to_remove TEXT;+BEGIN+FOR table_to_remove IN (+ select table_name+ from information_schema.tables+ where table_schema = 'public' and table_name like 'missing_%'+)+LOOP+ EXECUTE 'DROP TABLE IF EXISTS ' || table_to_remove || ' CASCADE';+END LOOP;+END;+$$;++-- restore missing rows in all tables where deleted project was referenced (via project_id)+DO+$$+DECLARE source_table TEXT;+DECLARE source_column TEXT;+BEGIN++EXECUTE 'CREATE TABLE missing_projects AS (select * from projects where id = ' || helpers.missing_project_id() || ');';++FOR source_table, source_column IN (+ select table_name, column_name+ from information_schema.columns+ where table_schema = 'public' and column_name = 'project_id'+ and table_name not like '%_journals'+ and table_name != 'grids'+ -- apparently grids do not get deleted when a project is which is why we skip them here+ -- otherwise there will be an 'already exists' error during restoration later+)+LOOP+ EXECUTE 'CREATE TABLE missing_' || source_table || ' AS (' ||+ 'SELECT * from ' || source_table || ' where project_id = ' || helpers.missing_project_id() || ')';+END LOOP;++END;+$$;++-- BEGIN: restore rows related to restrored rows above+--+-- todo: This could probably be scripted to cover all relevant tables as well.+-- But for now it's easier to just list them by hand. This is the only+-- part of this script which may have to be adapted in the future to+-- account for new tables or relations.++CREATE TABLE missing_attachments AS (+ select * from attachments where container_type = 'WorkPackage' and container_id in (+ select id from missing_work_packages+ )+);++CREATE TABLE missing_meeting_contents AS (+ select * from meeting_contents where meeting_id in (select id from missing_meetings)+);++CREATE TABLE missing_messages AS (+ select * from messages where forum_id in (select id from missing_forums)+);++CREATE TABLE missing_wiki_pages AS (+ select * from wiki_pages where wiki_id in (select id from missing_wikis)+);++CREATE TABLE missing_changesets AS (+ select * from changesets where repository_id in (select id from missing_repositories)+);++CREATE TABLE missing_member_roles AS (+ select * from member_roles where member_id in (select id from missing_members)+);++-- END: restore rows related to restrored rows above++-- create missing journals and journal data+DO+$$+DECLARE journable_type_name TEXT;+DECLARE clauses TEXT;+DECLARE clause TEXT;+BEGIN++clauses := '(journable_id = ' || helpers.missing_project_id() || ' and journable_type = ''Project'')';++FOR journable_type_name IN (+ select distinct(journable_type) from journals where journable_type != 'Project'+)+LOOP+ clause := '(journable_id in (select id from missing_' ||+ helpers.pluralize(helpers.camel_to_down_case(journable_type_name)) ||+ ') and journable_type = ''' || journable_type_name ||+ ''')';+ clauses := FORMAT('%s or %s', clauses, clause);+END LOOP;++EXECUTE 'CREATE TABLE missing_journals AS (select * from journals where ' || clauses || ')';++FOR journable_type_name IN (+ select distinct(journable_type) from journals where journable_type != 'Project'+)+LOOP+ EXECUTE 'CREATE TABLE missing_' || helpers.camel_to_down_case(journable_type_name) || '_journals AS (' ||+ 'select * from ' || helpers.camel_to_down_case(journable_type_name) || '_journals where id in (' ||+ 'select data_id from missing_journals where journable_type = ''' || journable_type_name || '''))';+END LOOP;++END;+$$;++-- create missing custom values+DO+$$+DECLARE customized_type_name TEXT;+DECLARE query TEXT;+DECLARE clause TEXT;+BEGIN++query := 'SELECT * from custom_values where (customized_type = ''Project'' and customized_id = ' || helpers.missing_project_id() || ')';++FOR customized_type_name IN (select distinct(customized_type) from custom_values where customized_type != 'Project')+LOOP+ clause := '(customized_type = ''' || customized_type_name ||+ ''' and customized_id in (select id from missing_' ||+ helpers.pluralize(helpers.camel_to_down_case(customized_type_name)) || '))';+ query := FORMAT('%s or %s', query, clause);+END LOOP;++EXECUTE 'CREATE TABLE missing_custom_values AS (' || query || ')';++END;+$$;++DROP SCHEMA helpers CASCADE;++COMMIT;diff --git a/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/restore.sh b/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/restore.shnew file mode 100644index 00000000000..3cb91fe53b6--- /dev/null+++ b/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/restore.sh@@ -0,0 +1,12 @@+#!/bin/bash++# Dump missing data from backup.+# This assumes you restored the correct backup into a database called `openproject_backup`.+# Edit `dump.sql` to define the missing project ID first!+cat dump.sql | psql -d openproject_backup+pg_dump -d openproject_backup -t 'missing_*' -f missing_data.sql++# Restore missing data in current database.+# This assumes your current OpenProject database is called `openproject`.+cat missing_data.sql | psql -d openproject+cat restore.sql | psql -d openprojectdiff --git a/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/restore.sql b/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/restore.sqlnew file mode 100644index 00000000000..d521c99dc13--- /dev/null+++ b/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/restore.sql@@ -0,0 +1,41 @@+SET search_path="public";++BEGIN;++DO+$$+DECLARE table_to_copy TEXT;+BEGIN+FOR table_to_copy IN (+ select table_name+ from information_schema.tables+ where table_schema = 'public' and table_name like 'missing_%'+ order by table_name = 'missing_projects' desc+ -- order such that project is re-created first to satisfy foreign key constraints+)+LOOP+ raise notice 'Restoring % to %', table_to_copy, REPLACE(table_to_copy, 'missing_', '');++ EXECUTE 'INSERT INTO ' || REPLACE(table_to_copy, 'missing_', '') ||+ ' (SELECT * FROM ' || table_to_copy || ');';+END LOOP;+END;+$$;++-- remove all the missing_* tables we created after we're done+DO+$$+DECLARE table_to_remove TEXT;+BEGIN+FOR table_to_remove IN (+ select table_name+ from information_schema.tables+ where table_schema = 'public' and table_name like 'missing_%'+)+LOOP+ EXECUTE 'DROP TABLE IF EXISTS ' || table_to_remove || ' CASCADE';+END LOOP;+END;+$$;++COMMIT;