Exemplo n.º 1
0
        /// <summary>
        /// Fetches the files from Remote Storage for the restore script.
        /// </summary>
        /// <param name="database">The database.</param>
        /// <param name="sortOrder">The sort order.</param>
        /// <returns></returns>
        public StringBuilder FetchFilesForRestoreScript(string database, ItemSortOrder sortOrder)
        {
            var remoteItems = m_remoteItemClass == ItemClass.Blob
                                ? m_azureHelper.GetBlobItems()
                                : m_fileHelper.GetFileItems();

            Console.WriteLine("Number of items found in Remote Storage: {0}", remoteItems.Count);
            Console.WriteLine();

            Console.WriteLine("Starting file review ...");

            var filesToFetch = ParseLatestBackup(remoteItems, database);

            var rfh = new RemoteFetchHelper(m_remoteItemClass);

            // Actually fetch the files
            var files = rfh.FetchItemsFromRemoteStorage(filesToFetch, sortOrder);

            // Generate the script and return it
            return(BuildRestoreScript(files));
        }
Exemplo n.º 2
0
        // Synchronises with Remote File Storage
        public void SyncFileStorage()
        {
            var fileHelper = new FileStorageHelper();

            var localFiles = GetLocalFiles(m_syncEncryptedFilesOnly);
            // var localFileNames = localFiles.Select(localFile => localFile.FullName.Replace(m_localDirectory.Name, string.Empty)).ToList();

            var remoteFiles = fileHelper.GetFileItems();

            var syncHelper = new SyncHelper(m_remoteItemClass, m_isDebug);

            if (m_deleteExplicitFilesFromRemoteStorage && !string.IsNullOrEmpty(m_explicitFilesToDeleteMatchingString))
            {
                // Now only explicitly delete files from Remote File Storage if this setting is set
                var delete = remoteFiles.Where(x => x.Name.Contains(m_explicitFilesToDeleteMatchingString)).ToList();

                if (delete.Count > 0)
                {
                    syncHelper.DeleteFromFileStorage(delete, m_syncEncryptedFilesOnly);
                    // Get new FileInfos list
                    remoteFiles = fileHelper.GetFileItems();
                }
            }

            var remoteFetchHelper = new RemoteFetchHelper(m_remoteItemClass);

            Console.WriteLine("Starting file comparison ...");

            var filesToDeleteRemotely        = new List <RemoteItem>(remoteFiles.Count);
            var filesToDeleteLocally         = new List <FileInfo>(localFiles.Count);
            var filesToCopyToRemoteStorage   = new List <FileInfo>(localFiles.Count);
            var filesToCopyFromRemoteStorage = new List <RemoteItem>(remoteFiles.Count);

            // Exists on Remote Path, does not exist on local - fetch from Remote Path
            foreach (var remoteFile in remoteFiles)
            {
                var localMatch = remoteFile.Name;

                var localFile = localFiles.FirstOrDefault(x => x.Name == localMatch);

                if (localFile != null && localFile.Name.Contains(localMatch))
                {
                    if (localFile.Length != remoteFile.Size)
                    {
                        filesToCopyFromRemoteStorage.Add(remoteFile);
                    }
                }
                else
                {
                    filesToCopyFromRemoteStorage.Add(remoteFile);
                }
            }

            // Exists on File Path, does not exist on local - delete from File Path
            filesToDeleteRemotely.AddRange(from remoteFile in remoteFiles
                                           where !localFiles.Exists(x => x.Name == remoteFile.Name)
                                           select remoteFile);

            // Exists on local, does not exist on File Path - delete from local
            filesToDeleteLocally.AddRange(from localFile in localFiles
                                          where !remoteFiles.Exists(x => x.Name == localFile.Name)
                                          select localFile);

            // Exists on local, does not exist on File Path - copy to File Path
            filesToCopyToRemoteStorage.AddRange(from localFile in localFiles
                                                where !remoteFiles.Exists(x => x.Name == localFile.Name)
                                                select localFile);

            if (m_copyFilesToRemoteStorage)
            {
                syncHelper.CopyToFileStorage(filesToCopyToRemoteStorage);
            }

            if (m_fetchFilesFromRemoteStorage)
            {
                // If explicit matching string is set for files to fetch,
                // then fetch those only, otherwise fetch everything
                remoteFetchHelper.FetchItemsFromRemoteStorage(
                    m_fetchExplicitFilesFromRemoteStorage
                                                ? filesToCopyFromRemoteStorage.Where(remoteFile => remoteFile.Name.Contains(m_explicitFilesToFetchMatchingString))
                                                : filesToCopyFromRemoteStorage, m_itemSortOrder);
            }

            if (m_deleteMissingFilesFromLocalStorage)
            {
                syncHelper.DeleteFromLocalStorage(filesToDeleteLocally, m_syncEncryptedFilesOnly);
            }
            else
            {
                Console.WriteLine("No files will be deleted from Local File Storage.");
            }

            if (m_deleteMissingFilesFromRemoteStorage)
            {
                syncHelper.DeleteFromFileStorage(filesToDeleteRemotely, m_syncEncryptedFilesOnly);
            }
            else
            {
                Console.WriteLine("No files will be deleted from Remote File Storage.");
            }
        }