Skip to content

Commit dfe5d09

Browse files
Merge pull request #10 from michaelmsonne/dev
v. 1.1.0.0 - fixes and more
2 parents 7bd629a + 5b72541 commit dfe5d09

20 files changed

Lines changed: 564 additions & 86 deletions

CHANGELOG.MD

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
## [1.1.0.0] - 09-03-2024
2+
3+
### Added
4+
- Added a new option to set days to keep of logs (default 30 days)
5+
- Added new argument to download
6+
- repository metadata (.json)
7+
- repository releases data (.json)
8+
- More to come...
9+
- Added more loggnig in the tool
10+
11+
### Changed
12+
- Changed some typos
13+
- Moved some code to classes
14+
- Upgraded some dependencies
15+
- Microsoft.Windows.Compatibility - 8.0.0 > 8.0.2
16+
- McMaster.Extensions.CommandLineUtils - 4.1.0 > 4.1.1
17+
- Changed backup folder name to just the timestamp
18+
19+
### Fixed
20+
- Fixed some bugs with the all branche backup function
21+
122
## [1.0.0] - 05-03-2024
223

324
Initial release of the tool

GithubBackup/Class/ApplicationStatus.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ public static void ApplicationEndBackup(bool isSuccess)
4444
Console.WriteLine("Backup start Time: " + Globals._startTime);
4545
Console.WriteLine("Backup end Time: " + Globals._endTime);
4646
Console.WriteLine("Errors: " + Globals._errors);
47+
Console.WriteLine("Warnings: " + Globals._warnings);
4748

4849
// Log the TimeSpan value to the log file
4950
Message("Backup Run Time: " + Globals._elapsedTime, EventType.Information, 1000);
5051
Message("Backup start Time: " + Globals._startTime, EventType.Information, 1000);
5152
Message("Backup end Time: " + Globals._endTime, EventType.Information, 1000);
5253
Message("Errors: " + Globals._errors, EventType.Information, 1000);
54+
Message("Warnings: " + Globals._warnings, EventType.Information, 1000);
5355

5456
Console.ForegroundColor = isSuccess ? ConsoleColor.Green : ConsoleColor.Red;
5557

@@ -273,7 +275,7 @@ public static void ApplicationEndMessage()
273275
{
274276
// Log end of program
275277
Console.WriteLine($"\nEnd of application - {Globals._appName} v. {Globals._vData}\n");
276-
Message($"End of application - {Globals._appName} v. {Globals._vData}", EventType.Information, 1000);
278+
Message($"End of application - {Globals._appName} v. {Globals._vData}\n", EventType.Information, 1000);
277279
}
278280

279281
private static void SetConsoleColorDefaultAndError(bool isSuccess)

GithubBackup/Class/Backups.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public static void DaysToKeepBackups(string outBackupDir, int daysToKeep)
1919
Console.WriteLine($"Set to keep {daysToKeep} number of backups (day(s)) in backup folder: '{outBackupDir}'");
2020
Console.ResetColor();
2121

22+
// Log
23+
Message("Checking for backups needed to be deleted form backup folder: '" + outBackupDir + "'...", EventType.Information, 1000);
24+
2225
// Loop folders
2326
foreach (string dir in Directory.GetDirectories(outBackupDir))
2427
{
@@ -70,14 +73,23 @@ public static void DaysToKeepBackups(string outBackupDir, int daysToKeep)
7073
}
7174
}
7275

73-
// If no backups to delete
74-
if (backupsToDelete == false)
76+
switch (backupsToDelete)
7577
{
76-
// Log
77-
Message("No old backups needed to be deleted form backup folder: '" + outBackupDir + "'", EventType.Information, 1000);
78-
Console.ForegroundColor = ConsoleColor.Green;
79-
Console.WriteLine("No old backups needed to be deleted form backup folder: '" + outBackupDir + "'\n");
80-
Console.ResetColor();
78+
// If no backups to delete
79+
case false:
80+
// Log
81+
Message("> Done - No old backups needed to be deleted form backup folder: '" + outBackupDir + "'", EventType.Information, 1000);
82+
Console.ForegroundColor = ConsoleColor.Green;
83+
Console.WriteLine("No old backups needed to be deleted form backup folder: '" + outBackupDir + "'\n");
84+
Console.ResetColor();
85+
break;
86+
case true:
87+
// Log
88+
Message("> Done - Old backups deleted from backup folder: '" + outBackupDir + "'", EventType.Information, 1000);
89+
Console.ForegroundColor = ConsoleColor.Green;
90+
Console.WriteLine("Old backups deleted from backup folder: '" + outBackupDir + "'\n");
91+
Console.ResetColor();
92+
break;
8193
}
8294
}
8395

