# MTC Sync Script

A bash automation tool for synchronizing project files from a remote server to your local machine with automatic VS Code SFTP configuration.

## Installation

1. Clone the project
2. Add it with symbolic link to all mtc. project folder and call it

```sh
ln -s ~/Documents/GitHub/mtc-project-sync-script/sync-project.sh . 
```

3. Call it with `./sync-project.sh` and enter needed credentials

## Overview

**MTC Sync Script** streamlines the process of downloading and setting up projects from remote servers. It automates:
- Creating compressed archives on the remote server
- Downloading files via SCP
- Extracting and organizing files locally
- Configuring VS Code for SFTP development

Perfect for developers working with remote projects who want a fast, one-command setup experience.

## Features

- ✅ **Interactive Setup** - Simple prompts for server and local paths
- ✅ **Automated Sync** - Compress, download, and extract in one command
- ✅ **VS Code Integration** - Auto-generates SFTP configuration
- ✅ **Error Checking** - Validates each step with helpful error messages
- ✅ **Cleanup** - Removes temporary files automatically
- ✅ **Colored Output** - Clear status messages with color coding

## Requirements

### System Dependencies
- **Bash** shell
- **SSH/SCP** - Secure shell commands (usually pre-installed on macOS/Linux)
- **sshpass** - Password-based SSH authentication tool
- **tar** - Archive/compression utility (pre-installed)

### Installation of Dependencies

**macOS** (using Homebrew):
```bash
brew install sshpass
```

**Ubuntu/Debian**:
```bash
sudo apt-get install sshpass openssh-client
```

**CentOS/RHEL**:
```bash
sudo yum install sshpass openssh-clients
```

### Server Requirements
- SSH access to the remote server
- Project files in a known folder path (e.g., `~/public_leo`)

## Usage

### Basic Usage

1. **Make the script executable** (if needed):
   ```bash
   chmod +x sync-project.sh
   ```

2. **Run the script**:
   ```bash
   ./sync-project.sh
   ```

3. **Answer the prompts**:
   - **Server address**: Enter your server credentials (e.g., `user@server.com` or `leo@192.168.1.100`)
   - **Server project path**: Path where your project is stored on the server (e.g., `~/public_leo`)
   - **Local folder**: Where you want to download the project (e.g., `./my_project`)
   - **SSH password**: Your password when prompted

### Example Session

```bash
$ ./sync-project.sh

Enter server address (user@host): leo@myserver.com
Enter server project path: ~/public_leo
Enter local folder path: ./my_project
[System will prompt for SSH password]

✓ Archive created successfully
✓ File transfer complete
✓ Extraction complete
✓ VS Code SFTP config created
```

## What the Script Does

1. **Connects to Remote Server** - Uses SSH to authenticate with password or key
2. **Creates Archive** - Compresses project files into `tar.gz` format on server
3. **Downloads Archive** - Uses SCP to securely copy archive to local machine
4. **Extracts Files** - Unpacks the archive to your specified local folder
5. **Cleans Up** - Removes temporary archive files
6. **Configures VS Code** - Creates `.vscode/sftp.json` for seamless SFTP workflow

## VS Code SFTP Configuration

The script automatically generates a `.vscode/sftp.json` file with:
- **Auto-upload on save** - Changes sync to server immediately
- **Pre-configured ignore patterns** - Excludes common folders:
  - `node_modules/`
  - `vendor/`
  - `storage/`
  - `.vscode/`
  - `.git/`
  - And others...

This allows you to continue developing locally and have changes automatically uploaded to the remote server.

## Error Handling

The script includes error checking at each step:
- ✓ Validates SSH connection
- ✓ Checks if remote path exists
- ✓ Verifies archive creation
- ✓ Confirms successful download and extraction
- ✓ Exits gracefully with error messages if anything fails

## Troubleshooting

### "sshpass: command not found"
Install sshpass using the commands above for your OS.

### "SSH connection refused"
- Verify server address is correct
- Check that SSH is enabled on the server
- Confirm your username and password

### "Remote path not found"
- Verify the server project path is correct
- Ensure the path exists on the server
- Check user permissions

### "Local folder already exists"
The script will still sync, but existing files will be overwritten. Back up important files first.

## License

This script is provided as-is for development and synchronization purposes.

## Contributing

Feel free to improve this script or report issues!
