-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
82 lines (62 loc) · 2.73 KB
/
Program.cs
File metadata and controls
82 lines (62 loc) · 2.73 KB
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
// See https://aka.ms/new-console-template for more information
using HackMdBackup;
bool testMode = Environment.GetEnvironmentVariable("TEST_MODE") == "1";
string GetConfig(string envName, string defaultValue = "",bool isOptional = false)
{
string? envVar = Environment.GetEnvironmentVariable(envName);
if(!isOptional && string.IsNullOrEmpty(envVar) && string.IsNullOrEmpty(defaultValue))
{
throw new Exception($"Missing configuration env: {envName}");
}
return string.IsNullOrEmpty(envVar) ? defaultValue : envVar;
}
var pingHttp = new HttpClient();
string hcUrl = GetConfig("HEALTHCHECK_URL", isOptional: true);
if (!String.IsNullOrEmpty(hcUrl))
{
await pingHttp.GetAsync($"{hcUrl}/start");
}
string gpgPublicKey = GetConfig("GPG_PUBKEY_FILE", "foo", isOptional: testMode);
string tarFile = GetConfig("BACKUP_TAR_PATH","/tmp/backup.tar.gz");
string webDriverEndpoint = GetConfig("WEBDRIVER_URL", "http://localhost:4444");
string s3Host = GetConfig("S3_HOST", isOptional: testMode);
string s3AccessKEy = GetConfig("S3_ACCESS_KEY", isOptional: testMode);
string s3SecretKey = GetConfig("S3_SECRET_KEY", isOptional: testMode);
string s3Bucket = GetConfig("S3_BUCKET", isOptional: testMode);
string cookieCsrf = GetConfig("COOKIE_CSRF", isOptional: true);
string cookieSessionId = GetConfig("COOKIE_SESSION_ID", isOptional: true);
string cookieUserId = GetConfig("COOKIE_USER_ID", isOptional: true);
HackMdApi api = new HackMdApi(webDriverEndpoint, testMode: testMode)
{
GitHubUsername = GetConfig("GITHUB_USERNAME"),
GitHubPassword = GetConfig("GITHUB_PASSWORD"),
GitHub2FaSeed = GetConfig("GITHUB_OTP_SEED"),
BackupPath = GetConfig("BACKUP_PATH","."),
CredetialCachePath = GetConfig("CREDENTIAL_CACHE_PATH","."),
HackMdHost = GetConfig("HACKMD_HOST"),
};
if (!string.IsNullOrEmpty(cookieSessionId))
{
api.SetCookieExternal(cookieCsrf, cookieSessionId, cookieUserId);
}
Console.WriteLine("==> Pulling notes");
await api.GetAllNotes();
if (testMode)
{
Console.WriteLine("==> Test mode: skipping compress/encrypt/upload");
Console.WriteLine($"==> Backup files saved to: {api.BackupPath}");
Console.WriteLine("done.");
return;
}
FileProcessor fp = new FileProcessor();
Console.WriteLine("==> Compressing...");
fp.CreateTarGz(tarFile, api.BackupPath);
Console.WriteLine("==> Encrypting...");
string datetime = DateTime.UtcNow.ToString("s").Replace(':', '-');
string encryptedFile = $"{tarFile}.{datetime}.gpg";
fp.EncryptFile(tarFile, gpgPublicKey, encryptedFile, false, true);
Console.WriteLine("==> Send to S3 storage");
var s3 = new S3Uploader(s3Host, s3AccessKEy, s3SecretKey, s3Bucket);
await s3.UploadFileAsync(encryptedFile);
await pingHttp.GetAsync($"{hcUrl}");
Console.WriteLine("done.");