Created
March 2, 2013 17:45
-
-
Save glynrob/5072177 to your computer and use it in GitHub Desktop.
Python code to use GnuPG to encrypt a folder and FTP it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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