/// <summary>备份文件到目标文件</summary> /// <param name="bakfile"></param> public void Backup(String bakfile) { bakfile = bakfile.GetFullPath().EnsureDirectory(); WriteLog("{0}备份SQLite数据库{1}到{2}", Database.ConnName, (Database as SQLite).FileName, bakfile); var sw = new Stopwatch(); sw.Start(); // 删除已有文件 if (File.Exists(bakfile)) { File.Delete(bakfile); } using (var session = Database.CreateSession()) using (var conn = Database.Factory.CreateConnection()) { session.Open(); conn.ConnectionString = "Data Source={0}".F(bakfile); conn.Open(); //var method = conn.GetType().GetMethodEx("BackupDatabase"); // 借助BackupDatabase函数可以实现任意两个SQLite之间倒数据,包括内存数据库 session.Conn.Invoke("BackupDatabase", conn, "main", "main", -1, null, 0); } // 压缩 WriteLog("备份文件大小:{0:n0}字节", bakfile.AsFile().Length); if (bakfile.EndsWithIgnoreCase(".zip")) { //var rnd = new Random(); var tmp = Path.GetDirectoryName(bakfile).CombinePath(Rand.Next() + ".tmp"); File.Move(bakfile, tmp); ZipFile.CompressFile(tmp, bakfile); File.Delete(tmp); WriteLog("压缩后大小:{0:n0}字节", bakfile.AsFile().Length); } sw.Stop(); WriteLog("备份完成,耗时{0:n0}ms", sw.ElapsedMilliseconds); }
/// <summary>压缩文件</summary> /// <param name="fi"></param> /// <param name="destFile"></param> public static void Compress(this FileInfo fi, String destFile) { if (destFile.IsNullOrEmpty()) { destFile = fi.Name + ".zip"; } if (File.Exists(destFile)) { File.Delete(destFile); } if (destFile.EndsWithIgnoreCase(".zip")) { ZipFile.CompressFile(fi.FullName, destFile); } else { new SevenZip().Compress(fi.FullName, destFile); } }
/// <summary> /// Pokrećemo proceduru BACKUP-a /// </summary> public void Start() { // ---------------------------------------------------------------------------------------------------------------------------- // Automatski backup baze // ---------------------------------------------------------------------------------------------------------------------------- // postavljamo automatski backup ukoliko nije podešen if (string.IsNullOrEmpty(Mipsed7.Core.ApplicationDatabaseInformation.AutoBackup_MIPSED7_dbBackup_StartAutomatically)) { Mipsed7.Core.ApplicationDatabaseInformation.AutoBackup_MIPSED7_dbBackup_StartAutomatically = "1"; } if (Mipsed7.Core.ApplicationDatabaseInformation.AutoBackup_MIPSED7_dbBackup_StartAutomatically == "1") { Mipsed7.Core.ApplicationDatabaseInformation.AutoBackup_MIPSED7_dbBackup = AppDomain.CurrentDomain.BaseDirectory + "MIPSED.7.dbbackup.exe"; } else { Mipsed7.Core.ApplicationDatabaseInformation.AutoBackup_MIPSED7_dbBackup = string.Empty; // gasimo aplikaciju System.Diagnostics.Process.GetCurrentProcess().Kill(); // ukoliko automatski backup nije podešen, izađi iz procedure return; } // pristup INTERNETU - ukoliko je FALSE izađi iz procedure if (!IsConnectedToInternet()) { return; } // STEP #1 - planiranje backupo-ova DatabaseMaintenance databaseMaintenance = new DatabaseMaintenance(); ZipFile zipFile = new ZipFile(); FTPClient ftpClient = new FTPClient(); string oibKorisnika = Mipsed7.Core.ApplicationDatabaseInformation.OIB; string databaseBackupFilename = string.Empty; string backupPath = string.Empty; // ---------------------------------------------- // DNEVNI backup // ---------------------------------------------- databaseBackupFilename = string.Format("{0}_database_{1}_dnevni_{2}_{3}_{4}.bak", Mipsed7.Core.ApplicationDatabaseInformation.DatabaseName.Replace(" ", "_"), oibKorisnika, DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day); DatabaseObject databaseObject = new DatabaseObject(DatabaseType.Dnevna, oibKorisnika, databaseBackupFilename); // provjera if (!ftpClient.FileExists(databaseObject)) { // backup databaseMaintenance.Backup(databaseObject, out backupPath); // compress file into ZIP archive zipFile.CompressFile(backupPath); // upload ftpClient.FileUpload(databaseObject, backupPath); } // ---------------------------------------------- // TJEDNI backup // ---------------------------------------------- databaseBackupFilename = string.Format("{0}_database_{1}_tjedni_{2}_{3}.bak", Mipsed7.Core.ApplicationDatabaseInformation.DatabaseName.Replace(" ", "_"), oibKorisnika, DateTime.Today.Year, ApplicationLogic.Tools.DateTimeTool.WeekIndex(DateTime.Today)); databaseObject = new DatabaseObject(DatabaseType.Tjedna, oibKorisnika, databaseBackupFilename); // provjera if (!ftpClient.FileExists(databaseObject)) { // backup databaseMaintenance.Backup(databaseObject, out backupPath); // compress file into ZIP archive zipFile.CompressFile(backupPath); // upload ftpClient.FileUpload(databaseObject, backupPath); // delete daily backups ftpClient.DeleteDailyBackups(string.Format("/{0}/{1}", oibKorisnika, DateTime.Today.Year)); } // ---------------------------------------------- // MJESEČNI backup - prvog u mjesecu za prošli mjesec // ---------------------------------------------- databaseBackupFilename = string.Format("{0}_database_{1}_mjesecni_{2}_{3}.bak", Mipsed7.Core.ApplicationDatabaseInformation.DatabaseName.Replace(" ", "_"), oibKorisnika, DateTime.Today.AddMonths(-1).Year, DateTime.Today.AddMonths(-1).Month); databaseObject = new DatabaseObject(DatabaseType.Mjesecna, oibKorisnika, databaseBackupFilename); // provjera if (!ftpClient.FileExists(databaseObject)) { // backup databaseMaintenance.Backup(databaseObject, out backupPath); // compress file into ZIP archive zipFile.CompressFile(backupPath); // upload ftpClient.FileUpload(databaseObject, backupPath); // delete daily & week backups ftpClient.DeleteDailyBackups(string.Format("/{0}/{1}", oibKorisnika, DateTime.Today.Year)); ftpClient.DeleteWeeklyBackups(string.Format("/{0}/{1}", oibKorisnika, DateTime.Today.Year)); } databaseMaintenance.DeleteBackupFolder(); }