git clone https://github.com/mhtalci/web-db-transfer.git
cd web-db-transfer
chmod +x transfer.sh
This script automates the process of copying website files and database dumps from one server to another using rsync
for files and mysqldump
for the database.
- SSH access to both source and destination servers.
- MySQL/MariaDB access credentials for both source and destination databases.
- SSH Keys for password-less authentication.
To make the script run smoothly without entering passwords each time, it's recommended to set up SSH keys for authentication between your local and remote servers.
If you don't already have an SSH key pair, you can generate one using the following command:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This will generate a public and private key pair. You will be prompted to enter a file to save the key (you can accept the default location) and an optional passphrase.
By default, the key pair will be saved in: ~/.ssh/id_rsa (private key) ~/.ssh/id_rsa.pub (public key)
Next, you need to copy your public key (~/.ssh/id_rsa.pub) to both the source and destination servers. This can be done using the ssh-copy-id command:
ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_server
Replace username with your SSH username and remote_server with the actual server’s address (IP or hostname). This command will prompt you for the password of the remote server, and once it’s entered, it will copy your public key to the remote server’s ~/.ssh/authorized_keys file.
After copying your public key to the remote servers, you can test the SSH connection to ensure password-less login is working:
ssh username@remote_server
If everything is set up correctly, you should be logged in without being prompted for a password.
Once your SSH keys are set up, the script will automatically use them for authentication when connecting to the remote servers, assuming the private key is located in ~/.ssh/id_rsa (default location).
If you have stored your SSH key in a different location, you can specify the key path in the script. For example, update the rsync command in the script like this:
rsync -az --info=progress2 --stats -e "ssh -i /path/to/your/private_key -p $SRCSSHPORT" $SRCUSER@$SRCHOST:$SRCHOME/ $DSTUSER@$DSTHOST:$DSTHOME/
This will allow you to use a custom private key file for the transfer.
bash transfer.sh
- Customize the variables in the script (e.g., source and destination server details).
- Run the script on your server.
MIT License