Automatic FTP backup for servers

Today I need to find a way to do some backup on an FTP server. Of course there is a lot of way to do that, but I need something automatic that does some full (monthly) and incremental (daily) backup. I decided to use duplicity because it’s a really simple and effective software. The main issue, is that duplicity command line is a bit hard to remember for me. So why not simply write a simple python script that does the job for me.

So here, we are :

#!/usr/bin/python 
# Edit DATA and DEST
DATA='/var/www'
DEST='ftp://username:password@ftp.server/backup'

import os
import sys

def backup(b_type='incr'):
    dup_cmd ='nice -n 19 duplicity %s  --volsize 512 --no-encryption --asynchronous-upload ' % (b_type,)
    temp = '%s %s %s' % (dup_cmd,DATA,DEST)
    os.system(temp)

def info():
    dup_cmd='duplicity collection-status  %s' % (DEST)
    os.system(dup_cmd)

def list_files():
    dup_cmd='duplicity list-current-files  %s' % (DEST)
    os.system(dup_cmd)

def clean():
    dup_cmd='duplicity remove-all-but-n-full 1 --force %s' % (DEST)
    os.system(dup_cmd)

def usage():
    print "%s [-incr|-full|-list|-info|-clean]" % sys.argv[0]

if __name__=='__main__':
    if len(sys.argv) > 1:
        if sys.argv[1] == '-incr':
            backup('inc')
        elif sys.argv[1] == '-full':
            backup('full')
        elif sys.argv[1] == '-info':
            info()
        elif sys.argv[1] == '-list':
            list_files()
        elif sys.argv[1] == '-clean':
            clean()
        else:
            usage()
    else:
        usage()

Please note that I disabled the PGP encryption for this backup. Mainly because, I don’t want to deal w/ lost keys when I will need to restore the backup. But this can be done easily. I will post a complete guide if someone ask.

The next step call the script in cron.d for example with something like this : 30  3   *  *  *

Feel happy no more stress with backup ;)



Related Posts

One thought on “Automatic FTP backup for servers

  1. Hi,
    Nice, thanks for the info!
    May I ask for the full guide with encryption keys? Should symmetric encryption be used or is it better to use a key pair and save the private key in a secure location? In that case I suppose nothing sensitive (private key) would even have to be stored on the server?
    Thanks,

    /Nico

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>