Exemplo n.º 1
0
        private void BackupDatabase()
        {
            try
            {
                Config config = _dtCtrl.GetConfig();

                if (config.ScheduledBackup)
                {
                    if (config.BackupPeriod == 1 || config.BackupPeriod == 7 && (int)DateTime.Today.DayOfWeek == config.BackupDay)
                    {
                        int timeDiff = Util.CompareTime(config.BackupTime, DateTime.Now);

                        if (timeDiff <= Config.TimeBound) //in allowed range
                        {
                            string fileName = "FaceIDBK" + DateTime.Now.Ticks + ".mdb";

                            if (config.BackupFolder.EndsWith(@"\") == false)
                            {
                                config.BackupFolder += @"\";
                            }

                            string filePath = config.BackupFolder + fileName;

                            _dtCtrl = Properties.Settings.Default.IsClient ? RemoteDataController.Instance : LocalDataController.Instance;
                            lock (_dtCtrl)
                            {
                                _dtCtrl.BackupDatabase(filePath);
                            }

                            //sleep for 23 hours
                            Thread.Sleep(_timetoSleep3);
                            BackupDatabase();
                        }
                        else //sleep until the right time
                        {
                            if (timeDiff < 0)
                            {
                                timeDiff += TimeSpan.FromDays(1).Seconds;
                            }

                            Thread.Sleep(Convert.ToInt32(TimeSpan.FromSeconds(timeDiff).TotalMilliseconds));
                            BackupDatabase();
                        }
                    }
                }
            }
            catch
            {
                //TODO
                //Exception: another process is accessing db at the time

                Thread.Sleep(_timeToSleep1);
                BackupDatabase();
            }
        }
        private void btnBackup_Click(object sender, EventArgs e)
        {
            //get backup destination
            string fileName     = "BK" + DateTime.Now.Ticks + ".mdb";
            string backupFolder = txtBackupFolder.Text;

            if (Directory.Exists(backupFolder) == false)
            {
                MessageBox.Show("Folder " + backupFolder + " not found.");
                return;
            }

            if (backupFolder.EndsWith(@"\") == false)
            {
                backupFolder += @"\";
            }

            string backupPath = backupFolder + fileName;

            //backup db
            try
            {
                _dtCtrl.BackupDatabase(backupPath);

                Config config = _dtCtrl.GetConfig();
                config.LastestBackup     = DateTime.Now;
                config.LastestBackupFile = backupPath;
                _dtCtrl.UpdateConfig(config);

                MessageBox.Show("Backup Complete.");
            }
            catch (Exception ex)
            {
                MessageBox.Show("There has been an error. Please try again later. Error detail: " + ex.Message);
            }
        }
Exemplo n.º 3
0
        private void BackupDatabase()
        {
            try
            {
                Config config = _dtCtrl.GetConfig();

                if (config.ScheduledBackup)
                {
                    if (config.BackupPeriod == 1 || config.BackupPeriod == 7 && (int)DateTime.Today.DayOfWeek == config.BackupDay)
                    {
                        int timeDiff = Util.CompareTime(config.BackupTime, DateTime.Now);

                        if (timeDiff <= Config.TimeBound) //in allowed range
                        {
                            string fileName = "FaceIDBK" + DateTime.Now.Ticks + ".mdb";

                            if (config.BackupFolder.EndsWith(@"\") == false)
                                config.BackupFolder += @"\";

                            string filePath = config.BackupFolder + fileName;

                            _dtCtrl = Properties.Settings.Default.IsClient ? RemoteDataController.Instance : LocalDataController.Instance;
                            lock (_dtCtrl)
                            {
                                _dtCtrl.BackupDatabase(filePath);
                            }

                            //sleep for 23 hours
                            Thread.Sleep(_timetoSleep3);
                            BackupDatabase();
                        }
                        else //sleep until the right time
                        {
                            if (timeDiff < 0)
                                timeDiff += TimeSpan.FromDays(1).Seconds;

                            Thread.Sleep(Convert.ToInt32(TimeSpan.FromSeconds(timeDiff).TotalMilliseconds));
                            BackupDatabase();
                        }
                    }
                }
            }
            catch
            {
                //TODO
                //Exception: another process is accessing db at the time

                Thread.Sleep(_timeToSleep1);
                BackupDatabase();
            }
        }