Skip to content

Instantly share code, notes, and snippets.

@glynrob
Created March 2, 2013 17:45
Show Gist options
  • Save glynrob/5072177 to your computer and use it in GitHub Desktop.
Save glynrob/5072177 to your computer and use it in GitHub Desktop.
Python code to use GnuPG to encrypt a folder and FTP it
import os
import gnupg
import tarfile
import ftplib
# create zip file of the folder
tar = tarfile.open("docs_backup.tar", "w")
tar.add("/home/pi/Documents", arcname="bar")
tar.close()
# encrypt the file with the public key provided
gpg = gnupg.GPG(gnupghome='/home/pi/.gnupg')
with open('docs_backup.tar', 'rb') as f:
status = gpg.encrypt_file(
f, recipients=['GlynRobOpen'],
always_trust='true',
output='docs_backup.tar.gpg')
# output response if you are testing
#print 'ok: ', status.ok
#print 'status: ', status.status
#print 'stderr: ', status.stderr
# Remove the original zip file
os.system('rm -rf /home/pi/backupsys/docs_backup.tar')
# FTP encrypted file
sftp = ftplib.FTP('0.0.0.0','FTPUser','FTPPassword') # Connect
fp = open('docs_backup.tar.gpg','rb') # file to send
sftp.storbinary('STOR /httpdocs/docs_backup.tar.gpg', fp) # Send the file
fp.close() # Close file and FTP
sftp.quit()
# Remove encrypted file
os.system('rm -rf /home/pi/backupsys/docs_backup.tar.gpg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment