A one-click bash script to securely dump, transfer, and import a MySQL/MariaDB database from one live server to another — all in one step.
✅ Dump a source database
✅ Securely transfer the dump via SSH
✅ Automatically import into a destination MySQL/MariaDB server
✅ Automatically clean up (.sql file deleted from both servers)
✅ Easy-to-configure and reusable
db/
├── db.sh # The main transfer script
└── README.md # This documentation
- Linux/macOS terminal (bash)
mysqldump,mysqlinstalled (default in most systems)sshpassfor SSH automation (if not using SSH key)
sudo apt update
sudo apt install sshpass mysql-client -y- Open the script file:
nano db.sh- Replace the following placeholders with your actual credentials:
# Source DB
SRC_DB="your_database"
SRC_USER="your_database_user"
SRC_PASS="your_source_db_password"
# Destination
DEST_HOST="your_IP"
DEST_DB="your_database"
DEST_USER="your_database_user"
DEST_PASS="your_dest_db_password"
DEST_SSH_USER="root"
DEST_SSH_PASS="your_ssh_password"- Make the script executable:
chmod +x db.sh- Run it:
./db.sh✅ That’s it — your database will be dumped, transferred, imported, and cleaned up in seconds.
This script deletes the backup .sql file from both servers after the import is complete.
- Local
.sqlfile: deleted from the source server - Remote
.sqlfile: deleted from the destination server after import
This ensures security and avoids disk clutter.
- This script uses plain-text passwords for MySQL and SSH. Only use on trusted environments.
- For better security, consider:
- SSH key authentication instead of
sshpass - Using
.my.cnfto store MySQL credentials securely
- SSH key authentication instead of
📦 Dumping source database (Database)...
🚚 Transferring backup to destination server (IP)...
📥 Importing into destination database (Database)...
🧹 Deleting local backup file...
✅ Transfer, import & cleanup complete!Author: zamibd
License: MIT — free to use, modify, and distribute.
Feel free to open an issue or pull request on the GitHub repo.