backup.ts 942 B

1234567891011121314151617181920212223242526272829303132
  1. import * as FileSystem from 'expo-file-system/legacy';
  2. const DB_PATH = `${FileSystem.documentDirectory}SQLite/WatermelonDB.db`;
  3. const BACKUP_PATH = `${FileSystem.documentDirectory}backups/backup.db`;
  4. export async function createLocalBackup() {
  5. try {
  6. await FileSystem.makeDirectoryAsync(`${FileSystem.documentDirectory}backups/`, {
  7. intermediates: true
  8. });
  9. await FileSystem.copyAsync({ from: DB_PATH, to: BACKUP_PATH });
  10. } catch (e) {
  11. console.error('Backup failed', e);
  12. }
  13. }
  14. export async function hasLocalBackup() {
  15. const info = await FileSystem.getInfoAsync(BACKUP_PATH);
  16. return info.exists;
  17. }
  18. export async function restoreFromBackup() {
  19. try {
  20. const info = await FileSystem.getInfoAsync(BACKUP_PATH);
  21. if (!info.exists) return false;
  22. await FileSystem.copyAsync({ from: BACKUP_PATH, to: DB_PATH });
  23. return true;
  24. } catch (e) {
  25. console.error('Restore failed', e);
  26. return false;
  27. }
  28. }