/// <summary> /// Tries to get the latest database with a prefix 'backup' /// </summary> /// <param name="cstr">Connection string to the server from which the latest database is picked</param> /// <returns></returns> private async Task <string> GetLatestOnlineBackupDatabase(SqlConnectionStringBuilder cstr) { // Connect to the master database using (var connection = await cstr.ConnectToMaster()) { // Get databases var databases = await GetDatabases(connection, DatabaseState.ONLINE); // Gather backups with matching prefix and order descending var ordered = from db in databases let backupMeta = db.GetBackupMetadata() where backupMeta != null && String.Equals( ExportPrefix, backupMeta.Prefix, StringComparison.OrdinalIgnoreCase) orderby backupMeta.Timestamp descending select backupMeta; // Take the most recent one and check it's time var mostRecent = ordered.FirstOrDefault(); if (mostRecent != null) { return(mostRecent.Db.name); } } return(null); }
private async Task <bool> DoesDBExist(SqlConnectionStringBuilder cstr, string name) { // Connect to the master database using (var connection = await cstr.ConnectToMaster()) { var db = await GetDatabase(connection, name); return(db != null); } }
private async Task CleanExports(SqlConnectionStringBuilder cstr, DateTime timestampUtc) { using (var connection = await cstr.ConnectToMaster()) { // Get databases var databases = await GetDatabases(connection); // Delete matching DBs var toDelete = from d in databases let backupMeta = d.GetBackupMetadata() where backupMeta != null && String.Equals( ExportPrefix, backupMeta.Prefix, StringComparison.OrdinalIgnoreCase) && backupMeta.Timestamp.UtcDateTime <= timestampUtc select d; foreach (var db in toDelete) { Log.DroppingDatabase(db.name); await connection.ExecuteAsync("DROP DATABASE [" + db.name + "]"); } } }