Skip to content

Commit 270edf9

Browse files
committed
Remove zip archival of backups and replace with directory
based archival * Zip-based archival of the backups was running up against physical file size limits (> 4GB) on some storage devices and file systems. A folder structure is now used instead. * Added a new flag to only run the clean up routines for the specified tools * Removed dependence on the System.IO.Compression namespace
1 parent 40b4827 commit 270edf9

File tree

6 files changed

+285
-232
lines changed

6 files changed

+285
-232
lines changed

AtlassianCloudBackupsLibrary/BackupAtlassianService.cs

+86-67
Original file line numberDiff line numberDiff line change
@@ -144,97 +144,116 @@ public async Task<IBackupJob> Prepare()
144144
/// The actual work to get the backup from the remote service
145145
/// </summary>
146146
/// <returns>Awaitable BackupAtlassianService instance</returns>
147-
public async Task<IBackupJob> Execute()
147+
public async Task<IBackupJob> Execute(bool runCleanUpOnly = false)
148148
{
149-
Logger.Current.Log(_logLabel, string.Format("Beginning backup for {0}...", GetServiceToBeBackedUpLabel()));
150-
151-
await Prepare();
152-
153-
using (Client)
149+
if (!runCleanUpOnly)
154150
{
155-
Client.BaseAddress = new Uri(string.Format(_service.BaseUrl, Account));
156-
Client.DefaultRequestHeaders.Accept.Clear();
157-
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
151+
Logger.Current.Log(_logLabel,
152+
string.Format("Beginning backup for {0}...", GetServiceToBeBackedUpLabel()));
158153

159-
Logger.Current.Log(_logLabel, "Attempting to log in to Atlassian...");
160-
// Get auth token from the cloud instance
161-
var content = new StringContent("{" + string.Format(" \"username\" : \"{0}\", \"password\" : \"{1}\" ", UserName, Password) + "}", Encoding.UTF8, "application/json");
154+
await Prepare();
162155

163-
var response = await Client.PostAsync(_service.AuthUrl, content);
164-
165-
if (response.IsSuccessStatusCode)
166-
{
167-
Logger.Current.Log(_logLabel, string.Format("Successfully logged in to the {0} account as {1}.", Account, UserName));
168-
}
169-
else
156+
using (Client)
170157
{
171-
Logger.Current.Log(_logLabel, "Failed to authenticate to Atlassian cloud servers!");
172-
Logger.Current.Log(_logLabel, string.Format("Aborting backup job for {0}!", GetServiceToBeBackedUpLabel()));
173-
return this;
174-
}
158+
Client.BaseAddress = new Uri(string.Format(_service.BaseUrl, Account));
159+
Client.DefaultRequestHeaders.Accept.Clear();
160+
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
175161

176-
Logger.Current.Log(_logLabel, "Triggering backup...");
177-
// Trigger backup
178-
var triggerContent = new StringContent("{ \"cbAttachments\" : true }", Encoding.UTF8, "application/json");
179-
var triggerResponse = await Client.PostAsync(_service.BackupTriggerUrl, triggerContent);
162+
Logger.Current.Log(_logLabel, "Attempting to log in to Atlassian...");
163+
// Get auth token from the cloud instance
164+
var content =
165+
new StringContent(
166+
"{" + string.Format(" \"username\" : \"{0}\", \"password\" : \"{1}\" ", UserName,
167+
Password) + "}", Encoding.UTF8, "application/json");
180168

181-
if (!triggerResponse.IsSuccessStatusCode)
182-
{
183-
var resultBody = triggerResponse.Content.ReadAsStringAsync().Result;
184-
Logger.Current.Log(_logLabel, string.Format("Failed to trigger backup generation! Reason : {0}", resultBody));
185-
}
186-
else
187-
{
188-
Logger.Current.Log(_logLabel, "Backup generation triggered successfully.");
189-
}
169+
var response = await Client.PostAsync(_service.AuthUrl, content);
190170

191-
var backupFileName = string.Empty;
192-
var backupCreated = false;
171+
if (response.IsSuccessStatusCode)
172+
{
173+
Logger.Current.Log(_logLabel,
174+
string.Format("Successfully logged in to the {0} account as {1}.", Account, UserName));
175+
}
176+
else
177+
{
178+
Logger.Current.Log(_logLabel, "Failed to authenticate to Atlassian cloud servers!");
179+
Logger.Current.Log(_logLabel,
180+
string.Format("Aborting backup job for {0}!", GetServiceToBeBackedUpLabel()));
181+
return this;
182+
}
193183

194-
while (!backupCreated)
195-
{
196-
var getBackupProgressResponse = await Client.GetAsync(_service.BackupProgressUrl);
184+
Logger.Current.Log(_logLabel, "Triggering backup...");
185+
// Trigger backup
186+
var triggerContent =
187+
new StringContent("{ \"cbAttachments\" : true }", Encoding.UTF8, "application/json");
188+
var triggerResponse = await Client.PostAsync(_service.BackupTriggerUrl, triggerContent);
197189

198-
if (getBackupProgressResponse.IsSuccessStatusCode)
190+
if (!triggerResponse.IsSuccessStatusCode)
199191
{
200-
var resultBody = getBackupProgressResponse.Content.ReadAsStringAsync();
201-
var body = JObject.Parse(resultBody.Result);
192+
var resultBody = triggerResponse.Content.ReadAsStringAsync().Result;
193+
Logger.Current.Log(_logLabel,
194+
string.Format("Failed to trigger backup generation! Reason : {0}", resultBody));
195+
}
196+
else
197+
{
198+
Logger.Current.Log(_logLabel, "Backup generation triggered successfully.");
199+
}
202200

203-
var currentStatus = body["currentStatus"].ToString();
204-
var estimatedProgress = body["alternativePercentage"].ToString();
205-
var fileNamePropExists = body["fileName"] != null;
201+
var backupFileName = string.Empty;
202+
var backupCreated = false;
206203

207-
Logger.Current.Log(_logLabel, currentStatus + " ==> " + estimatedProgress);
204+
while (!backupCreated)
205+
{
206+
var getBackupProgressResponse = await Client.GetAsync(_service.BackupProgressUrl);
208207

209-
if (fileNamePropExists)
208+
if (getBackupProgressResponse.IsSuccessStatusCode)
210209
{
211-
backupFileName = body["fileName"].ToString();
212-
backupCreated = true;
210+
var resultBody = getBackupProgressResponse.Content.ReadAsStringAsync();
211+
var body = JObject.Parse(resultBody.Result);
213212

214-
Logger.Current.Log(_logLabel, "Backup file generation complete.");
215-
}
216-
}
213+
var currentStatus = body["currentStatus"].ToString();
214+
var estimatedProgress = body["alternativePercentage"].ToString();
215+
var fileNamePropExists = body["fileName"] != null;
217216

218-
System.Threading.Thread.Sleep(5000);
219-
}
217+
Logger.Current.Log(_logLabel, currentStatus + " ==> " + estimatedProgress);
220218

221-
Logger.Current.Log(_logLabel, string.Format("Getting generated backup file from {0}", string.Format(_service.BaseUrl, Account)+_service.DownloadUrlBase+backupFileName));
222-
// Download the backup
223-
using (var fileResponse = await Client.GetAsync(_service.DownloadUrlBase + backupFileName, HttpCompletionOption.ResponseHeadersRead))
224-
{
225-
Logger.Current.Log(_logLabel, string.Format("Downloading {0} bytes of data...", fileResponse.Content.Headers.ContentLength));
219+
if (fileNamePropExists)
220+
{
221+
backupFileName = body["fileName"].ToString();
222+
backupCreated = true;
226223

227-
using (var stream = await fileResponse.Content.ReadAsStreamAsync())
224+
Logger.Current.Log(_logLabel, "Backup file generation complete.");
225+
}
226+
}
227+
228+
System.Threading.Thread.Sleep(5000);
229+
}
230+
231+
Logger.Current.Log(_logLabel,
232+
string.Format("Getting generated backup file from {0}",
233+
string.Format(_service.BaseUrl, Account) + _service.DownloadUrlBase + backupFileName));
234+
// Download the backup
235+
using (var fileResponse = await Client.GetAsync(_service.DownloadUrlBase + backupFileName,
236+
HttpCompletionOption.ResponseHeadersRead))
228237
{
229-
using (var outStream = File.Open(Path.Combine(BackupDestination, string.Format(FileName, DateTime.Now.ToString("MMddyy"))), FileMode.Create))
238+
Logger.Current.Log(_logLabel,
239+
string.Format("Downloading {0} bytes of data...",
240+
fileResponse.Content.Headers.ContentLength));
241+
242+
using (var stream = await fileResponse.Content.ReadAsStreamAsync())
230243
{
231-
await stream.CopyToAsync(outStream);
244+
using (var outStream =
245+
File.Open(
246+
Path.Combine(BackupDestination,
247+
string.Format(FileName, DateTime.Now.ToString("MMddyy"))), FileMode.Create))
248+
{
249+
await stream.CopyToAsync(outStream);
250+
}
232251
}
233252
}
234-
}
235253

236-
Logger.Current.Log(_logLabel, "Download complete.");
237-
Logger.Current.Log(_logLabel, "Backup complete.");
254+
Logger.Current.Log(_logLabel, "Download complete.");
255+
Logger.Current.Log(_logLabel, "Backup complete.");
256+
}
238257
}
239258

240259
await CleanUp();

0 commit comments

Comments
 (0)