Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single backup #1439

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

r3dh3ck
Copy link
Contributor

@r3dh3ck r3dh3ck commented Jul 26, 2024

Resolves #745

Copy link
Member

@alexbakker alexbakker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up!

I only took a cursory look at the code, because I think we first need to make some adjustments to the flow here. Instead of introducing a new "Single backup" toggle, repurpose the "Number of versions to keep" option as a "Versioning strategy" setting as proposed here: #745 (comment). "None" implies a single file and "Keep x versions" implies a directory. Prompt the user to make a choice when first enabling automatic backups. If the user changes the versioning strategy later from single file to directory or the other way around, force the user to select a new location. "Directory for backup files" becomes "Backup location", etc.

We also need some kind of warning (with the usual "I understand the risk" checkbox) when a user tries to opt for a single file backup. Something like: "The selected versioning strategy is not reliable and not recommended. A single backup failure could lead to losing your only backup."

@@ -562,6 +567,25 @@ public Set<UUID> getGroupFilter() {
}
}

@Nullable
public Uri getBackupFileLocation() {
String value = _prefs.getString("pref_backup_file_location", null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need a separate preference for this. We should be able to detect whether we're dealing with a directory or a single file based on the URI:

  • content://com.android.externalstorage.documents/tree
  • content://com.android.externalstorage.documents/document

@r3dh3ck
Copy link
Contributor Author

r3dh3ck commented Oct 23, 2024

@alexbakker
Copy link
Member

Thanks for the update! Looks good. I'll take a closer look soon. Hopefully later this week.

<string name="pref_backups_versioning_strategy_keep_x_versions">Keep x versions</string>
<string name="pref_backups_versioning_strategy_single_backup">Single backup</string>
<string name="pref_backups_versioning_strategy_single_backup_warning">The selected versioning strategy is not reliable and not recommended. A single backup failure could lead to losing your only backup.</string>
<string name="pref_backups_versioning_strategy_dialog_title">Select your desired versioning strategy</string>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest: "Select a versioning strategy"

@@ -221,6 +244,15 @@ private CharSequence getBackupStatusMessage(@Nullable Preferences.BackupResult r
return spannable;
}

private void createBackupFile() {
String fileName = VaultBackupManager.FILENAME_PREFIX +".json";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the following below the definition of VaultBackupManager.FILENAME_PREFIX and refer to it here:

public static final String FILENAME_SINGLE = String.format("%s.json", FILENAME_PREFIX);

}
ContentResolver resolver = _context.getContentResolver();
try (FileInputStream inStream = new FileInputStream(tempFile)) {
OutputStream outStream = resolver.openOutputStream(fileUri, "wt");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include the opened OutputStream in the try-with-resources statement above.

@@ -51,10 +53,10 @@ public VaultBackupManager(Context context, AuditLogRepository auditLogRepository
_auditLogRepository = auditLogRepository;
}

public void scheduleBackup(File tempFile, Uri dirUri, int versionsToKeep) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep passing these settings in instead of reading from Preferences here. There's a potential race where the preferences can change while the backup is running.

} else if (strategy == BackupsVersioningStrategy.MULTIPLE_BACKUPS) {
int versionsToKeep = _prefs.getBackupsVersionCount();
createBackup(tempFile, uri, versionsToKeep);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw a RuntimeException if the strategy is anything other than SINGLE_BACKUP or MULTIPLE_BACKUPS.

_versioningStrategyPreference.setOnPreferenceClickListener(preference -> {
BackupsVersioningStrategy currentStrategy = _prefs.getBackupVersioningStrategy();
Dialogs.showBackupsVersioningStrategy(requireContext(), currentStrategy, strategy -> {
if (strategy == BackupsVersioningStrategy.MULTIPLE_BACKUPS) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make sure the user only has to select a location if the versioning strategy was actually changed.

Comment on lines +607 to +620
String path = uri.getPath();
if (path == null) {
return BackupsVersioningStrategy.UNDEFINED;
}
String[] pathArray = path.split("/");
if (pathArray.length < 2) {
return BackupsVersioningStrategy.UNDEFINED;
}
String type = pathArray[1];
if (type.equals("tree")) {
return BackupsVersioningStrategy.MULTIPLE_BACKUPS;
} else if (type.equals("document")) {
return BackupsVersioningStrategy.SINGLE_BACKUP;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a nicer way to do this check: DocumentsContractCompat.isTreeUri

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option for single undated backup file
2 participants