예제 #1
0
        public static async Task <ReturnedSaveFuncInfo> BackupDbAsync(string connectionString, ENSource Source, string path = "", Guid?Guid = null, CancellationToken token = default)
        {
            var    line         = 0;
            var    res          = new ReturnedSaveFuncInfo();
            bool   IsAutomatic  = string.IsNullOrEmpty(path);
            string DatabaseName = "";

            try
            {
                OnCreateBackup?.Invoke(null, new CreateBackupArgs()
                {
                    Message = "", State = CreateBackupState.Start
                });
                token.ThrowIfCancellationRequested();
                DeleteTempFilesToken?.Cancel();

                if (IsAutomatic)
                {
                    path = CreateFileName(connectionString);
                }
                if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
                {
                    //در نرم افزار حسابداری آدرس پوشه پشتیبان گیری اتوماتیک داده شده است
                    //باید فایل پشتیبان در این پوشه ایجاد گردد
                    path        = CreateFileName(connectionString, path);
                    IsAutomatic = true;
                }

                DeleteTempFilesToken?.Cancel();
                token.ThrowIfCancellationRequested();

                //تهیه اولین نسخه بکاپ توسط سرویس اس کیو ال
                var CreateSqlBackuResult = await CreateSqlServerBackupFileAsync(path, connectionString);

                DatabaseName = CreateSqlBackuResult.value;
                res.AddReturnedValue(CreateSqlBackuResult);
                DeleteTempFilesToken?.Cancel();
                token.ThrowIfCancellationRequested();

                var PathForZipDirectory = Zip.Move2Temp(path);
                res.AddReturnedValue(PathForZipDirectory);
                DeleteTempFilesToken?.Cancel();
                token.ThrowIfCancellationRequested();

                try
                {
                    //please dont remove this try
                    //اگر به خطا خورد باید تابع ادامه پیدا کند و پشتیبان با پسوند .بک تهیه شود
                    res.AddReturnedValue(await CompressFile.CompressFileInstance.CompressFileAsync(PathForZipDirectory.value, path, token));
                }
                catch (Exception ex)
                {
                    WebErrorLog.ErrorInstence.StartErrorLog(ex);
                    res.AddReturnedValue(ex);
                }
                res.value = path.ToLower().Replace(".npz2", ".np").Replace(".npz", ".np").Replace(".np", ".npz2");

                //بنا به هر دلیلی امکان فشرده کردن فایل وجود نداشته است
                //باید همان بکاپ فشرده نشده به آدرس مقصد درخواستی مشتری متقل گردد
                if (res.HasError)
                {
                    var file = Directory.GetFiles(PathForZipDirectory.value).FirstOrDefault();
                    File.Move(file, path.ToLower().Replace(".npz2", ".np").Replace(".npz", ".np"));
                }

                OnCreateBackup?.Invoke(null,
                                       new CreateBackupArgs()
                {
                    Message = "", State = CreateBackupState.End
                });
            }
            catch (OperationCanceledException ex)
            {
                res.AddReturnedValue(ex);
            }
            catch (Exception ex)
            {
                WebErrorLog.ErrorInstence.StartErrorLog(ex, $"Error in Line {line}");
                res.AddReturnedValue(ex);
            }
            finally
            {
                DeleteTempFilesToken?.Cancel();
                DeleteTempFilesToken = new CancellationTokenSource();
                Task.Run(() => deleteTempsAsync(DeleteTempFilesToken.Token));
                Task.Run(() => BackupHistory.DeleteOldBackupsAsync());
                Task.Run(() => BackupHistory.SaveAsync(new BackupHistory()
                {
                    Date         = DateTime.Now,
                    Guid         = Guid ?? System.Guid.NewGuid(),
                    IsAutomatic  = IsAutomatic,
                    DatabaseName = DatabaseName,
                    Path         = path,
                    IsSuccess    = !res.HasError,
                    Source       = Source
                }));
            }
            return(res);
        }
