Example #1
0
        public static BackupItemEx FindBackupItemEx(List <BackupItemEx> items, Type objectType)
        {
            BackupItemEx bi = null;

            if (items != null)
            {
                foreach (BackupItemEx item in items)
                {
                    if (item.ItemType == objectType)
                    {
                        bi = item;
                        break;
                    }
                }
            }
            return(bi);
        }
Example #2
0
 protected virtual void Backup(BackupItemEx bi)
 {
 }
Example #3
0
        public virtual bool Backup(List <BackupItemEx> items, string backupfile)
        {
            try
            {
                if (items != null && items.Count > 0)
                {
                    string dir = AppDomain.CurrentDomain.BaseDirectory;
                    // make sure the capacity is big enough
                    List <string>  files          = new List <string>(items.Count);
                    int            index          = 0;
                    int            count          = 0;
                    AutoResetEvent autoResetEvent = new AutoResetEvent(false);
                    foreach (BackupItemEx bi in items)
                    {
                        if ((bi.BackupObjects == null || bi.BackupObjects.Count == 0) && string.IsNullOrEmpty(bi.DumpQuery))
                        {
                            continue;
                        }

                        ++count;
                        ThreadPool.QueueUserWorkItem((obj) =>
                        {
                            BackupItemEx tmpBi = obj as BackupItemEx;
                            Backup(tmpBi);
                            if (File.Exists(tmpBi.BackupFileName))
                            {
                                FileInfo fi = new FileInfo(tmpBi.BackupFileName);
                                if (fi.Length > 2)
                                {
                                    lock (this) //need synchronization block
                                    {
                                        files.Add(tmpBi.BackupFileName);
                                    }
                                }
                                else
                                {
                                    FileDirectoryOperate.DeleteFileWithTime(tmpBi.BackupFileName);
                                }
                            }
                            Interlocked.Increment(ref index);
                            if (index == count)
                            {
                                autoResetEvent.Set();
                            }

                            try
                            {
                                if (worker != null && worker.WorkerReportsProgress && worker.IsBusy)
                                {
                                    worker.ReportProgress(index * 100 / items.Count);
                                }
                            }
                            catch
                            {
                            }
                        }, bi);
                    }
                    autoResetEvent.WaitOne(60000, false);


                    if (files.Count == 0)
                    {
                        return(false);
                    }

                    if (worker != null && worker.WorkerReportsProgress && worker.IsBusy)
                    {
                        worker.ReportProgress(50, string.Format("Back up  database:", backupfile));
                    }

                    SharpZipHelper.ZipMultiFiles(files.ToArray(), backupfile, DatabaseCommon.ZipPwd, 3, worker);
                    foreach (string file in files)
                    {
                        FileDirectoryOperate.DeleteFileWithTime(file);
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex);
                return(false);
            }
        }
Example #4
0
        protected override void Backup(BackupItemEx bi)
        {
            string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Temp\");

            if (!Directory.Exists(dir))
            {
                FileDirectoryOperate.CreateDirectoryEx(dir);
            }
            if (bi.IsFullTable)
            {
                bi.BackupFileName = Path.Combine(dir, bi.TableName + ".bak");
            }
            else
            {
                bi.BackupFileName = Path.Combine(dir, bi.TableName + "_" + bi.GuidFieldName + ".bak");
            }

            var columnPropertyList = DatabaseHelper.GetColumnPropertyInfo(bi.TableName, bi.ItemType);

            try
            {
                using (FileStream fs = new FileStream(bi.BackupFileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
                    using (TextWriter tw = new StreamWriter(fs, Encoding.Unicode))
                    {
                        Type blobType   = typeof(object);
                        Type stringType = typeof(string);
                        Type doubleType = typeof(double);
                        int  recIndex   = 0;
                        do
                        {
                            foreach (object backupObj in bi.BackupObjects)
                            {
                                int count = 0;
                                foreach (PropertyInfo pi in columnPropertyList)
                                {
                                    bool   writeContent = true;
                                    object val          = pi.GetValue(backupObj, null);
                                    if (pi.PropertyType == stringType)
                                    {
                                        if (val != null && string.IsNullOrEmpty(val as string))
                                        {
                                            val = "\0";
                                        }
                                    }
                                    else if (pi.PropertyType == doubleType)
                                    {
                                        val = string.Format(CultureInfo.InvariantCulture, "{0:R}", val);
                                    }
                                    else if (pi.PropertyType == blobType && val is byte[])
                                    {
                                        byte[] array = val as byte[];
                                        EncodingOperateHelper.ToHexString(array, tw);
                                        writeContent = false;
                                    }
                                    if (writeContent)
                                    {
                                        tw.Write(val);
                                    }
                                    if (++count < columnPropertyList.Count)
                                    {
                                        tw.Write(",\0");
                                    }
                                }
                                tw.WriteLine();
                            }

                            bi.RecordsNum -= bi.BackupObjects.Count;
                            recIndex      += bi.BackupObjects.Count;
                            bi.BackupObjects.Clear();
                            if (bi.RecordsNum > 0 && !string.IsNullOrEmpty(bi.DumpQuery))
                            {
                                bi.BackupObjects = DatabaseHelper.GetBoundedEntities(bi.DumpQuery, BackupItemEx.BatchNum, recIndex);
                            }
                            else
                            {
                                break;
                            }
                        } while (bi.RecordsNum > 0);
                    }
            }
            catch (System.Exception ex)
            {
                LogHelper.Log(ex);
            }
        }