@@ -148,7 +160,7 @@ public static void DaysToKeepBackupsDefault(string outBackupDir)
148160
}
149161
}
150162

151-
public static void CountCurrentNumersOfBackup(string outBackupDir)
163+
public static void CountCurrentNumbersOfBackup(string outBackupDir)
152164
{
153165
//Count backups in folder
154166
string searchPattern = "*??-??-????-(??-??)";

GithubBackup/Class/CleanupLog.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
73
using static GithubBackup.Class.FileLogger;
84

95
namespace GithubBackup.Class
106
{
117
internal class CleanupLog
128
{
13-
public static void CleanupLogs()
9+
public static void CleanupLogs(int daysOfLogfilesToKeep)
1410
{
1511
// Cleanup old log files
1612
string[] oldfiles = Directory.GetFiles(Files.LogFilePath);
@@ -27,7 +23,7 @@ public static void CleanupLogs()
2723
FileInfo fi = new FileInfo(file);
2824

2925
// Get all last access time back in time
30-
if (fi.LastAccessTime < DateTime.Now.AddDays(-30))
26+
if (fi.LastAccessTime < DateTime.Now.AddDays(-daysOfLogfilesToKeep))
3127
{
3228
try
3329
{
@@ -72,17 +68,17 @@ public static void CleanupLogs()
7268
if (Globals._oldLogfilesToDelete)
7369
{
7470
// Log
75-
Message($"There was {Globals._oldLogFilesToDeleteCount} old log files to delete (-30 days)", EventType.Information, 1000);
71+
Message($"There was {Globals._oldLogFilesToDeleteCount} old log files to delete (-{Globals._daysToKeepLogFilesOption})", EventType.Information, 1000);
7672
Console.ForegroundColor = ConsoleColor.Green;
77-
Console.WriteLine($"There was {Globals._oldLogFilesToDeleteCount} old log files to delete (-30 days)");
73+
Console.WriteLine($"There was {Globals._oldLogFilesToDeleteCount} old log files to delete (-{Globals._daysToKeepLogFilesOption})");
7874
Console.ResetColor();
7975
}
8076
else
8177
{
8278
// Log
83-
Message("No old log files to delete (-30 days)", EventType.Information, 1000);
79+
Message($"No old log files to delete (-{Globals._daysToKeepLogFilesOption}) day(s)", EventType.Information, 1000);
8480
Console.ForegroundColor = ConsoleColor.Green;
85-
Console.WriteLine("No old log files to delete (-30 days)");
81+
Console.WriteLine($"No old log files to delete (-{Globals._daysToKeepLogFilesOption}) day(s)");
8682
Console.ResetColor();
8783
}
8884
}

GithubBackup/Class/Files.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public static string LogFilePath
1313
get
1414
{
1515
// Root folder for log files
16-
var logfilePathvar = ProgramDataFilePath + @"\Log";
17-
return logfilePathvar;
16+
var logfilePath = ProgramDataFilePath + @"\Log";
17+
return logfilePath;
1818
}
1919
}
2020

@@ -24,8 +24,7 @@ public static string ProgramDataFilePath
2424
{
2525
// Root path for program data
2626
var currentDirectory = Directory.GetCurrentDirectory();
27-
var programDataFilePathvar = currentDirectory;
28-
return programDataFilePathvar;
27+
return currentDirectory;
2928
}
3029
}
3130

@@ -34,11 +33,16 @@ public static string TokenFilePath
3433
get
3534
{
3635
// Root folder for log files
37-
var tokenFilePathvar = ProgramDataFilePath + @"\token.bin";
38-
return tokenFilePathvar;
36+
var tokenFilePath = ProgramDataFilePath + @"\token.bin";
37+
return tokenFilePath;
3938
}
4039
}
4140

41+
/// <summary>
42+
/// Not in use from here - TODO: Cleanup
43+
/// </summary>
44+
/// <param name="outDir"></param>
45+
4246
// If args is set to delete original downloaded .zip and .json files
4347
// Get output folder from backup with date for folder to backup to
4448
public static void DeleteZipAndJson(string outDir)
@@ -145,4 +149,4 @@ public static void DeleteFileAndWait(string filepath, string outDir, int timeout
145149
}
146150
}
147151
}
148-
}
152+
}

