How to backup bulk images through code, uploaded by clients on a windows cloud server?
Loading
How to backup bulk images through code, uploaded by clients on a windows cloud server?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jithu ThomasRank #437 · 5.7K reputationPosted Feb 10, 2024, 1:00 AM EST
you can achieve this using a C# console applicaiton on the windows server: -
using System; using System.IO; using System.Linq; class Program { static void Main() { string sourceFolderPath = @"C:\Path\To\Client\Uploaded\Images"; string backupFolderPath = @"C:\Path\To\Backup\Location"; BackupImages(sourceFolderPath, backupFolderPath); } static void BackupImages(string sourceFolderPath, string backupFolderPath) { try { // Ensure the source folder exists if (!Directory.Exists(sourceFolderPath)) { Console.WriteLine($"Source folder '{sourceFolderPath}' does not exist."); return; } // Create a timestamp for the backup folder string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss"); string backupFolderName = $"backup_{timestamp}"; // Create the backup folder string backupPath = Path.Combine(backupFolderPath, backupFolderName); Directory.CreateDirectory(backupPath); // Copy files from the source folder to the backup folder string[] filesToBackup = Directory.GetFiles(sourceFolderPath); foreach (string filePath in filesToBackup) { string fileName = Path.GetFileName(filePath); string destinationPath = Path.Combine(backupPath, fileName); File.Copy(filePath, destinationPath); } Console.WriteLine($"Backup completed. Images are stored in: {backupPath}"); } catch (Exception ex) { Console.WriteLine($"Error during backup: {ex.Message}"); } } }Shweta Varshney98 reputationPosted Feb 10, 2024, 3:16 AM EST
Is it possible to backup from cloud windows server to my laptop or external harddisk?
Jithu ThomasRank #437 · 5.7K reputationPosted Feb 10, 2024, 3:25 AM EST
YES!!! It is possible... it can be done in many ways as follows: -
Cloud Storage Services
Remote Desktop Connection
FTP
Backup Software:
VPN or Direct Connection:
Using Powershell/Commond promopt: -
/E: Copies subdirectories, including empty ones./Z: Uses restartable mode. If the copy is interrupted,robocopywill resume where it left off./COPYALL: Copies all file information, including attributes, timestamps, and security information./R:3: Specifies the number of retries on failed copies (adjust as needed)./W:2: Specifies the wait time between retries in seconds (adjust as needed).Make sure to replace
\\cloud-server\Share\Folderwith the actual path on your cloud server and"E:\Backup"with the path on your local laptop or external hard disk.# Define your cloud server source path $sourcePath = "\\cloud-server\Share\Folder" # Define your local destination path (e.g., external hard disk) $destinationPath = "E:\Backup" # Use robocopy to copy files from the cloud server to your local destination robocopy $sourcePath $destinationPath /E /Z /COPYALL /R:3 /W:2 # Optionally, display a message indicating the completion of the backup Write-Host "Backup completed!"Shweta Varshney98 reputationPosted Feb 17, 2024, 7:11 AM EST
Shweta Varshney98 reputationPosted Feb 22, 2024, 2:40 AM EST
I used bash command but it give error command not found
#! /bin/bash
$sourcePath = "\\cloud server\shared\folder"
$destinationPath = "E:\backup"
robocopy $sourcePath $destinationPath /E /Z /COPYALL /R:3 /W:2
Write-Host "Backup completed!"
What should I do? Please suggest.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 11, 2024, 6:40 AM EDT
C# console application for backing up images looks well-structured and functional.
While you have a general catch block to handle exceptions, it's better to handle specific exceptions individually to provide more meaningful error messages. For instance, you can handle IOException, UnauthorizedAccessException, etc., separately. If the backup folder already contains a file with the same name as the file being copied, it will be overwritten. You might want to check if the file exists in the destination folder and decide whether to overwrite it, skip it, or rename it.
using System; using System.IO; class Program { static void Main() { string sourceFolderPath = @"C:\Path\To\Client\Uploaded\Images"; string backupFolderPath = @"C:\Path\To\Backup\Location"; BackupImages(sourceFolderPath, backupFolderPath); } static void BackupImages(string sourceFolderPath, string backupFolderPath) { try { if (!Directory.Exists(sourceFolderPath)) { Console.WriteLine($"Source folder '{sourceFolderPath}' does not exist."); return; } string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss"); string backupFolderName = $"backup_{timestamp}"; string backupPath = Path.Combine(backupFolderPath, backupFolderName); if (Directory.Exists(backupPath)) { Console.WriteLine($"Backup folder '{backupPath}' already exists. Creating a new folder with a different name."); backupPath = GetUniqueBackupFolderName(backupFolderPath, timestamp); Directory.CreateDirectory(backupPath); } else { Directory.CreateDirectory(backupPath); } string[] filesToBackup = Directory.GetFiles(sourceFolderPath); foreach (string filePath in filesToBackup) { string fileName = Path.GetFileName(filePath); string destinationPath = Path.Combine(backupPath, fileName); if (File.Exists(destinationPath)) { Console.WriteLine($"File '{fileName}' already exists in the backup folder. Skipping..."); continue; } File.Copy(filePath, destinationPath); } Console.WriteLine($"Backup completed. Images are stored in: {backupPath}"); } catch (UnauthorizedAccessException) { Console.WriteLine("Access to a file or folder is denied. Check permissions."); } catch (IOException ex) { Console.WriteLine($"An I/O error occurred: {ex.Message}"); } catch (Exception ex) { Console.WriteLine($"Error during backup: {ex.Message}"); } } static string GetUniqueBackupFolderName(string backupFolderPath, string timestamp) { int counter = 1; string backupPath; do { string backupFolderName = $"backup_{timestamp}_{counter}"; backupPath = Path.Combine(backupFolderPath, backupFolderName); counter++; } while (Directory.Exists(backupPath)); return backupPath; } }Tuhin PaulRank #7 · 71.9K reputationPosted Mar 11, 2024, 6:41 AM EDT
You're currently checking if the source folder exists, which is good. However, you may also want to check if the backup folder exists and handle scenarios where it already exists or create a new folder with a different timestamp.