private async Task <bool> StoreFile(Stream fileStream, byte[] hash)
        {
            if (fileStream == null || hash == null || hash.Length != 20)
            {
                throw new ArgumentException();
            }

            var subdir          = Hasher.GetDirectoryNameFromHash(hash);
            var storageLocation = new BackupDirectory(Path.Combine(StorageDirectory.FullName, subdir));

            if (!storageLocation.Exists)
            {
                storageLocation.Create();
            }

            var fileName    = Hasher.GetFileNameFromHash(hash);
            var outFilePath = Path.Combine(storageLocation.FullName, fileName);

            using (var outStream = File.OpenWrite(outFilePath))
                using (var deflateStream = new DeflateStream(outStream, CompressionMode.Compress))
                {
                    fileStream.CopyTo(deflateStream);
                }

            //Validate the file??
            //var hashCheck = Hasher.GetFileHash(file);

            return(await Task.FromResult(true));
        }
예제 #2
0
        private static List <BackupDetail> ServerBackupDetails(BackupDirectory backupDirectory)
        {
            string serverDir = Path.Combine(ServerDir(), backupDirectory.ToString());

            List <BackupDetail> serverBackupDetails = new List <BackupDetail>();

            foreach (string file in Directory.GetFiles(serverDir))
            {
                var fileBackupDetail = new BackupDetail();
                fileBackupDetail.SavedName       = Path.GetFileName(file);
                fileBackupDetail.ActualName      = fileBackupDetail.SavedName;
                fileBackupDetail.FileSystemType  = FileSystemType.File;
                fileBackupDetail.BackupDirectory = backupDirectory;
                fileBackupDetail.MD5CheckSum     = CalculateMD5(file);
                serverBackupDetails.Add(fileBackupDetail);
            }
            foreach (string dir in Directory.GetDirectories(serverDir))
            {
                var directoryBackupDetail = new BackupDetail();
                directoryBackupDetail.SavedName       = Path.GetFileName(dir);
                directoryBackupDetail.ActualName      = directoryBackupDetail.SavedName;
                directoryBackupDetail.FileSystemType  = FileSystemType.Directory;
                directoryBackupDetail.BackupDirectory = backupDirectory;
                serverBackupDetails.Add(directoryBackupDetail);
            }

            return(serverBackupDetails);
        }
예제 #3
0
        /// <summary>
        ///     Performs a global backup action, copying all files in the SavesDirectory to the BackupDirectory.
        /// </summary>
        public static void Backup()
        {
            if (Busy)
            {
                ToConsole("Could not perform backup action, the service is busy.");
                return;
            }

            Busy = true;

            DateTime now = DateTime.UtcNow;

            ToConsole(String.Empty);
            ToConsole("Backup action started...");

            TryCatch(
                () =>
            {
                DateTime expire = DateTime.UtcNow.Subtract(TimeSpan.FromDays(3.0));

                Parallel.ForEach(
                    BackupDirectory.EnumerateDirectories(),
                    dir => TryCatch(
                        () =>
                {
                    if (dir.CreationTimeUtc < expire)
                    {
                        dir.Delete(true);
                    }
                }));
            });

            TryCatch(
                () =>
            {
                var target = IOUtility.EnsureDirectory(
                    BackupDirectory + "/" + DateTime.Now.ToSimpleString("D d M y - t@h-m@"), true);

                Parallel.ForEach(
                    SavesDirectory.EnumerateDirectories(),
                    dir =>
                    TryCatch(
                        () =>
                        dir.CopyDirectory(IOUtility.EnsureDirectory(dir.FullName.Replace(SavesDirectory.FullName, target.FullName)))));
            });

            if (OnBackup != null)
            {
                TryCatch(OnBackup, ToConsole);
            }

            Busy = false;

            double time = (DateTime.UtcNow - now).TotalSeconds;

            ToConsole("Backup action completed in {0:F2} second{1}", time, (time != 1) ? "s" : String.Empty);
        }
예제 #4
0
        /// <summary>
        ///     Performs a global backup action, copying all files in the SavesDirectory to the BackupDirectory.
        /// </summary>
        public static void Backup()
        {
            if (Busy)
            {
                ToConsole("Could not perform backup action, the service is busy.");
                return;
            }

            Busy = true;

            var now = DateTime.UtcNow;

            try
            {
                ToConsole(String.Empty);
                ToConsole("Backup action started...");

                if (BackupExpireAge > TimeSpan.Zero)
                {
                    ToConsole("Backup Expire Age: {0}", BackupExpireAge);

                    lock (IOLock)
                    {
                        BackupDirectory.EmptyDirectory(BackupExpireAge);
                    }
                }

                lock (IOLock)
                {
                    SavesDirectory.CopyDirectory(
                        IOUtility.EnsureDirectory(BackupDirectory + "/" + DateTime.Now.ToSimpleString("D d M y"), true));
                }

                InvokeByPriority(OnBackup);
            }
            finally
            {
                Busy = false;
            }

            var time = (DateTime.UtcNow - now).TotalSeconds;

            ToConsole("Backup action completed in {0:F2} second{1}", time, (time != 1) ? "s" : String.Empty);
        }
예제 #5
0
파일: VitaNex.cs 프로젝트: LordEnigma/UO
 private static void FlushExpired()
 {
     BackupDirectory.EmptyDirectory(TimeSpan.FromDays(3.0));
 }