SCP Not Working: Custom Python Shell Script Migration for Ubuntu 20.04 and Debian 12
In today's world, backups are crucial for protecting machines and managing backups. One common method of backing up data is by using an SCP (Secure Copy Protocol) server. This article will cover a custom Python shell script migration for Ubuntu 20.04 and Debian 12 to address issues with SCP not working.
Background
SCP is a protocol used for securely transferring files between a local and remote host or between two remote hosts. It is based on the BSD protocol and uses SSH for data transfer. SCP is commonly used for backups, and it is built into most SSH clients.
However, there are situations where SCP may not work as expected. For instance, if the backup server has various clients uploading daily archives via SCP, it can become overloaded, leading to issues with data transfer.
Custom Python Shell Script Migration
To address this issue, a custom Python shell script can be written to manage backups. This script can be used as an alternative to SCP, providing a more efficient and reliable method of backing up data.
Prerequisites
Before migrating to a custom Python shell script, it is essential to ensure that the following prerequisites are met:
- Python 3.6 or higher is installed on both the local and remote hosts.
- SSH keys are set up between the local and remote hosts.
- The remote host has Python and the required libraries installed.
Script Overview
The custom Python shell script will perform the following tasks:
- Establish an SSH connection to the remote host.
- Create a backup directory on the remote host if it does not already exist.
- Transfer the backup files from the local host to the remote host.
- Compress the backup files on the remote host.
- Close the SSH connection.
Script Code
import paramiko
import os
import time
# Set up SSH client
ssh = paramiko.SSHClient()
ssh.set\_missing\_host\_key\_policy(paramiko.AutoAddPolicy())
# Establish SSH connection
ssh.connect('remote\_host', username='username', password='password')
# Set up SCP client
scp = ssh.open\_sftp()
# Create backup directory on remote host if it does not already exist
if not scp.listdir\_attr('/path/to/backup/directory'):
scp.mkdir('/path/to/backup/directory')
# Transfer backup files from local host to remote host
for file in os.listdir('/path/to/local/backup/directory'):
if file.endswith('.tar.gz'):
scp.put('/path/to/local/backup/directory/' + file, '/path/to/backup/directory/' + file)
# Compress backup files on remote host
for file in scp.listdir\_attr('/path/to/backup/directory'):
if file.endswith('.tar.gz'):
scp.chdir('/path/to/backup/directory')
scp.execute('tar -czf ' + file.split('.')[0] + '.tar.gz ' + file)
scp.remove(file)
# Close SSH connection
scp.close()
ssh.close()
Migrating to a custom Python shell script can provide a more efficient and reliable method of backing up data than using SCP. By following the steps outlined in this article, you can create a custom Python shell script for managing backups on Ubuntu 20.04 and Debian 12.
References
- SCP (Secure Copy Protocol) - https://www.ssh.com/ssh/scp/
- Python Paramiko Library - https://www.paramiko.org/
- SSH Keys -