private void UploadDatabaseBackupScript(GoogleDriveManager googleDriveManager) { var connectionString = ConfigurationManager.ConnectionStrings["Gallery"].ToString(); var server = new Server(new ServerConnection(new SqlConnection(connectionString))); var database = server.Databases[new SqlConnectionStringBuilder(connectionString).InitialCatalog]; var options = new ScriptingOptions { ScriptData = true, ScriptSchema = true, ScriptDrops = false, Indexes = true, IncludeHeaders = true }; byte[] bytes = null; using (var ms = new MemoryStream()) { TextWriter tw = new StreamWriter(ms); foreach (Table table in database.Tables) { foreach (var statement in table.EnumScript(options)) { tw.WriteLine(statement); } } tw.Flush(); ms.Position = 0; bytes = ms.ToArray(); } googleDriveManager.Upload($"#Backup#{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.sql", bytes); }
public void Execute(IJobExecutionContext context) { using (var db = new GalleryContext()) { var numberOfPicturesToUpload = Convert.ToInt32(ConfigurationManager.AppSettings["NumberOfPicturesToUpload"].ToString()); var pictureToProcessList = db.Picture.Where(x => !x.IsBackupCopySaved).OrderBy(x => x.CreationDate).Take(numberOfPicturesToUpload).ToList(); if (pictureToProcessList.Count == 0) { return; } var googleDriveManager = new GoogleDriveManager(); foreach (var pictureToProcess in pictureToProcessList) { var regex = new Regex($"^{pictureToProcess.id}[.-]"); var picturePathList = Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Picture")) .Where(x => regex.IsMatch(Path.GetFileName(x))) .ToList(); picturePathList.ForEach(x => googleDriveManager.Upload(x)); pictureToProcess.IsBackupCopySaved = true; db.SaveChanges(); } UploadDatabaseBackupScript(googleDriveManager); } }