Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x' # ggf. anpassen
dotnet-version: '10.0.x'

- name: Restore dependencies
run: dotnet restore
Expand All @@ -41,7 +41,15 @@ jobs:
-o publish/${{ matrix.rid }}

- name: Upload artifact
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v7
with:
name: backup-${{ matrix.rid }}
path: publish/${{ matrix.rid }}
archive: 'false'
- uses: softprops/action-gh-release@v2
with:
token: '${{ github.token }}'
tag_name: ${{ github.run_id }}
body: |
ByteMeBackup Auto Release for Github.
files: "backup-${{ matrix.rid }}"
10 changes: 8 additions & 2 deletions ByteMeBackup/Configuration/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class AppConfig
public class BackupConfig
{
public string BackupPath { get; set; } = string.Empty;
public string[] BackupFiles { get; set; } = [];
public BackupMode BackupMode { get; set; } = BackupMode.Folder;
public string BackupPrefix { get; set; } = string.Empty;

// Soon to be implemented
// public ??? BackupSchedule { get; set; } = ???;
public BackupType BackupType { get; set; } = BackupType.MountedDrive;
public string MountedDrivePath { get; set; } = string.Empty;
}
Expand All @@ -30,4 +30,10 @@ public enum BackupType
{
ToServer,
MountedDrive,
}

public enum BackupMode
{
Files,
Folder,
}
63 changes: 53 additions & 10 deletions ByteMeBackup/Core/BackupTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,64 @@ public async Task Run()
await LogAsync("Starting backup task...", "[bold gray]Starting backup task...[/]");
await LogAsync("Backup for Config", $"Start Backup with path {BackupConfig.BackupPath}");

var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
var timestamp = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
var zipFileName = $"{BackupConfig.BackupPrefix}{timestamp}.tar.gz";
var tempBackupPath = Path.GetTempPath() + BackupConfig.BackupPrefix + timestamp;
var tempZipPath = Path.Combine(Path.GetTempPath(), zipFileName);

await TarFile.CreateFromDirectoryAsync(BackupConfig.BackupPath, tempZipPath, true,
new CancellationToken(false));
await LogAsync($"Choosing Backup Mode {BackupConfig.BackupMode.ToString()}",
$"[white]Choosing Backup Mode \"{BackupConfig.BackupMode.ToString()}\"[/]");
switch (BackupConfig.BackupMode)
{
case BackupMode.Files:
{
await LogAsync("-# Creating Temporary Backup Folder", $"[gray]Creating Temporary Backup Folder[/]");
var uuid = $"{Guid.CreateVersion7().ToString()}-{timestamp}";
var tempBackupFolder = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), uuid));

try
{
await LogAsync("-# Collecting Files for File Backup.",
$"[gray]Collecting Files for File Backup.[/]");
foreach (var path in BackupConfig.BackupFiles)
{
await LogAsync($"-# Copy File {path} to backup.", $"[gray]Copy File {path} to backup.[/]");
File.Copy(path, Path.Combine(tempBackupFolder.FullName, path.Split("/").Last()));
}

await LogAsync("-# Creating tag.gz file for backup.",
"[gray]Creating tag.gz file for backup.[/]");
await TarFile.CreateFromDirectoryAsync(tempBackupFolder.FullName, tempZipPath, true,
new CancellationToken(false));
}
finally
{
foreach (var fileInfo in tempBackupFolder.GetFiles())
{
fileInfo.Delete();
}
}
}
break;
case BackupMode.Folder:
{
await LogAsync("-# Creating tag.gz file for backup.",
"[gray]Creating tag.gz file for backup.");
await TarFile.CreateFromDirectoryAsync(BackupConfig.BackupPath, tempZipPath, true,
new CancellationToken(false));
}
break;
}

var mode = BackupConfig.BackupMode == BackupMode.Files
? $"Files: \n{string.Join(", \n> ", BackupConfig.BackupFiles)}"
: $"Directory: {BackupConfig.BackupPath}";
await LogAsync($"""
**Backup created successfully!**
> Directory: {BackupConfig.BackupPath}
> Zip File: {zipFileName}
> Temp Zip Path: {tempZipPath}
> Mode: {BackupConfig.BackupMode.ToString()}
> {mode}
> Backup-File: {zipFileName}
> Temporary Path: {tempZipPath}
> Timestamp: {timestamp}
""",
$"[green]Backup created: {tempZipPath}[/]"
Expand All @@ -57,8 +102,8 @@ await LogAsync($"""
break;

default:
var ex = new NotSupportedException($"Backup type {BackupConfig.BackupType} is not supported.");
AnsiConsole.WriteException(ex);
AnsiConsole.WriteException(
new NotSupportedException($"Backup type {BackupConfig.BackupType} is not supported."));
await LogService.SendLogAsync($"❌ **Unsupported backup type:** {BackupConfig.BackupType}");
return;
}
Expand Down Expand Up @@ -98,8 +143,6 @@ private async Task HandleMountedDriveBackup(string sourceZipPath, string fileNam

File.Copy(sourceZipPath, targetPath, overwrite: true);

// home/test
// /home/backuo
await LogAsync($"""
**Backup copied to mounted drive successfully!**
> File: {fileName}
Expand Down
1 change: 0 additions & 1 deletion ByteMeBackup/Services/DiscordWebhookLogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public async Task SendLogAsync(string message)
{
content = message,
username = "ByteMeBackup",
avatar_url = "https://i.imgur.com/ytvbK23.png"
};

var response = await httpClient.PostAsJsonAsync(config.DiscordWebhookUrl, payload);
Expand Down