/// <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));
        }
Example #2
0
        // Synchronises with Azure Blob Storage
        public void SyncAzureStorage()
        {
            var common = new Common();

            var localFiles = common.GetLocalFiles(m_syncEncryptedFilesOnly);

            var blobItems = m_azureStorageHelper.GetBlobItems();

            var syncHelper = new SyncHelper(m_remoteItemClass, m_isDebug);

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

                if (delete.Count > 0)
                {
                    syncHelper.DeleteFromAzureStorage(delete.Select(d => d), m_syncEncryptedFilesOnly);
                    // Get new blobItems list
                    blobItems = m_azureStorageHelper.GetBlobItems();
                }
            }

            var remoteFetchHelper = new RemoteFetchHelper(m_remoteItemClass);

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

            var filesToDeleteOnRemoteStorage = new List <RemoteItem>(blobItems.Count);
            var filesToCopyToRemoteStorage   = new List <FileInfo>(localFiles.Count);
            var filesToCopyFromRemoteStorage = new List <RemoteItem>(blobItems.Count);

            // Exists on RemoteStorage, does not exist on local - fetch from Remote Storage
            foreach (var azureFile in blobItems)
            {
                var localMatch = m_isRunningOnWindows ? azureFile.Name.Replace(@"\", @"/") : azureFile.Name;

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

                if (localFile.Key != null && localFile.Value != null && localFile.Value.Contains(localMatch))
                {
                    if (localFile.Key.Length != azureFile.Size)
                    {
                        filesToCopyFromRemoteStorage.Add(azureFile);
                    }
                }
                else
                {
                    filesToCopyFromRemoteStorage.Add(azureFile);
                }
            }

            // Exists on RemoteStorage, does not exist on local - delete from RemoteStorage
            filesToDeleteOnRemoteStorage.AddRange(from azureFile in blobItems
                                                  where !localFiles.ContainsValue(m_isRunningOnWindows ? azureFile.Name.Replace(@"\", @"/") : azureFile.Name)
                                                  select azureFile);

            // Exists on local, does not exist on RemoteStorage - copy to RemoteStorage
            filesToCopyToRemoteStorage.AddRange(from localFile in localFiles
                                                where !blobItems.Exists(x => (m_isRunningOnWindows ? x.Name.Replace(@"/", @"\") : x.Name) == localFile.Value)
                                                select localFile.Key);

            if (m_isDebug)
            {
                return;
            }

            if (m_copyFilesToRemoteStorage)
            {
                syncHelper.CopyToAzureStorage(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(blobItem => blobItem.Name.Contains(m_explicitFilesToFetchMatchingString))
                                                : filesToCopyFromRemoteStorage, m_itemSortOrder);
            }

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