Files
mtc-project-sync-script/README.md

548 lines
14 KiB
Markdown
Raw Normal View History

2025-11-19 10:04:36 +02:00
# MTC Project Sync Script
A comprehensive automation toolkit for synchronizing and managing projects between remote servers and local development environments. Designed for developers working with remote servers who need efficient project setup and bidirectional file synchronization.
## Table of Contents
- [Overview](#overview)
- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Scripts Reference](#scripts-reference)
- [sync-project.sh](#sync-projectsh)
- [local-remote.sh](#local-remotesh)
- [remote-local.sh](#remote-localsh)
- [sftp-watch.sh](#sftp-watchsh)
- [build-watch.sh](#build-watchsh)
- [Configuration](#configuration)
- [Troubleshooting](#troubleshooting)
- [License](#license)
2025-11-18 12:36:14 +02:00
2025-10-22 10:50:38 +03:00
## Overview
2025-11-19 10:04:36 +02:00
**MTC Project Sync Script** is a bash-based automation suite that streamlines remote project development workflows. It handles initial project setup, ongoing file synchronization, and integrates seamlessly with VS Code for SFTP-based development.
### What It Does
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
- Downloads complete projects from remote servers with one command
- Configures VS Code SFTP settings automatically
- Provides helper scripts for continuous sync and build workflows
- Manages bidirectional file synchronization between local and remote environments
- Integrates lazygit for convenient version control
2025-10-22 10:50:38 +03:00
## Features
2025-11-19 10:04:36 +02:00
- **One-Command Setup** - Initialize and download remote projects instantly
- **VS Code Integration** - Auto-generates SFTP configuration with optimized ignore patterns
- **Bidirectional Sync** - Push local changes to server or pull remote changes to local
- **Watch Mode Support** - Automatically sync files on changes
- **Build Integration** - Watch and upload build artifacts automatically
- **Error Handling** - Comprehensive validation at each step
- **Clean Architecture** - Modular scripts for different workflows
- **Version Control Ready** - Includes lazygit and proper .gitignore setup
2025-10-22 10:50:38 +03:00
## Requirements
### System Dependencies
2025-11-19 10:04:36 +02:00
- **Bash** (4.0 or higher)
- **SSH/SCP** - Secure shell and copy utilities
- **sshpass** - Password-based SSH authentication
- **tar** - Archive utility
- **rsync** - Fast file synchronization tool
- **watchexec** (optional) - For watch mode functionality
2025-10-22 10:50:38 +03:00
### Installation of Dependencies
2025-11-19 10:04:36 +02:00
#### macOS (using Homebrew)
2025-10-22 10:50:38 +03:00
```bash
2025-11-19 10:04:36 +02:00
brew install sshpass rsync watchexec
2025-10-22 10:50:38 +03:00
```
2025-11-19 10:04:36 +02:00
#### Ubuntu/Debian
2025-10-22 10:50:38 +03:00
```bash
2025-11-19 10:04:36 +02:00
sudo apt-get install sshpass rsync openssh-client
# For watch functionality:
cargo install watchexec-cli
# or download from: https://github.com/watchexec/watchexec/releases
2025-10-22 10:50:38 +03:00
```
2025-11-19 10:04:36 +02:00
#### CentOS/RHEL
2025-10-22 10:50:38 +03:00
```bash
2025-11-19 10:04:36 +02:00
sudo yum install sshpass rsync openssh-clients
2025-10-22 10:50:38 +03:00
```
### Server Requirements
2025-11-19 10:04:36 +02:00
- SSH access with username/password or key-based authentication
- Sufficient permissions to create/read files in project directory
- tar utility installed on remote server
## Installation
### Method 1: Clone Repository
```bash
cd ~/Documents/GitHub
git clone <repository-url> mtc-project-sync-script
cd mtc-project-sync-script
chmod +x sync-project.sh
```
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
### Method 2: Create Symbolic Link (Recommended)
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
For easy access from any project directory:
```bash
# From your project directory
ln -s ~/Documents/GitHub/mtc-project-sync-script/sync-project.sh sync-project.sh
# Run the script
./sync-project.sh
```
This creates a symbolic link that allows you to use the script without copying it to every project.
## Quick Start
1. **Navigate to your desired project directory:**
2025-10-22 10:50:38 +03:00
```bash
2025-11-19 10:04:36 +02:00
mkdir my-project && cd my-project
2025-10-22 10:50:38 +03:00
```
2025-11-19 10:04:36 +02:00
2. **Create symbolic link or copy the script:**
```bash
ln -s ~/Documents/GitHub/mtc-project-sync-script/sync-project.sh .
```
3. **Run the sync script:**
2025-10-22 10:50:38 +03:00
```bash
./sync-project.sh
```
2025-11-19 10:04:36 +02:00
4. **Provide the requested information:**
- Server address (e.g., `username@server.com`)
- Server project path (e.g., `/home/username/public_html`)
- Local folder path (e.g., `./project-name`)
- SSH password when prompted
5. **Start developing!** The script will have:
- Downloaded all project files
- Created VS Code SFTP configuration
- Generated helper scripts for ongoing development
- Set up lazygit for version control
## Scripts Reference
### sync-project.sh
**Main synchronization script** - Downloads a complete project from a remote server and configures the local development environment.
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
#### Usage
```bash
./sync-project.sh
```
#### What It Does
1. **Prompts for Configuration:**
- Server credentials (user@host)
- Remote project path
- Local destination folder
- SSH password
2. **Creates Archive on Server:**
- Compresses the entire project directory into `leo.tar.gz`
- Stores archive in parent directory temporarily
3. **Downloads Archive:**
- Uses SCP to securely transfer the archive
- Shows progress during transfer
4. **Extracts Locally:**
- Unpacks the archive to specified local folder
- Preserves file permissions and structure
5. **Generates VS Code Configuration:**
- Creates `.vscode/sftp.json` with:
- Server connection details
- Auto-upload on save enabled
- Optimized ignore patterns
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
6. **Creates Helper Scripts:**
- Generates `local-remote.sh`, `remote-local.sh`, `sftp-watch.sh`, and `build-watch.sh`
- Pre-configures them with your server credentials
- Adds them to `.gitignore`
7. **Deploys lazygit:**
- Copies lazygit binary to local project
- Uploads lazygit to remote server
- Adds to `.gitignore`
8. **Cleanup:**
- Removes temporary archive files from both local and remote
#### Example Session
2025-10-22 10:50:38 +03:00
```bash
$ ./sync-project.sh
2025-11-19 10:04:36 +02:00
=== Project Sync Configuration ===
Server user@host (e.g., leo@server.com): john@myserver.com
Server project path (e.g., /home/leo/public_leo): /home/john/websites/shop
Local folder path (e.g., Delta Pharmacy): online-shop
SSH Password: [hidden]
Starting project sync...
Creating archive on server...
Archive created successfully
Copying archive to local machine...
Archive copied successfully
Extracting archive...
Archive extracted successfully
Cleaning up local archive...
Cleaning up server archive...
File sync complete!
.vscode/sftp.json created successfully
build-watch.sh created successfully
Added build-watch.sh to .gitignore
local-remote.sh created successfully
Added local-remote.sh to .gitignore
remote-local.sh created successfully
Added remote-local.sh to .gitignore
sftp-watch.sh created successfully
Added sftp-watch.sh to .gitignore
Uploading lazygit to server...
lazygit uploaded successfully
Added lazygit to .gitignore
```
---
### local-remote.sh
**Push local changes to remote server** - Synchronizes files from your local machine to the remote server.
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
#### Usage
```bash
./local-remote.sh
```
#### What It Does
- Uses `rsync` to upload local files to remote server
- Preserves timestamps and permissions
- Deletes files on server that don't exist locally (using `--delete` flag)
- Excludes common development directories (node_modules, vendor, storage, etc.)
- Only uploads changed files for efficiency
#### Excluded Directories
- `.git`
- `.vscode`
- `.DS_Store`
- `node_modules`
- `vendor`
- `storage`
- `uploads`
- `temp`
- `cache`
- `sitepress-multilingual-cms`
#### Use Cases
- Deploy local changes to staging/production
- Update server after local development session
- Restore server files from local backup
---
### remote-local.sh
**Pull remote changes to local** - Synchronizes files from the remote server to your local machine.
#### Usage
```bash
./remote-local.sh
2025-10-22 10:50:38 +03:00
```
2025-11-19 10:04:36 +02:00
#### What It Does
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
- Uses `rsync` to download files from remote server
- Preserves timestamps and permissions
- Does NOT delete local files (no `--delete` flag)
- Excludes common development directories
- Only downloads changed files for efficiency
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
#### Excluded Directories
- `.vscode`
- `.DS_Store`
- `node_modules`
- `vendor`
- `storage`
- `uploads`
- `temp`
- `cache`
- `sitepress-multilingual-cms`
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
#### Use Cases
- Update local environment with server changes
- Pull production data for debugging
- Sync changes made by other team members
- Backup server files to local machine
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
---
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
### sftp-watch.sh
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
**Bidirectional sync with watch mode** - Continuously monitors file changes and syncs automatically.
#### Usage
```bash
./sftp-watch.sh
```
#### What It Does
1. **Initial Sync:**
- Runs `remote-local.sh` first to ensure local is up-to-date
- Displays "Syncing remote->local..." message
2. **Watch Mode:**
- Monitors current directory for file changes
- Debounces changes (500ms delay) to avoid excessive syncing
- Automatically runs `local-remote.sh` on any file modification
#### Requirements
- Requires `watchexec` to be installed
#### Use Cases
- Development with immediate server deployment
- Real-time collaboration on remote server
- Continuous integration workflows
- Testing on remote environment while coding locally
#### Example Workflow
```bash
# Terminal 1: Start watch mode
./sftp-watch.sh
# Terminal 2: Make changes to your code
vim index.php
# Changes automatically sync to server after save
```
---
### build-watch.sh
**Watch and build mode** - Monitors source files, rebuilds on changes, and deploys build output.
#### Usage
```bash
./build-watch.sh
```
#### What It Does
1. Monitors current directory for file changes (2-second debounce)
2. Runs `npm run build` when changes are detected
3. Automatically executes `local-remote.sh` after successful build
4. Uploads built assets to remote server
#### Requirements
- Requires `watchexec` to be installed
- Requires `package.json` with build script defined
- Node.js and npm installed
#### Use Cases
- Frontend development with build steps (React, Vue, etc.)
- Asset compilation (Sass, Less, TypeScript)
- Continuous deployment of build artifacts
- Development where server runs production builds
#### Example Workflow
```bash
# Terminal: Start build watch
./build-watch.sh
# Make changes to source files
# Watches for changes -> Runs npm run build -> Uploads to server
```
#### package.json Example
```json
{
"scripts": {
"build": "webpack --mode production"
}
}
```
---
## Configuration
### VS Code SFTP Configuration
The sync script automatically generates `.vscode/sftp.json`:
```json
{
"name": "project-name",
"host": "server.com",
"protocol": "sftp",
"port": 22,
"username": "your-username",
"password": "your-password",
"remotePath": "/path/to/remote/project",
"uploadOnSave": true,
"useTempFile": false,
"openSsh": false,
"ignore": [
".vscode",
".DS_Store",
"node_modules",
"vendor",
"storage",
"uploads",
"temp",
"cache",
"sitepress-multilingual-cms"
]
}
```
### Customizing Ignore Patterns
Edit `.vscode/sftp.json` or the helper scripts to add/remove excluded directories:
```bash
# Edit local-remote.sh or remote-local.sh
--exclude custom_folder \
```
### Security Considerations
1. **Credentials in Scripts:**
- Helper scripts contain plaintext passwords
- These scripts are automatically added to `.gitignore`
- Never commit these files to version control
2. **Alternative: SSH Keys**
- Consider setting up SSH key-based authentication
- Eliminates need for password storage
- More secure for production environments
```bash
# Generate SSH key
ssh-keygen -t rsa -b 4096
# Copy to server
ssh-copy-id user@server.com
```
2025-10-22 10:50:38 +03:00
## Troubleshooting
2025-11-19 10:04:36 +02:00
### Common Issues
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
#### "sshpass: command not found"
**Solution:** Install sshpass using your package manager (see [Requirements](#requirements))
#### "watchexec: command not found"
**Solution:** Install watchexec for watch mode functionality:
```bash
brew install watchexec # macOS
cargo install watchexec-cli # Cross-platform
```
#### "Permission denied" during SSH
**Solutions:**
- Verify username and password are correct
2025-10-22 10:50:38 +03:00
- Check that SSH is enabled on the server
2025-11-19 10:04:36 +02:00
- Ensure your user has permission to access the remote path
- Try connecting manually: `ssh user@server.com`
#### "Remote path not found"
**Solutions:**
- Verify the server project path exists
- Use absolute paths (e.g., `/home/user/project` not `~/project`)
- Check path spelling and case sensitivity
#### "Archive creation failed"
**Solutions:**
- Ensure you have write permissions in the parent directory
- Check if there's enough disk space on the server
- Verify tar is installed on the server
#### Files not syncing in watch mode
**Solutions:**
- Ensure watchexec is installed
- Check that you're editing files in the watched directory
- Try increasing debounce delay in the script
- Verify rsync is working manually
#### Build fails in build-watch.sh
**Solutions:**
- Verify `npm run build` works manually
- Check that Node.js and npm are installed
- Ensure package.json has a build script defined
- Review npm error messages for missing dependencies
### Debug Mode
Enable verbose output for rsync commands:
```bash
# Edit local-remote.sh or remote-local.sh
# Add -v flag to rsync
rsync -avzuv ...
```
### Manual Testing
Test components individually:
```bash
# Test SSH connection
ssh user@server.com
# Test SCP transfer
scp testfile.txt user@server.com:~/
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
# Test rsync
rsync -avzu --dry-run . user@server.com:~/test/
```
## Best Practices
1. **Initial Setup:**
- Run `sync-project.sh` once per project
- Keep the sync script in a central location and use symbolic links
2. **Development Workflow:**
- Use `sftp-watch.sh` for rapid development with live server testing
- Use `build-watch.sh` for projects with build steps
- Use `local-remote.sh` for manual deployments
2025-10-22 10:50:38 +03:00
2025-11-19 10:04:36 +02:00
3. **Team Collaboration:**
- Run `remote-local.sh` before starting work to get latest changes
- Communicate with team when making large changes
- Use version control (git) in parallel for code history
4. **Security:**
- Never commit generated scripts with credentials
- Use SSH keys for production servers
- Limit server access with appropriate permissions
2025-10-22 10:50:38 +03:00
## License
2025-11-19 10:04:36 +02:00
This project is provided as-is for development and synchronization purposes.
2025-10-22 10:50:38 +03:00
## Contributing
2025-11-19 10:04:36 +02:00
Contributions, issues, and feature requests are welcome. Feel free to check the issues page or submit pull requests.
---
**Note:** This toolkit is designed for development workflows. Always test in staging environments before deploying to production servers.