Categories
Programming Uncategorized

SFTP/FTP Project uploader

This program uploads files and directories from your local machine to a remote server using either SFTP or FTP, based on the protocol specified in the configuration file.

Download here: mProjectUploader.zip

Features

  • Supports both SFTP (Secure Shell) and FTP (File Transfer Protocol).
  • Automatically handles directory creation on the remote server.
  • Skip files or directories based on configurable patterns.
  • Multiple overwrite policies, including:
    • skipAll: Skip all existing files.
    • overwriteOlder: Overwrite files only if the remote file is older.
    • overwriteDifferentSize: Overwrite files if their sizes differ.
    • overwriteAll: Overwrite all files.
    • OverwriteKeepTrack: Overwrite only if the file content has changed (based on checksum).
  • Tracks skipped, changed, and uploaded files.

Usage Instructions

1. Prepare the Configuration File

The program uses an XML configuration file (e.g., config.xml) to define its behavior. Below is an example of the configuration file:

<config>
<protocol>sftp</protocol> <!-- Use 'sftp' or 'ftp' -->
<host>example.com</host>
<port>22</port> <!-- Default: 22 for SFTP, 21 for FTP -->
<username>your_username</username>
<password>your_password</password>
<destinationPath>/remote/destination/folder</destinationPath>
<overwritePolicy>OverwriteKeepTrack</overwritePolicy>
<skipList>
<file>*.tmp</file> <!-- Skip all .tmp files -->
<file>*.log</file> <!-- Skip all .log files -->
<directory>node_modules</directory> <!-- Skip this directory -->
</skipList>
<checksums>
<!-- Checksum tracking for OverwriteKeepTrack -->
<file path="local/path/to/file1.txt" checksum="abc123" />
<file path="local/path/to/file2.txt" checksum="def456" />
</checksums>
</config>

2. Run the Program

Run the program using the following command:

SftpUploader.exe <config_file_path>

For example:

SftpUploader.exe config.xml

If no configuration file path is provided, the program looks for config.xml in the current directory.


Configuration Parameters

General Settings

  • <protocol>: Specifies the transfer protocol. Options:
    • sftp: Secure Shell File Transfer Protocol.
    • ftp: File Transfer Protocol.
  • <host>: Remote server hostname or IP address.
  • <port>: Server port (default: 22 for SFTP, 21 for FTP).
  • <username>: Username for authentication.
  • <password>: Password for authentication.
  • <destinationPath>: Remote directory path(s). Multiple <destinationPath> entries are supported.

File Handling

  • <overwritePolicy>: Determines how existing files are handled. Options:
    • skipAll: Skip all files that exist on the server.
    • overwriteOlder: Overwrite files only if the remote file is older.
    • overwriteDifferentSize: Overwrite files if their sizes differ.
    • overwriteAll: Overwrite all files.
    • OverwriteKeepTrack: Overwrite files only if their content has changed. Uses checksums stored in the checksums section.

Skip List

  • <skipList>: Allows you to exclude specific files or directories from processing.
    • <file>: Wildcard patterns for files to skip (e.g., *.log, *.tmp).
    • <directory>: Names of directories to skip.

Checksum Tracking

  • <checksums>: Used with OverwriteKeepTrack to track file changes.
    • <file>: Each entry contains the local file path and its checksum.

Features Based on Protocol

SFTP

  • Fully secure file transfer using SSH.
  • Handles recursive directory creation via the CreateDirectoryRecursively method.

FTP

  • Classic FTP protocol with directory and file handling.
  • Uses System.Net.FtpWebRequest for file uploads and directory creation.

Example Scenarios

1. Upload All Files

<overwritePolicy>overwriteAll</overwritePolicy>

This policy overwrites all files on the remote server.

2. Skip All Files

<overwritePolicy>skipAll</overwritePolicy>

This policy skips all files that already exist on the server.

3. Upload Only Changed Files

<overwritePolicy>OverwriteKeepTrack</overwritePolicy>

This policy compares checksums and uploads only files that have changed.

4. Skip Specific Files and Folders

<skipList>
<file>*.tmp</file>
<directory>bin</directory>
</skipList>

This configuration skips all .tmp files and the bin directory.


Statistics

At the end of execution, the program outputs statistics about the operation:

  • Total Files: The total number of files considered.
  • Skipped Files: The number of files skipped based on the policy or skip list.
  • Changed Files: The number of files identified as changed (only for OverwriteKeepTrack).
  • Uploaded Files: The number of files successfully uploaded.

Example output:

Processing destination: /remote/destination/folder
Total Files: 50, Skipped Files: 20, Changed Files: 5, Uploaded Files: 25

Error Handling

  • If the configuration file is missing or invalid, the program will display an error and exit.
  • Errors during file uploads or directory creation are logged to the console but do not stop the program.

Dependencies

  • SFTP: Renci.SshNet library for secure file transfer.
  • FTP: Built-in System.Net.FtpWebRequest.

Troubleshooting

  1. Missing Configuration File: Ensure the configuration file exists at the specified path and follows the correct format.
  2. Protocol Not Supported: Verify the <protocol> field in the configuration file is set to ftp or sftp.
  3. Authentication Issues: Check the host, username, and password values in the configuration file.
  4. Skipped Files: Review the <skipList> and <overwritePolicy> settings to ensure they align with your expectations.

Leave a Reply

Your email address will not be published. Required fields are marked *