~hrbrmstr/src-mowerplus.R

# src-mowerplus.R -rw-r--r-- 1.1 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
src_mowerplus <- function(backup_id, data_loc = "~/Data", overwrite = TRUE) {

  require(XML, quietly = TRUE, warn.conflicts = FALSE) # to read plist (property list) files
  require(tidyverse, quietly = TRUE, warn.conflicts = FALSE) # for printing and access to sqlite dbs
  
  # root of mobile backup dir for `backup_id`
  mb <- path.expand(file.path("~/Library/Application Support/MobileSync/Backup", backup_id))
  stopifnot(dir.exists(mb))
  
  data_loc <- path.expand(data_loc)
  stopifnot(dir.exists(data_loc))
  
  tf <- tempfile(fileext = ".sqlite")
  on.exit(unlink(tf), add=TRUE)
  
  # path to the extracted sqlite file
  out_db <- file.path(data_loc, "mowtrack.sqlite")

  file.copy(file.path(mb, "Manifest.db"), tf, overwrite = TRUE)
  
  manifest_db <- src_sqlite(tf)
  
  fils <- tbl(manifest_db, "Files")
  
  filter(fils, relativePath == "Library/Application Support/MowTracking.sqlite") %>% 
    pull(fileID) -> mowtrackdb_loc
  
  file.copy(
    file.path(mb, sprintf("%s/%s", substr(mowtrackdb_loc, 1, 2), mowtrackdb_loc)),
    file.path(data_loc, "mowtrack.sqlite"),
    overwrite = overwrite
  )
  
  src_sqlite(out_db)
  
}