GithubBackup/Class/Folders.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public static void DeleteDirectory(string path)
7979
}
8080
}
8181

82-
8382
public static int GetSubfolderCountForBranchFolders(string rootFolderPath, int depth)
8483
{
8584
// This method counts the number of subfolders in a folder, at a given depth used for branch folders

GithubBackup/Class/Globals.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Net.Mail;
4-
using LibGit2Sharp;
54

65
namespace GithubBackup.Class
76
{
@@ -25,6 +24,8 @@ public class Globals
2524
public static bool _allReposNotForks; // backup all repos from Github if true - default is false
2625
public static bool _allReposNotForksAndIsOwner; // backup all repos from Github if true - default is true as most common use case is to backup own repos and not forks from others repos
2726
public static bool _excludeBranchDependabot; // exclude branches with "dependabot" in it from backup if true - default is true
27+
public static bool _backupRepoMetadata; // backup metadata from repo if true - default is false
28+
public static bool _backupReleasedata; // backup release from repo if true - default is false
2829
//public static string _alloriginalBranches; // backup all original branches from repo
2930
//public static IReadOnlyList<Branch> _alloriginalBranches;
3031

@@ -37,6 +38,7 @@ public class Globals
3738
public static int _daysToKeepBackup; // number of days to keep backup in backup folder before deleting it - default is 30 days if not set
3839
public static int _currentBackupsInBackupFolderCount;
3940
public static int _errors; // count errors
41+
public static int _warnings; // count warnings
4042
public static int _repoCount; // count repos from Github
4143
public static int _repoBackupPerformedCount; // count repo items from Github
4244
public static int _repoBackupPerformedBranchCount; // count repo (branches) items from Github
@@ -46,12 +48,14 @@ public class Globals
4648
public static bool _isBackupOk; // check if backup is ok or not state
4749
public static string _repoCountStatusText; // text to display in email report
4850
public static string _isDaysToKeepNotDefaultStatusText; // text to display in email report if days to keep backup is not default value (30 days)
51+
public static string _isdaysToKeepLogFilesOptionDefaultStatusText; // text to display in email report if days to keep log files is not default value (30 days)
4952
public static string _totalBackupsIsDeletedStatusText; // text to display in email report if backups is deleted
5053

5154
// Set Global variables for cleanup
5255
public static int _totalBackupsIsDeleted; // count of total backups deleted
5356
public static int _oldLogFilesToDeleteCount; // count of old log files deleted
5457
public static bool _oldLogfilesToDelete; // delete old log files if true - default is false and function is not used
58+
public static int _daysToKeepLogFilesOption; // number of days to keep log files in log folder before deleting it - default is 30 days if not set
5559

5660
// Set Global variables for email
5761
public static string _mailto; // email address to send email to
@@ -73,4 +77,4 @@ public class Globals
7377
public static List<string> repocountelements = new List<string>(); // list of repos to backup from Github - used for email report
7478
public static List<string> repoitemscountelements = new List<string>(); // list of repo items to backup from Github - used for email report
7579
}
76-
}
80+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Newtonsoft.Json;
2+
using Octokit;
3+
using System;
4+
using System.IO;
5+
using System.Linq;
6+
using GithubBackup.Core;
7+
using static GithubBackup.Class.FileLogger;
8+
9+
namespace GithubBackup.Class
10+
{
11+
internal class MetadataJsonDownloader
12+
{
13+
public static void SaveMetadataFortheRepository(string repoDestinationBackupMetadataFilePath, GitHubClient client, Repository repo)
14+
{
15+
try
16+
{
17+
// Check if the repository has any branches
18+
var branchNames = BackupService.GetBranchesForRepository(client, repo); // Replace with your method to get branches
19+
20+
Console.WriteLine($"Processing metadata for repository '{repo.FullName}' for backup up to: '{repoDestinationBackupMetadataFilePath}'");
21+
Message($"Processing metadata for repository '{repo.FullName}' for backup up to: '{repoDestinationBackupMetadataFilePath}'", EventType.Information, 1000);
22+
23+
// Save metadata for the repository if it has any branches
24+
if (branchNames.Any())
25+
{
26+
// Save metadata for the repository
27+
repoDestinationBackupMetadataFilePath = Path.Combine(repoDestinationBackupMetadataFilePath, "repository_metadata.json");
28+
File.WriteAllText(repoDestinationBackupMetadataFilePath, JsonConvert.SerializeObject(repo, Formatting.Indented));
29+
30+
// Log the result
31+
Console.WriteLine($"Done processing metadata for repository '{repo.FullName}' for backup up to: '{repoDestinationBackupMetadataFilePath}'");
32+
Message($"> Done processing metadata for repository '{repo.FullName}' for backup up to: '{repoDestinationBackupMetadataFilePath}'", EventType.Information, 1000);
33+
}
34+
// Skip further processing if the repository is empty
35+
else
36+
{
37+
Console.WriteLine($"Skipped saving metadata for empty repository '{repo.FullName}' - if there was data to backup, repository metadata has been saved to: '{repoDestinationBackupMetadataFilePath}'");
38+
Message($"! Skipped saving metadata for empty repository '{repo.FullName}' - if there was data to backup, repository metadata has been saved to: '{repoDestinationBackupMetadataFilePath}'", EventType.Warning, 1001);
39+
return; // Skip further processing if the repository is empty
40+
}
41+
}
42+
catch (Exception ex)
43+
{
44+
// Handle the exception (log or display an error message)
45+
Console.WriteLine($"Error saving metadata for repository '{repo.FullName}': {ex.Message}");
46+
Message($"Error saving metadata for repository '{repo.FullName}': {ex.Message}", EventType.Error, 1001);
47+
48+
// Increment the _errors integer
49+
Globals._errors++; // Increment the _errors integer
50+
}
51+
}
52+
}
53+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Newtonsoft.Json;
2+
using Octokit;
3+
using System;
4+
using System.IO;
5+
using System.Linq;
6+
using static GithubBackup.Class.FileLogger;
7+
8+
namespace GithubBackup.Class
9+
{
10+
internal class ReleaseJsonDownloader
11+
{
12+
public static void SaveReleaseDataFortheRepository(string owner, Repository repo, GitHubClient client, string destinationPath)
13+
{
14+
try
15+
{
16+
// Get all releases for the repository
17+
var releases = client.Repository.Release.GetAll(owner, repo.Name).Result;
18+
19+
// Check if there are any releases
20+
if (releases.Any())
21+
{
22+
// Save metadata for the repository
23+
destinationPath = Path.Combine(destinationPath, "repository_releases.json");
24+
25+
// Serialize releases to JSON and save directly to the file
26+
File.WriteAllText(destinationPath, JsonConvert.SerializeObject(releases, Formatting.Indented));
27+
28+
// Log a message
29+
Console.WriteLine($"Releases information saved to: '{destinationPath}' for repository '{owner}/{repo.Name}'");
30+
Message($"Releases information saved to: '{destinationPath}' for repository '{owner}/{repo.Name}'", EventType.Information, 1000);
31+
}
32+
// If no releases are found
33+
else
34+
{
35+
// Log a message and skip further processing
36+
Console.WriteLine($"Skipped - no releases found for repository '{owner}/{repo.Name}'.");
37+
Message($"! Skipped - no releases found for repository '{owner}/{repo.Name}'.", EventType.Information, 1000);
38+
return; // Skip further processing if the repository is empty
39+
}
40+
}
41+
catch (Exception ex)
42+
{
43+
// Handle the exception (log or display an error message)
44+
Console.WriteLine($"Error downloading releases for repository '{owner}/{repo.Name}' - Error: {ex.Message}");
45+
Message($"Error downloading releases for repository '{owner}/{repo.Name}' - Error: {ex.Message}'", EventType.Error, 1001);
46+
47+
// Increment the _errors integer
48+
Globals._errors++; // Increment the _errors integer
49+
}
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)