From 13d51c7bf855ce89a56837078b292c1a5bd774c7 Mon Sep 17 00:00:00 2001 From: Julian van de Groep Date: Mon, 11 Feb 2019 14:42:09 +0100 Subject: [PATCH] Removed Google Drive backup storage support. I'm satistfied with how the SFTP method has been working, and it also means the backups aren't stored by Google as a MASSIVE plus. --- BackupManager/BackupManager.csproj | 1 - BackupManager/GoogleDatastore.cs | 78 ------------------------- BackupManager/Program.cs | 93 +----------------------------- 3 files changed, 1 insertion(+), 171 deletions(-) delete mode 100644 BackupManager/GoogleDatastore.cs diff --git a/BackupManager/BackupManager.csproj b/BackupManager/BackupManager.csproj index 81ce1d8..41a1b6f 100644 --- a/BackupManager/BackupManager.csproj +++ b/BackupManager/BackupManager.csproj @@ -6,7 +6,6 @@ - diff --git a/BackupManager/GoogleDatastore.cs b/BackupManager/GoogleDatastore.cs deleted file mode 100644 index 57e7711..0000000 --- a/BackupManager/GoogleDatastore.cs +++ /dev/null @@ -1,78 +0,0 @@ -using Google.Apis.Auth.OAuth2.Responses; -using Google.Apis.Json; -using Google.Apis.Util.Store; -using Newtonsoft.Json.Linq; -using System; -using System.Text; -using System.Threading.Tasks; - -namespace BackupManager -{ - public sealed class GoogleDatastore : IDataStore - { - public Config Config { get; set; } - - public GoogleDatastore(Config config) - { - Config = config; - } - - public Task GetAsync(string key) - { - TaskCompletionSource tcs = new TaskCompletionSource(); - - TokenResponse tr = new TokenResponse { - AccessToken = Config.GoogleAccessToken, - TokenType = Config.GoogleTokenType, - ExpiresInSeconds = Config.GoogleTokenExpires, - RefreshToken = Config.GoogleRefreshToken, - IssuedUtc = Config.GoogleTokenIssued == null - ? new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) - : DateTime.Parse(Config.GoogleTokenIssued) - }; - - tcs.SetResult((T)(object)tr); // fuck it - return tcs.Task; - } - - public Task DeleteAsync(string key) - => ClearAsync(); - - public Task ClearAsync() - { - Config.GoogleAccessToken = null; - Config.GoogleTokenType = null; - Config.GoogleTokenExpires = null; - Config.GoogleTokenIssued = null; - Config.GoogleTokenIssued = null; - return Task.Delay(0); - } - - public Task StoreAsync(string key, T value) - { - JObject json = JObject.Parse(NewtonsoftJsonSerializer.Instance.Serialize(value)); - - JToken accessToken = json.SelectToken(@"access_token"); - if (accessToken != null) - Config.GoogleAccessToken = (string)accessToken; - - JToken tokenType = json.SelectToken(@"token_type"); - if (tokenType != null) - Config.GoogleTokenType = (string)tokenType; - - JToken expiresIn = json.SelectToken(@"expires_in"); - if (expiresIn != null) - Config.GoogleTokenExpires = (long?)expiresIn; - - JToken refreshToken = json.SelectToken(@"refresh_token"); - if (refreshToken != null) - Config.GoogleRefreshToken = (string)refreshToken; - - JToken tokenIssued = json.SelectToken(@"Issued"); - if (refreshToken != null) - Config.GoogleTokenIssued = (string)tokenIssued; - - return Task.Delay(0); - } - } -} diff --git a/BackupManager/Program.cs b/BackupManager/Program.cs index a376bf3..56de438 100644 --- a/BackupManager/Program.cs +++ b/BackupManager/Program.cs @@ -1,10 +1,6 @@ -using Google.Apis.Auth.OAuth2; -using Google.Apis.Drive.v3; -using Google.Apis.Services; -using Renci.SshNet; +using Renci.SshNet; using Renci.SshNet.Common; using System; -using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.IO.Compression; @@ -17,7 +13,6 @@ using System.Text; using System.Threading; using System.Xml; using System.Xml.Serialization; -using GFile = Google.Apis.Drive.v3.Data.File; namespace BackupManager { @@ -25,8 +20,6 @@ namespace BackupManager { public readonly static Stopwatch sw = new Stopwatch(); - private const string FOLDER_MIME = @"application/vnd.google-apps.folder"; - private const string CONFIG_NAME = @"FlashiiBackupManager.v1.xml"; public static bool IsWindows @@ -47,7 +40,6 @@ namespace BackupManager CONFIG_NAME ); - private static DriveService DriveService; private static object BackupStorage; private static SftpClient SFTP; @@ -113,22 +105,6 @@ namespace BackupManager switch (Config.StorageMethod) { - case StorageMethod.GoogleDrive: - UserCredential uc = GoogleAuthenticate( - new ClientSecrets - { - ClientId = Config.GoogleClientId, - ClientSecret = Config.GoogleClientSecret, - }, - new[] { - DriveService.Scope.Drive, - DriveService.Scope.DriveFile, - } - ); - - CreateDriveService(uc); - break; - case StorageMethod.Sftp: if (string.IsNullOrWhiteSpace(Config.SftpHost) || string.IsNullOrWhiteSpace(Config.SftpUsername)) { @@ -185,10 +161,6 @@ namespace BackupManager switch (f) { - case GFile fgf: - Log($@"MySQL dump uploaded: {fgf.Name} ({fgf.Id})"); - break; - default: Log($@"MySQL dump uploaded."); break; @@ -216,10 +188,6 @@ namespace BackupManager switch (f) { - case GFile fgf: - Log($@"Misuzu data uploaded: {fgf.Name} ({fgf.Id})"); - break; - default: Log($@"Misuzu data uploaded."); break; @@ -281,18 +249,6 @@ namespace BackupManager switch (BackupStorage) { - case GFile gfile: - FilesResource.CreateMediaUpload request = DriveService.Files.Create(new GFile - { - Name = name, - Parents = new List { - gfile.Id, - }, - }, stream, type); - request.Fields = @"id, name"; - request.Upload(); - return request.ResponseBody; - case string scpName: SFTP.UploadFile(stream, scpName + @"/" + name); break; @@ -413,57 +369,10 @@ namespace BackupManager return output; } - public static UserCredential GoogleAuthenticate(ClientSecrets cs, string[] scopes) - { - Log(@"Authenticating with Google..."); - return GoogleWebAuthorizationBroker.AuthorizeAsync( - cs, - scopes, - @"user", - CancellationToken.None, - new GoogleDatastore(Config), - new PromptCodeReceiver() - ).Result; - } - - public static void CreateDriveService(UserCredential uc) - { - Log(@"Creating Google Drive service..."); - DriveService = new DriveService(new BaseClientService.Initializer() - { - HttpClientInitializer = uc, - ApplicationName = @"Flashii Backup Manager", - }); - } - public static void GetBackupStorage(string name = null) { switch (Config.StorageMethod) { - case StorageMethod.GoogleDrive: - name = name ?? Config.GoogleBackupDirectory; - Log(@"Getting backup folder..."); - FilesResource.ListRequest lr = DriveService.Files.List(); - lr.Q = $@"name = '{name}' and mimeType = '{FOLDER_MIME}'"; - lr.PageSize = 1; - lr.Fields = @"files(id)"; - GFile backupFolder = lr.Execute().Files.FirstOrDefault(); - - if (backupFolder == null) - { - Log(@"Backup folder doesn't exist yet, creating it..."); - FilesResource.CreateRequest dcr = DriveService.Files.Create(new GFile - { - Name = name, - MimeType = FOLDER_MIME, - }); - dcr.Fields = @"id"; - backupFolder = dcr.Execute(); - } - - BackupStorage = backupFolder; - break; - case StorageMethod.Sftp: string directory = (BackupStorage = name ?? Config.SftpBackupDirectoryPath) as string; try