Skip to content

Instantly share code, notes, and snippets.

@telekineticyeti
Created September 5, 2012 18:57
Show Gist options
  • Select an option

  • Save telekineticyeti/3642562 to your computer and use it in GitHub Desktop.

Select an option

Save telekineticyeti/3642562 to your computer and use it in GitHub Desktop.
Python shell script that utilizes the excellent LFTP to create a mirror between a local directory and a remote ftp site.
#!/usr/bin/python
'''
©2012 Paul Castle
Very crude but functional early edition, will expand in the future.
I run this script on a 3 day cron to keep my personal wiki backed up to my
local development server. LFTP is very good at retaining permissions between
mirrors.
User/pass variables require base64 encoded attributes to avoid shoulder-surfers.
IF you don't want b64, remove base64.b64decode routines from the mirrorftp
function.
TODO
- Add option for mirror reversal
- Expand options for compression
'''
import os, base64, time, sys
# Set Options
ftp = {
'host' : 'ftp://yourhost.com',
'user' : '',
'pass' : '',
'target' : '/home/target/folder',
'source' : '/source/remote/folder',
'exclude' : '--exclude-glob *.example',
'options' : '--continue --verbose --delete --parallel=4 --no-symlinks --use-cache'
}
def mirrorftp():
execstr = ("lftp -c \"\nset ssl:verify-certificate no\nopen %s\nuser %s %s\nlcd %s\nmirror %s %s %s %s\nbye\n\"") % (ftp['host'],base64.b64decode(ftp['user']),base64.b64decode(ftp['pass']),ftp['target'],ftp['options'],ftp['exclude'],ftp['source'],ftp['target'])
os.system(execstr)
# running script as root? warn if not
if os.geteuid() != 0:
print ("Warning!\nNot running as root!\nYou may get permission errors.")
#mirror ftp
fst = time.time()
print "Starting a Mirror Backup of "+ftp['host']
mirrorftp()
fet = time.time() - fst
#Compress archive
print ("Compressing archive")
zst = time.time()
os.system = ("tar -zcvf %s.tar.gz %s") % ("mirroredlftp",ftp['target'])
zet = time.time() - zst
# report
print ("Backup Complete! FTP-Backup %s seconds elapsed, Compression took %s seconds.") % (str(round(fet,2)),str(round(zet,2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment