-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadow_backup.R
253 lines (191 loc) · 5.11 KB
/
shadow_backup.R
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
message(Sys.time(), "\n\n")
x <- na.omit(stringr::str_match(commandArgs(), "--file=(.+)")[,2])
if (length(x)) setwd(dirname(x))
decolonize <- function(s) {
s <- stringr::str_replace_all(s, "[[:punct:]]", "_")
stringr::str_replace_all(s, "[[:space:]]", "__")
}
suppressPackageStartupMessages(
library(loggit)
)
set_logfile(
glue::glue(
"{getwd()}/log/shadow_backup_{decolonize(Sys.time())}.log"
)
)
set_timestamp_format("%Y-%m-%dT%H:%M:%OS4%z")
message("Execution start")
real_time_rq <- httr::RETRY("GET", "example.com")
real_time <- httr::parse_http_date(httr::headers(real_time_rq)$date)
off <- Sys.time() - real_time
loggit(
"INFO",
glue::glue("Offset of this server and real time is {format(off)}"),
offset_s = as.double(off, units = "secs")
)
source("secret.R")
source("utils/initializers.R")
source("utils/sql_constructors.R")
suppressPackageStartupMessages({
library(dplyr)
library(DBI)
})
repos <- "../shadow-splits"
loggit(
"INFO",
"Repo status",
output = capture.output(git2r::status(repos)) %>%
sanitize_ndjson()
)
git2r::config(
repo = git2r::repository(repos),
user.name = "shadow_db_backup_bot",
user.email = "brianwdavis@gmail.com"
)
dir_create_conditional <- function(path) {
if (!dir.exists(path)) {
dir.create(path)
}
}
etl_dump <- function(db_name, tbl_name) {
out <- glue::glue("../shadow-splits/{db_name}_{tbl_name}.sql")
system2(
"sqlite3",
args = c(
glue::glue("./db/{db_name}.db"),
glue::glue("'.dump {tbl_name}'")
),
stdout = out
)
info <- file.info(out)
# age <- as.double(
# Sys.time() - info$mtime,
# units = "secs"
# )
#
# if (age > 10 || is.na(age) || is.null(age)) {
# warning("File `", basename(out), "` was not written during this call.")
# }
#tibble(table = tbl_name, size = info$size, last_stored = info$mtime)
loggit(
"INFO", glue::glue("Dump"),
db = db_name, file = tbl_name, size = info$size, last_stored = info$mtime
)
}
etl_split_dumps <- function(db_name, tbl_name) {
dir_create_conditional(
glue::glue("../shadow-splits/{db_name}/{tbl_name}_sql")
)
dump_src <- glue::glue("../shadow-splits/{db_name}_{tbl_name}.sql")
dump_trg <- glue::glue("../shadow-splits/{db_name}/{tbl_name}_sql/sql_")
system2("split", args = c("-l 1000", "-a 3", dump_src, dump_trg))
num_splits <- length(list.files(dirname(dump_trg)))
loggit(
"INFO", "Split",
db = db_name, file = tbl_name, number_of_files = num_splits
)
}
con_sh_f <- etl_connect_shadow("forms")
DBI::dbListTables(con_sh_f) %>%
purrr::walk(~etl_dump("forms", .x))
message("Forms dump completed")
DBI::dbListTables(con_sh_f) %>%
purrr::walk(~etl_split_dumps("forms", .x))
message("Forms dumps split")
dbDisconnect(con_sh_f)
con_sh_s <- etl_connect_shadow("sensors")
DBI::dbListTables(con_sh_s) %>%
purrr::walk(~etl_dump("sensors", .x))
message("Sensors dump completed")
DBI::dbListTables(con_sh_s) %>%
purrr::walk(~etl_split_dumps("sensors", .x))
message("Sensors dumps split")
dbDisconnect(con_sh_s)
loggit(
"INFO",
"Repo status",
output = capture.output(git2r::status(repos)) %>%
sanitize_ndjson()
)
git2r::add(
repo = repos,
path = "*"
)
message("`git add`ed all files")
s <- git2r::status(repos)
s
loggit(
"INFO",
"Repo status",
output = capture.output(s) %>%
sanitize_ndjson()
)
l <- s[c("staged", "unstaged")] %>%
purrr::map_int(length)
sess <- sessionInfo()
sys <- Sys.info()
if (sum(l)) {
cmt <- capture.output(
git2r::commit(
repo = repos,
message = glue::glue(
"Automated backup at {Sys.time()}\n\n",
R.version.string,
"\n{sess$platform}\n{sess$running}",
"\n{sys['nodename']}\n{sys['user']}"
),
all = TRUE
)
) %>% sanitize_ndjson()
loggit(
"INFO",
"Repo commit",
output = cmt
)
} else {
message("Nothing to commit!")
}
# git2r::remote_add(
# repo = repos,
# name = "shadow-splits-remote",
# url = "https://github.com/precision-sustainable-ag/shadow-splits.git"
# )
if (sum(l)) {
git2r::push(
object = repos,
name = "shadow-splits-remote",
refspec = "refs/heads/master",
credentials = git2r::cred_user_pass(
username = "brianwdavis",
password = gh_token
)
)
message("Pushed to GH remote")
} else {
message("Nothing to push!")
}
message("Execution end")
set_logfile(logfile = NULL, confirm = TRUE)
# Generate README for shadow-backup repo ----
#
# writeLines(
# c(
# "# Structure",
# "Each file is a dump generated by calling: ", "",
# " - `sqlite3 .dump {table_name} > {db_name}_{table_name}.sql`",
# " - `split -l 1000 -a 3 {db_name}_{table_name}.sql {db_name}/{table_name}_sql/sql_`,
# "",
# "To reconstruct, use:", "",
# " - `cat {db_name}/{table_name}_sql/sql_* > {db_name}_{table_name}.sql`,
# " - `sqlite3 new_db.db ".read {db_name}_{table_name}.sql"`,
# "",
# "# Origin",
# "These files are automatically generated by the script at:", "",
# "`<link to ETL repo coming...>`"
# ),
# file.path(repos, "README.md")
# )
# git2r::add(
# repos,
# "README.md"
# )