|
2 | 2 | title: Database backup and restore
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -# Database backup and restore examples |
| 5 | +import EnterpriseBadge from '@site/src/components/EnterpriseBadge'; |
| 6 | + |
| 7 | +# Standalone database backup and restore examples |
6 | 8 |
|
7 | 9 | This is a guide on how to backup and restore your database using an Infrahub command line tool.
|
8 | 10 | Please [see this topic](../topics/database-backup) for the requirements for using the tool and an explanation of how it works.
|
@@ -42,4 +44,255 @@ In this example, I am running the backup command on the same machine that is run
|
42 | 44 | python -m utilities.db_backup neo4j restore /infrahub_backups --database-cypher-port=9876
|
43 | 45 | ```
|
44 | 46 |
|
45 |
| -In this example, I am restoring `.backup` files that exist in the `/infrahub_backups` directory and my Infrahub database container uses a non-standard port for cypher access: `9876` instead of `7687`. |
| 47 | +In this example, I am restoring `.backup` files that exist in the `/infrahub_backups` directory and my Infrahub database container uses a non-standard port for cypher access: `9876` instead of `7687`. |
| 48 | + |
| 49 | +## Cluster Database backup and restore examples <EnterpriseBadge /> |
| 50 | + |
| 51 | +### Cluster topology |
| 52 | + |
| 53 | +| Node | Role | |
| 54 | +|------------------|----------| |
| 55 | +| `database` | Leader | |
| 56 | +| `database-core2` | Follower | |
| 57 | +| `database-core3` | Follower | |
| 58 | + |
| 59 | +### Context |
| 60 | + |
| 61 | +We are going to back up the Neo4j database from `database-core2` and restore it on `database-core3`, after having dropped the Neo4j database cluster-wide. |
| 62 | + |
| 63 | +:::caution Important |
| 64 | +Always run backup/restore commands as the `neo4j` user inside the container to avoid permission issues with the data files. |
| 65 | +::: |
| 66 | + |
| 67 | +### Step 1: Create backup from database-core2 |
| 68 | + |
| 69 | +```bash |
| 70 | +docker exec -it -u neo4j infrahub-database-core2-1 bash |
| 71 | +mkdir -p backups |
| 72 | +neo4j-admin database backup --to-path=backups/ |
| 73 | +ls backups |
| 74 | +# Output should include: |
| 75 | +# neo4j-2025-03-24T19-57-18.backup |
| 76 | +``` |
| 77 | + |
| 78 | +### Step 2: Copy the backup to database-core2 |
| 79 | + |
| 80 | +```bash |
| 81 | +docker cp infrahub-database-core2-1:/var/lib/neo4j/backups/neo4j-2025-03-24T19-57-18.backup \ |
| 82 | + infrahub-database-core3-1:/var/lib/neo4j/neo4j-2025-03-24T19-57-18.backup |
| 83 | +``` |
| 84 | + |
| 85 | +### Step 3: Drop the neo4j database across the cluster |
| 86 | + |
| 87 | +Connect to any node |
| 88 | + |
| 89 | +```bash |
| 90 | +cypher-shell -d system -u neo4j |
| 91 | +DROP DATABASE neo4j; |
| 92 | +SHOW SERVERS; |
| 93 | +``` |
| 94 | + |
| 95 | +<center> |
| 96 | + |
| 97 | +</center> |
| 98 | + |
| 99 | +### Step 4: Clean residual data on database-core3 |
| 100 | + |
| 101 | +Connect to the container: |
| 102 | + |
| 103 | +```bash |
| 104 | +docker exec -it -u neo4j infrahub-database-core3-1 bash |
| 105 | +``` |
| 106 | + |
| 107 | +Remove any existing data to avoid corruption: |
| 108 | + |
| 109 | +```bash |
| 110 | +rm -rf /data/databases/neo4j |
| 111 | +rm -rf /data/transactions/neo4j |
| 112 | +``` |
| 113 | + |
| 114 | +Then restart the container to ensure a clean state: |
| 115 | + |
| 116 | +```bash |
| 117 | +docker restart infrahub-database-core3-1 |
| 118 | +``` |
| 119 | + |
| 120 | +### Step 5: Restore the backup on database-core3 |
| 121 | + |
| 122 | +Reconnect to the container: |
| 123 | + |
| 124 | +```bash |
| 125 | +docker exec -it -u neo4j infrahub-database-core3-1 bash |
| 126 | +``` |
| 127 | + |
| 128 | +Run the restore command: |
| 129 | + |
| 130 | +```bash |
| 131 | +neo4j-admin database restore \ |
| 132 | + --from-path=/var/lib/neo4j/neo4j-2025-03-24T19-57-18.backup neo4j |
| 133 | +``` |
| 134 | + |
| 135 | +<center> |
| 136 | + |
| 137 | +</center> |
| 138 | + |
| 139 | +### Step 6: Identify the seed instance id |
| 140 | + |
| 141 | +Connect via Cypher shell (on the system database): |
| 142 | + |
| 143 | +```bash |
| 144 | +cypher-shell -d system -u neo4j |
| 145 | +``` |
| 146 | + |
| 147 | +Run: |
| 148 | + |
| 149 | +```bash |
| 150 | +SHOW SERVERS; |
| 151 | +``` |
| 152 | + |
| 153 | +⚠️ **Note** : |
| 154 | +> Find the `serverId` corresponding to infrahub-database-core3-1. |
| 155 | +>> For example: d05fce79-e63e-485a-9ce7-1abbf9d18fce. |
| 156 | +
|
| 157 | +<center> |
| 158 | + |
| 159 | +</center> |
| 160 | + |
| 161 | +### Step 7: Recreate the neo4j database from the seed |
| 162 | + |
| 163 | +Run the following Cypher command: |
| 164 | + |
| 165 | +```bash |
| 166 | +CREATE DATABASE neo4j |
| 167 | +TOPOLOGY 3 PRIMARIES |
| 168 | +OPTIONS { |
| 169 | + existingData: 'use', |
| 170 | + existingDataSeedInstance: 'd05fce79-e63e-485a-9ce7-1abbf9d18fce' |
| 171 | +}; |
| 172 | +``` |
| 173 | + |
| 174 | +<center> |
| 175 | + |
| 176 | +</center> |
| 177 | + |
| 178 | +### Step 8: Verify cluster sync |
| 179 | + |
| 180 | +Check that the database is coming online: |
| 181 | + |
| 182 | +```bash |
| 183 | +SHOW DATABASES; |
| 184 | +``` |
| 185 | + |
| 186 | +<center> |
| 187 | + |
| 188 | +</center> |
| 189 | + |
| 190 | +Then validate cluster sync status: |
| 191 | + |
| 192 | +```bash |
| 193 | +SHOW SERVERS; |
| 194 | +``` |
| 195 | + |
| 196 | +<center> |
| 197 | + |
| 198 | +</center> |
| 199 | + |
| 200 | +All nodes should eventually show the neo4j database as online. |
| 201 | + |
| 202 | +### 📝 Notes |
| 203 | + |
| 204 | +- If any node shows as **dirty** or **offline**, check the logs and ensure that the file `/data/databases/neo4j/neostore` exists. |
| 205 | +- Restoring the database on a single node does **not** automatically register it with the cluster. |
| 206 | + You **must** run the `CREATE DATABASE ... OPTIONS { existingData: 'use' }` command to register the restored data properly. |
| 207 | + |
| 208 | +## Restore a database cluster backup on a standalone instance (for debug) |
| 209 | + |
| 210 | +### Context |
| 211 | + |
| 212 | +We are taking a backup from a Neo4j cluster and restoring it on a standalone local Neo4j instance (non-clustered), for the purpose of debugging and data analysis in a safe, isolated environment. |
| 213 | + |
| 214 | +### Step 1: Backup from a cluster node <EnterpriseBadge /> |
| 215 | + |
| 216 | +The backup was created from a cluster node (either follower or leader) using: |
| 217 | + |
| 218 | +```bash |
| 219 | +neo4j-admin database backup --to-path=backups/ |
| 220 | +# Resulting file: neo4j-2025-03-24T19-57-18.backup |
| 221 | +``` |
| 222 | + |
| 223 | +### Step 2: Copy the backup to database |
| 224 | + |
| 225 | +```bash |
| 226 | +docker cp neo4j-2025-03-24T19-57-18.backup \ |
| 227 | + infrahub-database-1:/var/lib/neo4j/neo4j-2025-03-24T19-57-18.backup |
| 228 | +``` |
| 229 | + |
| 230 | +### Step 3. Prepare the local neo4j instance |
| 231 | + |
| 232 | +Connect to the container: |
| 233 | + |
| 234 | +```bash |
| 235 | +docker exec -it -u neo4j infrahub-database-1 bash |
| 236 | +``` |
| 237 | + |
| 238 | +Clean any existing neo4j database (optional but recommended): |
| 239 | + |
| 240 | +```bash |
| 241 | +rm -rf /data/databases/neo4j |
| 242 | +rm -rf /data/transactions/neo4j |
| 243 | +``` |
| 244 | + |
| 245 | +Drop the neo4j Database |
| 246 | + |
| 247 | +```bash |
| 248 | +cypher-shell -d system -u neo4j |
| 249 | +DROP DATABASE neo4j; |
| 250 | +SHOW SERVERS; |
| 251 | +``` |
| 252 | + |
| 253 | +<center> |
| 254 | + |
| 255 | +</center> |
| 256 | + |
| 257 | +### Step 4. Restore the backup |
| 258 | + |
| 259 | +Run the restore command from the directory where the backup file is located: |
| 260 | + |
| 261 | +```bash |
| 262 | +neo4j-admin database restore \ |
| 263 | + --from-path=/var/lib/neo4j/neo4j-2025-03-24T19-57-18.backup neo4j |
| 264 | +``` |
| 265 | + |
| 266 | +### Step 5: Recreate the neo4j database |
| 267 | + |
| 268 | +Run the following Cypher command: |
| 269 | + |
| 270 | +```bash |
| 271 | +CREATE DATABASE neo4j |
| 272 | +``` |
| 273 | + |
| 274 | +### Step 6: Verify the status |
| 275 | + |
| 276 | +Check that the database is coming online: |
| 277 | + |
| 278 | +```bash |
| 279 | +SHOW DATABASES; |
| 280 | +``` |
| 281 | + |
| 282 | +<center> |
| 283 | + |
| 284 | +</center> |
| 285 | + |
| 286 | +Then validate database status: |
| 287 | + |
| 288 | +```bash |
| 289 | +SHOW SERVERS; |
| 290 | +``` |
| 291 | + |
| 292 | +<center> |
| 293 | + |
| 294 | +</center> |
| 295 | + |
| 296 | +### 📝 Notes |
| 297 | + |
| 298 | +- This process *restores only data* — not cluster roles, replication, or configuration. |
0 commit comments