Advertisement
trevellyan

simple script for rsync backup (server pull)

Mar 12th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import datetime
  4. import glob
  5. import os
  6. import shutil
  7. import subprocess
  8. import sys
  9. import time
  10.  
  11. source = sys.argv[1]
  12. if not ':' in source:
  13.     source = os.path.abspath(source)
  14. repository = os.path.abspath(sys.argv[2])
  15. # assume the pruning script is in the same folder as this script
  16. prune = os.path.join(os.path.dirname(sys.argv[0]), 'prune.py')
  17.  
  18. target = os.path.join(repository, 'pending')
  19. exclude = os.path.join(repository, 'rsync.exclude')
  20. rsynclogname = 'rsync.log'
  21. rsynclog = os.path.join(repository, rsynclogname)
  22. prunelog = os.path.join(repository, 'prune.log')
  23.  
  24. while True:
  25.     command = 'rsync'
  26.     options = [
  27.         '--archive',
  28.         '--delete',
  29. # uncomment the next line if the backup server doesn't have user names matching the clients'
  30. #        '--numeric-ids',
  31.         '--exclude-from=' + exclude,
  32.         '--log-file=' + rsynclog
  33.         ]
  34.  
  35.     # find the most recent successful backup, if any, so we can hard-link the new backup to it
  36.     folders = glob.glob(os.path.join(repository, '20*'))
  37.     if folders:
  38.         folders.sort()
  39.         options.append('--link-dest=' + folders.pop())
  40.  
  41.     args = [command] + options + [source] + [target]
  42.  
  43.     result = subprocess.call(args)
  44.     next = time.time() + 3600
  45.  
  46.     # return code 24 means some files disappeared before they could be copied, which we consider benign
  47.     if result == 0 or result == 24:
  48.         mtime = datetime.datetime.fromtimestamp(os.path.getmtime(rsynclog))
  49.         snapshotname = mtime.strftime('%Y%m%d-%H%M%S')
  50.         snapshotpath = os.path.join(repository, snapshotname)
  51.         os.rename(target, snapshotpath)
  52.         shutil.move(rsynclog, snapshotpath)
  53.  
  54.         subprocess.call([prune, repository])
  55.         if os.path.exists(prunelog):
  56.             shutil.move(prunelog, snapshotpath)
  57.  
  58.     # wait at least 1 hour between backups
  59.     pause = next - time.time()
  60.     if pause > 0:
  61.         time.sleep(pause)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement