public void BackupDatabase(IWrapperProvider wrapperProvider, string path)
        {
            IPathWrapper      pathWrapper      = wrapperProvider.GetWrapper <IPathWrapper>();
            IDirectoryWrapper directoryWrapper = wrapperProvider.GetWrapper <IDirectoryWrapper>();
            IFileWrapper      fileWrapper      = wrapperProvider.GetWrapper <IFileWrapper>();
            //copy data.xml to data.xml.bak
            //copy data.xml to data_date.bak - it will override last dayily backup. first run of day will create new one
            string backupFileName = pathWrapper.GetFileNameWithoutExtension(path) + DateTime.Now.ToString("yyyyMMdd");
            string backupPath     = pathWrapper.Combine(
                pathWrapper.GetDirectoryName(path),
                backupFileName);

            backupPath = pathWrapper.ChangeExtension(backupPath, pathWrapper.GetExtension(path));

            bool backupExists = File.Exists(backupPath);

            File.Copy(path, backupPath, true);

            if (!backupExists)
            {
                ManageBackups(path, pathWrapper, directoryWrapper, fileWrapper);
            }
        }
        public void ManageBackups(
            string path,
            IPathWrapper pathWrapper,
            IDirectoryWrapper directoryWrapper,
            IFileWrapper fileWrapper)
        {
            string dataFileFilter = Path.ChangeExtension(
                string.Format("{0}*", Path.GetFileNameWithoutExtension(DataFile)),
                Path.GetExtension(DataFile));
            var backupFiles = directoryWrapper.EnumerateFiles(
                pathWrapper.GetDirectoryName(path),
                dataFileFilter).Where(f => f != FullDataFilePath).OrderByDescending(f => f);
            //first save of day - delete old backups
            int backupcount = backupFiles.Count();
            int skipfiles   = 7; //backups to keep

            if (backupcount > skipfiles)
            {
                foreach (string s in backupFiles.Skip(skipfiles))
                {
                    fileWrapper.Delete(s);
                }
            }
        }
Esempio n. 3
0
 private void CreateDirectoryIfNotExists(string path)
 {
     _directoryWrapper.CreateDirectory(_pathWrapper.GetDirectoryName(path));
 }