Skip to content

Commit c9d19ac

Browse files
Merge pull request #82 from andrewlorien/feature/add-s3-upload
Feature/add s3 upload
2 parents e727295 + 3c9d918 commit c9d19ac

5 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/ScmBackup/BackupMaker.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
using ScmBackup.Hosters;
33
using ScmBackup.Http;
44
using System.Collections.Generic;
5+
using System.IO;
6+
using Amazon.S3;
7+
using Amazon.S3.Transfer;
8+
using System;
59

610
namespace ScmBackup
711
{
@@ -45,7 +49,42 @@ public string Backup(ConfigSource source, IEnumerable<HosterRepository> repos)
4549
}
4650
}
4751

52+
// Upload backup folder to S3 if bucket name is specified
53+
if (!string.IsNullOrEmpty(this.context.Config.Options.Backup.s3BucketName))
54+
{
55+
UploadToS3(sourceFolder, this.context.Config.Options.Backup.s3BucketName);
56+
}
57+
4858
return sourceFolder;
4959
}
60+
61+
private void UploadToS3(string sourceFolderPath, string bucketName)
62+
{
63+
try
64+
{
65+
this.logger.Log(ErrorLevel.Info, "Starting upload to S3 bucket: {0}", bucketName);
66+
67+
using (var s3Client = new AmazonS3Client())
68+
{
69+
var transferUtility = new TransferUtility(s3Client);
70+
var directoryTransferUtility = new TransferUtilityUploadDirectoryRequest
71+
{
72+
BucketName = bucketName,
73+
Directory = sourceFolderPath,
74+
SearchPattern = "*.*",
75+
SearchOption = SearchOption.AllDirectories,
76+
KeyPrefix = Path.GetFileName(sourceFolderPath)
77+
};
78+
79+
transferUtility.UploadDirectory(directoryTransferUtility);
80+
}
81+
82+
this.logger.Log(ErrorLevel.Info, "Successfully uploaded backup to S3 bucket: {0}", bucketName);
83+
}
84+
catch (Exception ex)
85+
{
86+
this.logger.Log(ErrorLevel.Error, "Failed to upload to S3: {0}", ex.Message);
87+
}
88+
}
5089
}
5190
}

src/ScmBackup/Configuration/ConfigOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ class BackupOptions
1818
public bool LogRepoFinished { get; set; }
1919
public bool AddTimestampedSubfolder { get; set; }
2020
public string TimestampFormat { get; set; }
21+
public string s3BucketName { get; set; }
2122
}
2223
}

src/ScmBackup/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[assembly: AssemblyFileVersion("0.0.0")]
1010
[assembly: AssemblyInformationalVersion("0.0.0-DEV")]
1111
[assembly: AssemblyTitle("SCM Backup")]
12-
[assembly: AssemblyDescription("Backup repositories from cloud hosters to your local machine")]
12+
[assembly: AssemblyDescription("Backup repositories from cloud hosters to your local machine or S3 bucket")]
1313
[assembly: AssemblyConfiguration("")]
1414
[assembly: AssemblyCompany("scm-backup.org")]
1515
[assembly: AssemblyProduct("SCM Backup")]

src/ScmBackup/ScmBackup.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
</ItemGroup>
2929

3030
<ItemGroup>
31+
<PackageReference Include="AWSSDK.S3" Version="3.7.305.28" />
3132
<PackageReference Include="MailKit" Version="4.5.0" />
3233
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
3334
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

src/ScmBackup/settings.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,7 @@ options:
4949
addTimestampedSubfolder: false
5050

5151
# timestamp format for timestamped subfolder.
52-
timestampFormat: yyyy-MM-dd_HHmm
52+
timestampFormat: yyyy-MM-dd_HHmm
53+
54+
# if this is not empty, save the files to an S3 bucket
55+
s3BucketName:

0 commit comments

Comments
 (0)