33using Amazon . S3 . Model ;
44using System ;
55using System . Collections . Generic ;
6+ using System . Configuration ;
7+ using System . Diagnostics ;
68using System . IO ;
79using System . Linq ;
8- using System . Management . Automation ;
9- using System . Net ;
10- using System . Text ;
11- using System . Threading . Tasks ;
1210
1311namespace SvnManager . WebUI
1412{
1513 public class SvnBackups
1614 {
17- private const string AccessKey = "" ;
18- private const string SecretKey = "" ;
19- private const string BucketName = "" ;
20- private const string BackupLocation = "" ;
15+ private static string AccessKey => ConfigurationManager . AppSettings [ "Backups.S3AccessKey" ] ;
16+ private static string SecretKey => ConfigurationManager . AppSettings [ "Backups.S3SecretKey" ] ;
17+ private static string BucketName => ConfigurationManager . AppSettings [ "Backups.S3BucketName" ] ;
18+ private static string BackupLocation => ConfigurationManager . AppSettings [ "Backups.BackupLocation" ] ;
19+ private static string RepoPath => ConfigurationManager . AppSettings [ "Manager.RepoPath" ] ;
20+ private static string SvnLocation => ConfigurationManager . AppSettings [ "SvnLocation" ] ;
21+ private static int DaysBetweenBackups => Convert . ToInt32 ( ConfigurationManager . AppSettings [ "Backups.DaysBetweenBackups" ] ) * - 1 ;
2122
2223 public static void Upload ( )
2324 {
@@ -27,13 +28,13 @@ public static void Upload()
2728 {
2829 BasicAWSCredentials cred = new BasicAWSCredentials ( AccessKey , SecretKey ) ;
2930 AmazonS3Client c = new AmazonS3Client ( cred , Amazon . RegionEndpoint . USWest2 ) ;
30- var files = new DirectoryInfo ( BackupLocation ) . EnumerateFiles ( ) . Where ( f => f . CreationTimeUtc > DateTime . UtcNow . AddDays ( - 7 ) ) ;
31+ var files = new DirectoryInfo ( BackupLocation ) . EnumerateFiles ( ) . Where ( f => f . CreationTimeUtc > DateTime . UtcNow . AddDays ( DaysBetweenBackups ) ) ;
3132 foreach ( var file in files )
3233 {
3334 InitiateMultipartUploadResponse initResponse = c . InitiateMultipartUpload ( new InitiateMultipartUploadRequest { StorageClass = S3StorageClass . StandardInfrequentAccess , BucketName = BucketName , Key = file . Name } ) ;
34- long partSize = 100 * ( long ) Math . Pow ( 2 , 20 ) ;
35+ long partSize = Convert . ToInt64 ( ConfigurationManager . AppSettings [ "Backups.S3PartSize" ] ) * ( long ) Math . Pow ( 2 , 20 ) ;
3536 List < UploadPartResponse > resps = new List < UploadPartResponse > ( ) ;
36- //var resp = c.PutObject(new Amazon.S3.Model.PutObjectRequest() { FilePath = file.FullName, BucketName = BucketName, Key = file.Name, StorageClass = S3StorageClass.StandardInfrequentAccess });
37+
3738 try
3839 {
3940 long filePosition = 0 ;
@@ -88,7 +89,7 @@ public static void DeleteLocal()
8889 {
8990 try
9091 {
91- var files = new DirectoryInfo ( BackupLocation ) . EnumerateFiles ( ) . Where ( f => f . CreationTimeUtc < DateTime . UtcNow . AddDays ( - 7 ) ) ;
92+ var files = new DirectoryInfo ( BackupLocation ) . EnumerateFiles ( ) . Where ( f => f . CreationTimeUtc < DateTime . UtcNow . AddDays ( DaysBetweenBackups ) ) ;
9293 foreach ( var file in files )
9394 {
9495 file . Delete ( ) ;
@@ -102,15 +103,27 @@ public static void DeleteLocal()
102103
103104 public static void RunBackups ( )
104105 {
106+ string output ;
105107 try
106108 {
107- // TODO: Create backups
108- //using (PowerShell ps = PowerShell.Create())
109- //{
110- // ps.AddCommand("Backup-SvnRepository");
111- // ps.AddArgument("*");
112- // ps.Invoke();
113- //}
109+ var repos = new DirectoryInfo ( RepoPath ) . GetDirectories ( ) ;
110+ foreach ( var r in repos )
111+ {
112+ using ( Process p = new Process ( ) )
113+ {
114+ p . StartInfo = new ProcessStartInfo ( $@ "{ SvnLocation } \svnadmin", $@ "dump { r . FullName } > { BackupLocation } \{ r . Name } _{ DateTime . UtcNow : yyyyMMddHHmm} .dump")
115+ {
116+ RedirectStandardError = true
117+ } ;
118+ p . Start ( ) ;
119+ p . WaitForExit ( ) ;
120+ output = p . StandardError . ReadToEnd ( ) ;
121+ if ( p . ExitCode != 0 )
122+ {
123+ // TODO: Send email with output ?
124+ }
125+ }
126+ }
114127 }
115128 catch ( Exception ex )
116129 {
@@ -120,7 +133,7 @@ public static void RunBackups()
120133
121134 public static bool IsBackupTime ( )
122135 {
123- var files = new DirectoryInfo ( BackupLocation ) . EnumerateFiles ( ) . Where ( f => f . CreationTimeUtc > DateTime . UtcNow . AddDays ( - 7 ) ) ;
136+ var files = new DirectoryInfo ( BackupLocation ) . EnumerateFiles ( ) . Where ( f => f . CreationTimeUtc > DateTime . UtcNow . AddDays ( - DaysBetweenBackups ) ) ;
124137 if ( files . Count ( ) <= 0 && DateTime . UtcNow . Hour > 3 && DateTime . UtcNow . Hour < 8 )
125138 return true ;
126139
0 commit comments