mirror of
https://github.com/lempamo/Project-Unite.git
synced 2025-01-22 17:22:15 +00:00
Database backup creation 😃
This commit is contained in:
parent
1cac3ac592
commit
35e94c2e94
5 changed files with 144 additions and 0 deletions
|
@ -110,6 +110,65 @@ public ActionResult CreateUser(CreateUserModel model)
|
|||
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())
|
||||
|
|
|
@ -204,6 +204,8 @@ public static ApplicationDbContext Create()
|
|||
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 @@ public class Avatar
|
|||
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; }
|
||||
}
|
||||
}
|
|
@ -14,6 +14,12 @@ public class IndexViewModel
|
|||
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; }
|
||||
|
|
|
@ -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\" />
|
||||
|
|
66
Project-Unite/Views/Admin/Backups.cshtml
Normal file
66
Project-Unite/Views/Admin/Backups.cshtml
Normal file
|
@ -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>
|
Loading…
Reference in a new issue