Skip to content

Commit

Permalink
Fail if any of the exports failed (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
jckleiner authored Dec 23, 2023
1 parent 672fecb commit f90e3b5
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Create a `.env` file with the following properties ([How do I find all these val
PCLOUD_API_HOST=
PCLOUD_FOLDER_ID=

# if you don't use the Docker image and want to download the backup to a different folder
# DOWNLOADS_DIRECTORY_PATH=<absolute-folder-path>

### Backup to Cloud With Docker

Once you created your `.env` file, you can run the following command to start your backup:
Expand Down
3 changes: 2 additions & 1 deletion documentation/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Do the following steps to set up the refresh token flow.

4. Click on continue and allow the app to access your files
5. Copy the authorization code (referred to as `AUTH_CODE` in the following steps) shown in the following screen
6. Send the following HTTP POST request with the actual values replacing the placeholders
6. Send the following HTTP POST request with the actual values replacing the placeholders.
Note that you need to encode the string `<app-key>:<app-secret>` as a BASE64 string.

```
curl --request POST \
Expand Down
64 changes: 56 additions & 8 deletions src/main/java/com/greydev/notionbackup/NotionBackup.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.io.File;
import java.io.IOException;
import java.rmi.UnexpectedException;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;

import com.greydev.notionbackup.cloudstorage.pcloud.PCloudApiClientFactory;
import com.greydev.notionbackup.cloudstorage.pcloud.PCloudClient;
Expand Down Expand Up @@ -66,12 +68,42 @@ public static void main(String[] args) {
// use a local file to skip the notion export step
// final File exportedFile = new File("notion-export-markdown_2022-01-18_23-17-13.zip");

CompletableFuture<Void> futureGoogleDrive = CompletableFuture.runAsync(() -> NotionBackup.startGoogleDriveBackup(exportedFile));
CompletableFuture<Void> futureDropbox = CompletableFuture.runAsync(() -> NotionBackup.startDropboxBackup(exportedFile));
CompletableFuture<Void> futureNextcloud = CompletableFuture.runAsync(() -> NotionBackup.startNextcloudBackup(exportedFile));
CompletableFuture<Void> futurePCloud = CompletableFuture.runAsync(() -> NotionBackup.startPCloudBackup(exportedFile));
AtomicBoolean hasErrorOccurred = new AtomicBoolean(false);

CompletableFuture<Void> futureGoogleDrive = CompletableFuture
.runAsync(() -> NotionBackup.startGoogleDriveBackup(exportedFile))
.handle((result, ex) -> {
if (ex != null) hasErrorOccurred.set(true);
return null;
});

CompletableFuture<Void> futureDropbox = CompletableFuture
.runAsync(() -> NotionBackup.startDropboxBackup(exportedFile))
.handle((result, ex) -> {
if (ex != null) hasErrorOccurred.set(true);
return null;
});

CompletableFuture<Void> futureNextcloud = CompletableFuture
.runAsync(() -> NotionBackup.startNextcloudBackup(exportedFile))
.handle((result, ex) -> {
if (ex != null) hasErrorOccurred.set(true);
return null;
});

CompletableFuture<Void> futurePCloud = CompletableFuture
.runAsync(() -> NotionBackup.startPCloudBackup(exportedFile))
.handle((result, ex) -> {
if (ex != null) hasErrorOccurred.set(true);
return null;
});

CompletableFuture.allOf(futureGoogleDrive, futureDropbox, futureNextcloud, futurePCloud).join();

if (hasErrorOccurred.get()) {
log.error("Not all backups were completed successfully. See the logs above to get more information about the errors.");
System.exit(1);
}
}


Expand All @@ -92,7 +124,11 @@ public static void startGoogleDriveBackup(File fileToUpload) {
return;
}
GoogleDriveClient GoogleDriveClient = new GoogleDriveClient(googleServiceOptional.get(), googleDriveRootFolderId);
GoogleDriveClient.upload(fileToUpload);
boolean isSuccess = GoogleDriveClient.upload(fileToUpload);

if (!isSuccess) {
throw new IllegalStateException("Backup was not successful");
}
}


Expand All @@ -109,7 +145,11 @@ public static void startDropboxBackup(File fileToUpload) {
return;
}
DropboxClient dropboxClient = new DropboxClient(dropboxServiceOptional.get());
dropboxClient.upload(fileToUpload);
boolean isSuccess = dropboxClient.upload(fileToUpload);

if (!isSuccess) {
throw new IllegalStateException("Backup was not successful");
}
}

private static Optional<String> getDropboxAccessToken() {
Expand Down Expand Up @@ -153,7 +193,11 @@ public static void startNextcloudBackup(File fileToUpload) {
return;
}

new NextcloudClient(email, password, webdavUrl).upload(fileToUpload);
boolean isSuccess = new NextcloudClient(email, password, webdavUrl).upload(fileToUpload);

if (!isSuccess) {
throw new IllegalStateException("Backup was not successful");
}
}

public static void startPCloudBackup(File fileToUpload) {
Expand Down Expand Up @@ -182,7 +226,11 @@ public static void startPCloudBackup(File fileToUpload) {
}
}
PCloudClient pCloudClient = new PCloudClient(pCloudApiClient.get(), pCloudFolderId);
pCloudClient.upload(fileToUpload);
boolean isSuccess = pCloudClient.upload(fileToUpload);

if (!isSuccess) {
throw new IllegalStateException("Backup was not successful");
}
}

private static Optional<String> extractGoogleServiceAccountSecret() {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/greydev/notionbackup/NotionClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ private Optional<String> triggerExportTask() throws IOException, InterruptedExce
}
*/
if (responseJsonNode.get("taskId") == null) {
log.error("Error name: {}, error message: {}", responseJsonNode.get("name"), responseJsonNode.get("message"));
JsonNode errorName = responseJsonNode.get("name");
log.error("Error name: {}, error message: {}", errorName, responseJsonNode.get("message"));
if (StringUtils.equalsIgnoreCase(errorName.toString(), "UnauthorizedError")) {
log.error("UnauthorizedError: seems like your token is not valid anymore. Try to log in to Notion again and replace you old token.");
}
return Optional.empty();
}

Expand All @@ -201,10 +205,9 @@ private Optional<String> getDownloadLink(String taskId) throws IOException, Inte
.POST(HttpRequest.BodyPublishers.ofString(postBody))
.build();

for (int i = 0; i < 8000; i++) {
for (int i = 0; i < 800; i++) {
HttpResponse<String> response = newClient.send(request, HttpResponse.BodyHandlers.ofString());

// TODO Need to prepare Jackson Document and see how this is handled. I don't wan't this wrapper "Results" class
Results results = objectMapper.readValue(response.body(), Results.class);

if (results.getResults().isEmpty()) {
Expand Down Expand Up @@ -232,6 +235,7 @@ private Optional<String> getDownloadLink(String taskId) throws IOException, Inte
log.info("Notion API workspace export 'state': '{}', Pages exported so far: {}", result.getState(), result.getStatus().getPagesExported());
return Optional.of(result.getStatus().getExportUrl());
}
sleep(6000);
}

log.info("Notion workspace export failed. After waiting 80 minutes, the export status from the Notion API response was still not 'success'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public boolean upload(File fileToUpload) {

if (response.statusCode() == 201) {
log.info("Nextcloud: successfully uploaded '{}'", fileToUpload.getName());
return true;
} else if (response.statusCode() == 204) {
log.info("Nextcloud: file upload response code is {}). " +
"This probably means a file with the same name already exists and it was overwritten/replaced.", response.statusCode());
Expand All @@ -50,7 +51,7 @@ public boolean upload(File fileToUpload) {
}

} catch (IOException | InterruptedException e) {
log.error("Exception: ", e);
log.error("Nextcloud Exception: ", e);
}

return false;
Expand Down

0 comments on commit f90e3b5

Please sign in to comment.