Copying Pages to Another Confluence with Attachments

Michael Winters November 14, 2024

I need to copy a Parent Page and all of its Children Pages (and their attachments) from one confluence site to another (both server based). I have tried to use the following code but the /copy url for all my pages returns 404 for me. I also tried another way using requests.post but that didn't copy over the attachments correctly. How can I copy a group of child pages from one confluence to another via python script?

import json

import requests

# Source Confluence API URL

source_page_id = "dummy_page_id"

source_url = f"https://source_confluence_instance.com/rest/api/content/{source_page_id}"

# Destination Confluence (target) API URL

destination_page_id = "dummy_destination_page_id"

destination_url = f"https://destination_confluence_instance.com/rest/api/content/{destination_page_id}/copy"

# Bearer tokens for source and destination

source_token = "dummy_source_token"

destination_token = "dummy_destination_token"

# Set up headers for source Confluence API request

source_headers = {

"Authorization": f"Bearer {source_token}",

"Accept": "application/json"

}

# Get page content from source Confluence

source_response = requests.get(source_url, headers=source_headers)

if source_response.status_code == 200:

source_page_data = source_response.json()

# Define payload to include content and attachments for copying

payload = json.dumps({

"copyAttachments": True,

"copyPermissions": True,

"copyProperties": True,

"copyLabels": True,

"copyCustomContents": True,

"copyDescendants": True,

"destinationPageId": destination_page_id,

"titleOptions": {

"replace": "",

"search": ""

}

})

# Set up headers for destination Confluence API request

destination_headers = {

"Authorization": f"Bearer {destination_token}",

"Accept": "application/json",

"Content-Type": "application/json"

}

# Send the page copy request to destination Confluence

destination_response = requests.post(destination_url, data=payload, headers=destination_headers)

if destination_response.status_code == 200:

print("Page successfully copied to the destination Confluence instance.")

else:

print(f"Failed to copy page to destination. Error: {destination_response.status_code} - {destination_response.text}")

else:

print(f"Failed to fetch page from source Confluence. Error: {source_response.status_code} - {source_response.text}")

I tried following https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-content---children-and-descendants/#api-wiki-rest-api-content-id-copy-post

2 answers

0 votes
Manoj Gangwar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 14, 2024

Hi @Michael Winters Welcome to the community! 

Please follow the below steps:

Export (back up) multiple pages to XML

If you want to export a space as an XML, Confluence can create a zipped archive of the XML.

 

XML space exports can be used to import your space content into another Confluence space running the same or newer version of Confluence.

 

To export pages to XML:

 

Go to the space and select Space tools > Content Tools from the bottom of the sidebar

Select Back up – an XML export contains every page, blog posts, comment, and attachment in the space, but excludes blog posts.

Under Save to restore directory, you can:

Give your XML export a file name (optional)

Select Save permanently if you want to keep your file on the server, otherwise, all space backups are only temporarily saved to your <home-directory>/restore/space, where <home-directory> is shared-home-directory for Confluence Data Center. You'll need access to the server to retrieve the file this way.

Select Back up

You can download the exported XML backup by selecting the file name when the export process has finished.

 

Ref- https://confluence.atlassian.com/doc/export-content-to-word-pdf-html-and-xml-139475.html

Michael Winters November 15, 2024

Thanks for the note! can you run me through uploading specific pages from this xml to my new confluence site?

 

0 votes
Kristian Klima
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 14, 2024

Hi @Michael Winters and welcome to the Community.

I can offer a non-programmatic solution that does the same (I've done it multiple times on Server).

You can customize an XML space export to specific sections/pages of the tree and import it to the Target site.

(If space keys are an issue, you can create a new space on the Source site, copy the specific pages, and export/import that in-between space for the same result).

Michael Winters November 14, 2024

Thanks for the response! I considered this but I don't have an option export from the target space as XML:
Screenshot 2024-11-14 at 11.59.03 AM.png
Any idea why this is? I am an admin. 

Kristian Klima
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 14, 2024

Not really, but it might have been moved under Migration... It's a wild guess but that option was definitely there when I last used the server as an admin.

 

https://community.atlassian.com/t5/Confluence-questions/Move-Single-Confluence-Space-Server-to-Cloud/qaq-p/922334

https://support.atlassian.com/migration/docs/use-the-confluence-cloud-migration-assistant-to-migrate/

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events