예제 #2
0
        public static async Task <ReturnedSaveFuncInfo> ReStoreDbAsync(string connectionString, string pathf, bool autoBackup, ENSource Source)
        {
            var ret = new ReturnedSaveFuncInfo();
            var cn  = new SqlConnection();

            try
            {
                GC.Collect();
                SqlConnection.ClearAllPools();

                if (autoBackup)
                {
                    var dir = Application.StartupPath + "\\NovinPardazBackUp";
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }

                    var file = Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly()?.Location)?.Replace(".exe", "__");
                    var d    = Calendar.MiladiToShamsi(DateTime.Now).Replace("/", "_");
                    d    += "__" + DateTime.Now.Hour + " - " + DateTime.Now.Minute;
                    file += d;

                    var filepath = dir + "\\" + file + ".Bak";
                    ret.AddReturnedValue(await BackupDbAsync(connectionString, Source, filepath, Guid.NewGuid()));
                }

                if (pathf.EndsWith(".NPZ") || pathf.EndsWith(".npz"))
                {
                    var zp = new Zip();
                    pathf = zp.ExtractTempDIR(pathf);
                }
                else if (pathf.EndsWith(".NPZ2") || pathf.EndsWith(".npz2"))
                {
                    try
                    {
                        var zp  = new CompressFile();
                        var res = await zp.ExtractTempDIR(pathf);

                        ret.AddReturnedValue(res);
                        if (res.HasError)
                        {
                            return(ret);
                        }
                        pathf = res.value;
                    }
                    catch
                    {
                        var zp = new Zip();
                        pathf = zp.ExtractTempDIR(pathf);
                    }
                }

                var csb = new SqlConnectionStringBuilder(connectionString);
                var serverConnectionString =
                    connectionString.Replace("Initial Catalog=" + csb.InitialCatalog, "Initial Catalog=");
                cn = new SqlConnection(serverConnectionString)
                {
                    ConnectionString = serverConnectionString
                };
                var cmd        = new SqlCommand("RESTORE DATABASE [dbName] FROM  DISK = N'bkPath' WITH  FILE = 1,  MOVE N'novin' TO N'dbMDFPath',  MOVE N'novin_log' TO N'dbLDFPath',   NOUNLOAD,  REPLACE,  STATS = 5", cn);
                var dbInfo     = new CLSServerDBs(connectionString);
                var backUpInfo = new DataBaseBackUpInfo(pathf, cn.ConnectionString);

                cmd.CommandText = cmd.CommandText.Replace("dbName", csb.InitialCatalog);
                cmd.CommandText = cmd.CommandText.Replace("bkPath", pathf);
                cmd.CommandText = cmd.CommandText.Replace("novin", backUpInfo.LogicalName);
                cmd.CommandText = cmd.CommandText.Replace("novin_log", backUpInfo.LogicalName + "_log.ldf");
                cmd.CommandText = cmd.CommandText.Replace("dbMDFPath", dbInfo.FileName);
                cmd.CommandText =
                    cmd.CommandText.Replace("dbLDFPath", dbInfo.FileName.ToLower().Replace(".mdf", "_log.Ldf"));

                //تایم اوت بالا بردن مجاز است
                cmd.CommandTimeout = 5 * 60 * 1000;//5 minutes

                cn.Open();
                var temp = cmd.CommandText;
                cmd.CommandText = "ALTER DATABASE [" + csb.InitialCatalog + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE";
                cmd.ExecuteNonQuery();

                cmd.CommandText = temp;
                cmd.ExecuteNonQuery();

                // cmd.CommandText = NovinPardaz.Properties.Resources.DatabaseSettings;
                cmd.CommandText = cmd.CommandText.Replace("dbName", csb.InitialCatalog);
                cmd.ExecuteNonQuery();

                cmd.CommandText = "ALTER DATABASE [" + csb.InitialCatalog + "] SET Multi_USER";
                cmd.ExecuteNonQuery();
                cn.Close();

                SqlConnection.ClearAllPools();
                Application.DoEvents();
            }
            catch (SqlException ex)
            {
                WebErrorLog.ErrorInstence.StartErrorLog(ex);
                try { cn.Close(); } catch { }
                ret.AddReturnedValue(ReturnedState.Error, $"حساب کاربری SQL دسترسی به مسیر نصب برنامه ندارد\r\n{ex.Message }//{ex?.InnerException?.Message ?? ""}");
            }
            catch (Exception ex)
            {
                WebErrorLog.ErrorInstence.StartErrorLog(ex);
                try { cn.Close(); } catch { }
                ret.AddReturnedValue(ex);
            }
            Task.Run(() => SetMultiUser(connectionString));
            return(ret);
        }