diff options
| author | Michael <[email protected]> | 2017-03-25 19:39:42 -0400 |
|---|---|---|
| committer | Michael <[email protected]> | 2017-03-25 19:39:42 -0400 |
| commit | 35e94c2e94c32ff1731acb8d02b20e4accc3ca4a (patch) | |
| tree | f542464188a9c8d2d2209332925e0a9fd1879455 | |
| parent | 1cac3ac592c34ecf373f034c2103a040492ce216 (diff) | |
| download | project-unite-35e94c2e94c32ff1731acb8d02b20e4accc3ca4a.tar.gz project-unite-35e94c2e94c32ff1731acb8d02b20e4accc3ca4a.tar.bz2 project-unite-35e94c2e94c32ff1731acb8d02b20e4accc3ca4a.zip | |
Database backup creation :smiley:
| -rw-r--r-- | Project-Unite/Controllers/AdminController.cs | 59 | ||||
| -rw-r--r-- | Project-Unite/Models/IdentityModels.cs | 10 | ||||
| -rw-r--r-- | Project-Unite/Models/ManageViewModels.cs | 6 | ||||
| -rw-r--r-- | Project-Unite/Project-Unite.csproj | 3 | ||||
| -rw-r--r-- | Project-Unite/Views/Admin/Backups.cshtml | 66 |
5 files changed, 144 insertions, 0 deletions
diff --git a/Project-Unite/Controllers/AdminController.cs b/Project-Unite/Controllers/AdminController.cs index a865ba7..a487dd3 100644 --- a/Project-Unite/Controllers/AdminController.cs +++ b/Project-Unite/Controllers/AdminController.cs @@ -110,6 +110,65 @@ Unlike previous ShiftOS site revamps, your account got migrated over. However, t return RedirectToAction("Users"); } + public ActionResult BackupDatabase() + { + var db = new ApplicationDbContext(); + string backupDir = "~/Backups/Database"; + string backupServerDir = Server.MapPath(backupDir); + if (!System.IO.Directory.Exists(backupServerDir)) + System.IO.Directory.CreateDirectory(backupServerDir); + + string backupUrl = backupDir.Remove(0, 1) + "/ShiftOS-" + DateTime.Now.ToString() + ".zip"; + string backupname = backupServerDir + "\\ShiftOS-" + DateTime.Now.ToString() + ".zip"; + + System.IO.Compression.ZipFile.CreateFromDirectory(Server.MapPath("~/Uploads", backupname)); + + var backupData = new DatabaseBackup(); + backupData.Id = Guid.NewGuid().ToString(); + backupData.UserId = User.Identity.GetUserId(); + backupData.DownloadUrl = backupUrl; + backupData.Timestamp = DateTime.Now; + db.AssetBackups.Add(backupData); + db.SaveChanges(); + return RedirectToAction("Backups"); + } + + public ActionResult BackupAssets() + { + var db = new ApplicationDbContext(); + string backupDir = "~/Backups/Assets"; + string backupServerDir = Server.MapPath(backupDir); + if (!System.IO.Directory.Exists(backupServerDir)) + System.IO.Directory.CreateDirectory(backupServerDir); + + string backupUrl = backupDir.Remove(0, 1) + "/ShiftOS-" + DateTime.Now.ToString() + ".sql"; + string backupname = backupServerDir + "\\ShiftOS-" + DateTime.Now.ToString() + ".sql"; + const string sqlCommand = @"BACKUP DATABASE [{0}] TO DISK = N'{1}' WITH NOFORMAT, NOINIT, NAME = N'ShiftOS Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10"; + int path = db.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, string.Format(sqlCommand, db.Database.Connection.Database, backupname)); + var backupData = new DatabaseBackup(); + backupData.Id = Guid.NewGuid().ToString(); + backupData.UserId = User.Identity.GetUserId(); + backupData.DownloadUrl = backupUrl; + backupData.Timestamp = DateTime.Now; + db.Backups.Add(backupData); + db.SaveChanges(); + return RedirectToAction("Backups"); + } + + + + + public ActionResult Backups() + { + ViewBag.Admin = true; + var backups = new BackupViewModel(); + var db = new ApplicationDbContext(); + backups.Databases = db.Backups.OrderByDescending(x => x.Timestamp); + backups.AssetFolders = db.AssetBackups.OrderByDescending(x => x.Timestamp); + + return View(backups); + } + public void DeleteTopic(ForumTopic topic) { foreach(var post in topic.Posts.ToArray()) diff --git a/Project-Unite/Models/IdentityModels.cs b/Project-Unite/Models/IdentityModels.cs index 187bad3..ced6fc3 100644 --- a/Project-Unite/Models/IdentityModels.cs +++ b/Project-Unite/Models/IdentityModels.cs @@ -204,6 +204,8 @@ namespace Project_Unite.Models return new ApplicationDbContext(); } + public DbSet<DatabaseBackup> Backups { get; set; } + public DbSet<DatabaseBackup> AssetBackups { get; set; } public DbSet<Avatar> UserAvatars { get; set; } public DbSet<Skin> Skins { get; set; } public DbSet<Configuration> Configs { get; set; } @@ -278,4 +280,12 @@ namespace Project_Unite.Models public string AvatarUrl { get; set; } public DateTime UploadedAt { get; set; } } + + public class DatabaseBackup + { + public string Id { get; set; } + public DateTime Timestamp { get; set; } + public string UserId { get; set; } + public string DownloadUrl { get; set; } + } }
\ No newline at end of file diff --git a/Project-Unite/Models/ManageViewModels.cs b/Project-Unite/Models/ManageViewModels.cs index 7a84bda..d8a1497 100644 --- a/Project-Unite/Models/ManageViewModels.cs +++ b/Project-Unite/Models/ManageViewModels.cs @@ -14,6 +14,12 @@ namespace Project_Unite.Models public bool BrowserRemembered { get; set; } } + public class BackupViewModel + { + public IEnumerable<DatabaseBackup> Databases { get; set; } + public IEnumerable<DatabaseBackup> AssetFolders { get; set; } + } + public class ManageLoginsViewModel { public IList<UserLoginInfo> CurrentLogins { get; set; } diff --git a/Project-Unite/Project-Unite.csproj b/Project-Unite/Project-Unite.csproj index 34ecf81..7c03cf0 100644 --- a/Project-Unite/Project-Unite.csproj +++ b/Project-Unite/Project-Unite.csproj @@ -121,6 +121,8 @@ <Reference Include="System" /> <Reference Include="System.Data" /> <Reference Include="System.Drawing" /> + <Reference Include="System.IO.Compression" /> + <Reference Include="System.IO.Compression.FileSystem" /> <Reference Include="System.Web.DynamicData" /> <Reference Include="System.Web.Entity" /> <Reference Include="System.Web.ApplicationServices" /> @@ -527,6 +529,7 @@ <Content Include="Views\Skins\PostSkin.cshtml" /> <Content Include="Views\Manage\ListAvatars.cshtml" /> <Content Include="Views\Manage\UploadAvatar.cshtml" /> + <Content Include="Views\Admin\Backups.cshtml" /> </ItemGroup> <ItemGroup> <Folder Include="App_Data\" /> diff --git a/Project-Unite/Views/Admin/Backups.cshtml b/Project-Unite/Views/Admin/Backups.cshtml new file mode 100644 index 0000000..313c2e7 --- /dev/null +++ b/Project-Unite/Views/Admin/Backups.cshtml @@ -0,0 +1,66 @@ +@model Project_Unite.Models.BackupViewModel +@{ + ViewBag.Title = "Backups"; +} + +<h2>Backups</h2> + +<p>On this page you can request a backup of the current database and/or asset folder - or restore from a previous backup.</p> + +<ul id="tabs" data-tabs="tabs" class="nav nav-tabs" role="tablist"> + <li class="active"><a data-toggle="tab" href="#t_dbs">Databases</a></li> + <li><a data-toggle="tab" href="#t_assets">Assets</a></li> +</ul> + +<div class="tab-content"> + <div class="tab-pane fade in active" id="t_dbs"> + <h4>Databases</h4> + <p>The database is what holds the bulk of the data on the ShiftOS site - forum posts, users, ACL rules, etc. Backing up the database can help in the case of spam, or a bug, or if you need to export the data to another software platform.</p> + + <ul class="nav nav-pills"> + <li><a href="@Url.Action("BackupDatabase")"><span class="glyphicon glyphicon-plus"></span> Backup database</a></li> + </ul> + <table class="table"> + <tr> + <th style="width:65%">Backup</th> + <th>Actions</th> + </tr> + @foreach(var backup in Model.Databases) + { + <tr> + <td>@Html.UserLink(backup.UserId) on @backup.Timestamp</td> + <td> + <a href="@backup.DownloadUrl" class="btn btn-default"><span class="glyphicon glyphicon-arrow-down"></span> Download</a> + @Html.ActionLink("Restore", "RestoreDatabaseBackup", new { id = backup.Id }); + </td> + </tr> + } + </table> + </div> + <div class="tab-pane fade in" id="t_assets"> + <h4>Asset folders</h4> + <p>The asset folder is a dumping ground for user-created content hosted by ShiftOS. Avatars, profile backdrops, skins, game releases and other files not suited for storage within the database are put here.</p> + + <p>User data is sorted by the site into folders named after the user. Backing this folder up can help in case the site's server goes down unexpectedly and cannot get back up and running - at least you have the user-created content and can bring it to a new server.</p> + + <ul class="nav nav-pills"> + <li><a href="@Url.Action("BackupAssets")"><span class="glyphicon glyphicon-plus"></span> Backup assets</a></li> + </ul> + <table class="table"> + <tr> + <th style="width:65%">Backup</th> + <th>Actions</th> + </tr> + @foreach (var backup in Model.AssetFolders) + { + <tr> + <td>@Html.UserLink(backup.UserId) on @backup.Timestamp</td> + <td> + <a href="@backup.DownloadUrl" class="btn btn-default"><span class="glyphicon glyphicon-arrow-down"></span> Download</a> + @Html.ActionLink("Restore", "RestoreAssetBackup", new { id = backup.Id }); + </td> + </tr> + } + </table> + </div> +</div>
\ No newline at end of file |
