How To Copy Files From One Server To Another
Last Updated :
10 Dec, 2024
Copying files between servers can seem daunting, but with Python, it becomes a straightforward process. Python provides several libraries to handle file transfers, with paramiko being a popular choice because it allows for SSH connections and file transfers via SFTP (SSH File Transfer Protocol). In this simple guide, we'll walk through how to use Python and paramiko to transfer a file from one server to another.
Prerequisites
- Python installed on your local machine: Ensure Python is set up correctly.
- paramiko library: If not already installed, you can install it via pip
pip install paramiko
Step-by-Step Guide to Copy Files Using Python
Step 1: Import paramiko and Set Up SSH Connection
First, import the necessary components from paramiko and establish an SSH connection to the source server where your file is located.
Python
import paramiko
# Establishing SSH client for the source server
source_ssh = paramiko.SSHClient()
source_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the source server
source_ip = "192.168.1.1"
source_username = "source_username"
source_password = "source_password"
source_ssh.connect(source_ip, username=source_username, password=source_password)
# Establishing SSH client for the destination server
destination_ssh = paramiko.SSHClient()
destination_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the destination server
destination_ip = "192.168.1.2"
destination_username = "destination_username"
destination_password = "destination_password"
destination_ssh.connect(destination_ip, username=destination_username, password=destination_password)
Step 2: Set Up SFTP Session
Once connected, initiate an SFTP session to manage file transfers.
Python
# Setup SFTP session for the source server
source_sftp = source_ssh.open_sftp()
# Setup SFTP session for the destination server
destination_sftp = destination_ssh.open_sftp()
Step 3: Copy the File
With the SFTP session open, you can now transfer files. Specify the path to the file on the source server and the destination path on the target server.
Python
file_to_transfer = '/path/on/source/file.txt'
local_path = '/path/on/local/file.txt'
# Download the file from the source server
source_sftp.get(file_to_transfer, local_path)
Step 4: Close SFTP Session and SSH Connection
After transferring the files, close the SFTP session and the SSH connection to clean up resources.
Python
destination_path = '/path/on/destination/file.txt'
# Upload the file to the destination server
destination_sftp.put(local_path, destination_path)
Step 5: Close SFTP Sessions and SSH Connections
After transferring the files, close the SFTP sessions and the SSH connections to clean up resources.
Python
source_sftp.close()
source_ssh.close()
destination_sftp.close()
destination_ssh.close()
Example
After applying these steps to copy a file named report.txt from one server to another:
Python
import paramiko
try:
# Setup SSH connection to the source server
source_ssh = paramiko.SSHClient()
source_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
source_ssh.connect('192.168.1.1', username='source_username', password='source_password')
print("SSH connection to the source server established.")
# Setup SSH connection to the destination server
destination_ssh = paramiko.SSHClient()
destination_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
destination_ssh.connect('192.168.1.2', username='destination_username', password='destination_password')
print("SSH connection to the destination server established.")
# Setup SFTP sessions
source_sftp = source_ssh.open_sftp()
destination_sftp = destination_ssh.open_sftp()
print("SFTP sessions started.")
# Paths
file_to_transfer = '/path/on/source/file.txt'
local_path = '/path/on/local/file.txt'
destination_path = '/path/on/destination/file.txt'
# Download the file from the source server
source_sftp.get(file_to_transfer, local_path)
print("File downloaded from the source server.")
# Upload the file to the destination server
destination_sftp.put(local_path, destination_path)
print("File uploaded to the destination server.")
finally:
if source_sftp: source_sftp.close()
if source_ssh: source_ssh.close()
if destination_sftp: destination_sftp.close()
if destination_ssh: destination_ssh.close()
print("Connections closed.")
Output:
SSH connection to the source server established.
SSH connection to the destination server established.
SFTP sessions started.
File downloaded from the source server.
File uploaded to the destination server.
Connections closed.
Conclusion
Using Python and paramiko to copy files between servers is an efficient method that leverages the power of programming to automate and secure file transfers. This method is highly useful for regular backups, data migration, or simply moving data between environments in a controlled manner. With the ability to script and automate these tasks, Python ensures that your data handling processes are both scalable and reliable.