This script automates the process of copying website files and database dumps from one server to another. It is designed to be robust, secure, and flexible, supporting local, remote, and mixed environments.
- File Synchronization:
- Uses
rsyncwith exclude patterns. - Smart Ownership: Automatically handles ownership (
--no-o --no-g) to ensure destination files are owned by the current user, preventing permission lockouts. - Progress Bar: Clean, non-intrusive progress bar for file transfers.
- Uses
- Database Synchronization:
- Supports MySQL/MariaDB and PostgreSQL.
- Smart Local Transfer: Automatically detects local-to-local transfers and pipes data directly, skipping temporary files.
- Non-Root Friendly: Uses
/tmpfor temporary dumps and safe flags (like--single-transaction) to run without root privileges.
- Flexible Topologies:
- Local-to-Local: Supports transferring between users on the same machine (e.g.,
prod->dev) by treating127.0.0.1as a remote host to bypass file permission issues via SSH. - Remote-to-Local / Local-to-Remote / Remote-to-Remote.
- Local-to-Local: Supports transferring between users on the same machine (e.g.,
- SSH access to source and destination servers.
- Database access credentials.
- SSH Keys for password-less authentication.
- Software:
rsync,mysql/mysqldump(orpsql/pg_dump).
- Edit
config_var.shwith your server details. - Important for Local User-to-User Transfers:
- If transferring between two users on the same machine (e.g.,
produser todevuser), setSRCHOST=127.0.0.1instead oflocalhost. - This forces the script to use SSH, allowing it to read files owned by the other user.
- If transferring between two users on the same machine (e.g.,
-
Pre-check (Optional):
./precheck.sh
-
Run Transfer:
./transfer.sh
To make the script run smoothly without entering passwords each time, set up SSH keys:
# Generate key
ssh-keygen -t rsa -b 4096
# Copy to remote (or localhost for user-to-user transfer)
ssh-copy-id username@remote_serverMIT License