예제 #1
0
        public static DateTime GetMostRecentBackup(string sourceFilePath)
        {
            DirectoryInfo backupFolder         = FileBackup.GetOrCreateBackupFolder(sourceFilePath);
            FileInfo      mostRecentBackupFile = FileBackup.GetBackupFiles(backupFolder, sourceFilePath).OrderByDescending(file => file.LastWriteTimeUtc).FirstOrDefault();

            if (mostRecentBackupFile != null)
            {
                return(mostRecentBackupFile.LastWriteTimeUtc);
            }
            return(DateTime.MinValue.ToUniversalTime());
        }
예제 #2
0
        // Full version: Copy or move file to backup version with separated path/source file name
        public static bool TryCreateBackup(string folderPath, string sourceFileName, bool moveInsteadOfCopy)
        {
            string sourceFilePath = Path.Combine(folderPath, sourceFileName);

            if (File.Exists(sourceFilePath) == false)
            {
                // nothing to do
                return(false);
            }

            // create backup folder if needed
            DirectoryInfo backupFolder = FileBackup.GetOrCreateBackupFolder(sourceFilePath);

            // create a timestamped copy of the file
            // file names can't contain colons so use non-standard format for timestamp with dashes for 24 hour-minute-second separation
            string sourceFileNameWithoutExtension = Path.GetFileNameWithoutExtension(sourceFileName);
            string sourceFileExtension            = Path.GetExtension(sourceFileName);
            string destinationFileName            = String.Concat(sourceFileNameWithoutExtension, ".", DateTime.Now.ToString("yyyy-MM-dd.HH-mm-ss"), sourceFileExtension);
            string destinationFilePath            = Path.Combine(backupFolder.FullName, destinationFileName);

            try
            {
                if (moveInsteadOfCopy)
                {
                    File.Move(sourceFilePath, destinationFilePath);
                }
                else
                {
                    File.Copy(sourceFilePath, destinationFilePath, true);
                }
            }
            catch
            {
                // Old code: We just don't create the backup now. While we previously threw an exception, we now test and warn the user earlier on in the code that a backup can't be made
                // System.Diagnostics.Debug.Print("Did not back up" + destinationFilePath);
                // throw new PathTooLongException("Backup failure: Could not create backups as the file path is too long", e);
                return(false);
            }

            // age out older backup files
            IEnumerable <FileInfo> backupFiles = FileBackup.GetBackupFiles(backupFolder, sourceFilePath).OrderByDescending(file => file.LastWriteTimeUtc);

            foreach (FileInfo file in backupFiles.Skip(Constant.File.NumberOfBackupFilesToKeep))
            {
                File.Delete(file.FullName);
            }
            return(true);
        }
예제 #3
0
 public static DateTime GetMostRecentBackup(string sourceFilePath)
 {
     try
     {
         DirectoryInfo backupFolder         = FileBackup.GetOrCreateBackupFolder(sourceFilePath);
         FileInfo      mostRecentBackupFile = null;
         if (backupFolder != null)
         {
             mostRecentBackupFile = FileBackup.GetBackupFiles(backupFolder, sourceFilePath, false).OrderByDescending(file => file.LastWriteTime).FirstOrDefault();
         }
         if (backupFolder != null && mostRecentBackupFile != null)
         {
             return(mostRecentBackupFile.LastWriteTime);
         }
         return(DateTime.MinValue);
     }
     catch
     {
         return(DateTime.MinValue);
     }
 }
예제 #4
0
        // Full version: Copy or move file to backup version with separated path/source file name
        // If specialBackup is non-empty, it creates a special checkpointfile, usually to flag a non=-backwards compatable upgrade to the tdb and ddb files
        public static bool TryCreateBackup(string folderPath, string sourceFileName, bool moveInsteadOfCopy, string specialBackup)
        {
            string sourceFilePath = Path.Combine(folderPath, sourceFileName);

            if (File.Exists(sourceFilePath) == false)
            {
                // nothing to do
                return(false);
            }

            // create backup folder if needed
            DirectoryInfo backupFolder = FileBackup.GetOrCreateBackupFolder(sourceFilePath);

            if (backupFolder == null)
            {
                // Something went wrong...
                return(false);
            }
            // create a timestamped copy of the file
            // file names can't contain colons so use non-standard format for timestamp with dashes for 24 hour-minute-second separation
            // If there is a specialBackup term, then we modify anadd it before the timestamp
            string sourceFileNameWithoutExtension = Path.GetFileNameWithoutExtension(sourceFileName);
            string sourceFileExtension            = Path.GetExtension(sourceFileName);

            specialBackup = specialBackup == String.Empty
                ? String.Empty
                : Constant.File.BackupCheckpointIndicator + specialBackup;
            string destinationFileName = String.Concat(sourceFileNameWithoutExtension, specialBackup, ".", DateTime.Now.ToString("yyyy-MM-dd.HH-mm-ss"), sourceFileExtension);
            string destinationFilePath = Path.Combine(backupFolder.FullName, destinationFileName);

            try
            {
                if (File.Exists(destinationFilePath) && new System.IO.FileInfo(destinationFilePath).Attributes.HasFlag(System.IO.FileAttributes.ReadOnly))
                {
                    // Can't overwrite it...
                    return(false);
                }
                if (moveInsteadOfCopy)
                {
                    File.Move(sourceFilePath, destinationFilePath);
                }
                else
                {
                    File.Copy(sourceFilePath, destinationFilePath, true);
                }
            }
            catch
            {
                // Old code: We just don't create the backup now. While we previously threw an exception, we now test and warn the user earlier on in the code that a backup can't be made
                // System.Diagnostics.Debug.Print("Did not back up" + destinationFilePath);
                // throw new PathTooLongException("Backup failure: Could not create backups as the file path is too long", e);
                return(false);
            }

            // age out older backup files (this skips the special checkpoint files)
            IEnumerable <FileInfo> backupFiles = FileBackup.GetBackupFiles(backupFolder, sourceFilePath, true).OrderByDescending(file => file.LastWriteTimeUtc);

            if (backupFiles == null)
            {
                // We can't delete older backups, but at least we were able to create a backup.
                return(true);
            }
            foreach (FileInfo file in backupFiles.Skip(Constant.File.NumberOfBackupFilesToKeep))
            {
                File.Delete(file.FullName);
            }
            return(true);
        }