Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import datetime
- import glob
- import os
- import shutil
- import subprocess
- import sys
- import time
- source = sys.argv[1]
- if not ':' in source:
- source = os.path.abspath(source)
- repository = os.path.abspath(sys.argv[2])
- # assume the pruning script is in the same folder as this script
- prune = os.path.join(os.path.dirname(sys.argv[0]), 'prune.py')
- target = os.path.join(repository, 'pending')
- exclude = os.path.join(repository, 'rsync.exclude')
- rsynclogname = 'rsync.log'
- rsynclog = os.path.join(repository, rsynclogname)
- prunelog = os.path.join(repository, 'prune.log')
- while True:
- command = 'rsync'
- options = [
- '--archive',
- '--delete',
- # uncomment the next line if the backup server doesn't have user names matching the clients'
- # '--numeric-ids',
- '--exclude-from=' + exclude,
- '--log-file=' + rsynclog
- ]
- # find the most recent successful backup, if any, so we can hard-link the new backup to it
- folders = glob.glob(os.path.join(repository, '20*'))
- if folders:
- folders.sort()
- options.append('--link-dest=' + folders.pop())
- args = [command] + options + [source] + [target]
- result = subprocess.call(args)
- next = time.time() + 3600
- # return code 24 means some files disappeared before they could be copied, which we consider benign
- if result == 0 or result == 24:
- mtime = datetime.datetime.fromtimestamp(os.path.getmtime(rsynclog))
- snapshotname = mtime.strftime('%Y%m%d-%H%M%S')
- snapshotpath = os.path.join(repository, snapshotname)
- os.rename(target, snapshotpath)
- shutil.move(rsynclog, snapshotpath)
- subprocess.call([prune, repository])
- if os.path.exists(prunelog):
- shutil.move(prunelog, snapshotpath)
- # wait at least 1 hour between backups
- pause = next - time.time()
- if pause > 0:
- time.sleep(pause)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement