コード例 #1
0
        /// <summary>
        /// Backup a file, using the SIS principle: if the file exists already in the previous backup,
        /// create a hardlink instead of making another copy.
        /// </summary>
        /// <param name="fileToBackup"> Full path to the file to backuped </param>
        /// <param name="fileToBackupTo"> Base directory to backup to </param>
        /// <param name="BaseDirPrevBackup"> Base directory of the previous backup </param>
        /// <returns> true if the file was actualy copied, false if the file didn't change since a previous backup </returns>
        private Boolean BackupFile(FileInfo fileToBackup, FileInfo fileToBackupTo, FileInfo prevBackupFile)
        {
            Boolean fileChanged = false;

            try
            {
                // Check if the file was backed up in the previous backup and wasn't changed afterwards,
                // to create a hardlink rather then copying the file again.
                if (prevBackupFile.Exists && IOHelper.Compare(prevBackupFile, fileToBackup, false))
                {
                    fileChanged = false;

                    if (OnlySimulate != true)
                    {
                        IOHelper.CreateHardLink(fileToBackupTo.FullName, prevBackupFile.FullName);
                    }
                    Log.Debug("HARDLINK " + fileToBackupTo.FullName + " TO " + prevBackupFile + " (= previous backup");
                }
                else
                {
                    fileChanged = true;

                    if (OnlySimulate != true)
                    {
                        File.Copy(fileToBackup.FullName, fileToBackupTo.FullName);
                    }
                    Log.Debug("COPY " + fileToBackup + " TO " + fileToBackupTo.FullName);

                    if (prevBackupFile.Exists)
                    {
                        ReportToFile("CHANGED - " + fileToBackupTo.FullName);
                    }
                    else
                    {
                        ReportToFile("NEW     - " + fileToBackupTo.FullName);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(fileToBackup + " TO " + fileToBackupTo.FullName + ": " + ex.Message);
            }

            return(fileChanged);
        }