]> piware.de Git - bin.git/blob - backup
add backup
[bin.git] / backup
1 #!/usr/bin/python
2
3 # call rsnapshot daily/weekly/monthly regularly
4 # this should be called from cron very often (several times a day) to not miss
5 # a cycle when the machine is powered down for extended times.
6 #
7 # Author: Martin Pitt <martin@piware.de>
8 # License: Public Domain
9
10 import os, time, os.path, pwd, subprocess
11
12 user = pwd.getpwuid(os.getuid()).pw_name
13 rsnapshot_basedir = '/var/backups/' + user
14 rsnapshot_conffile = os.path.expanduser('~/.rsnapshotrc')
15 verbose = True
16
17 def days_mod(path):
18     '''Return the number of days since the last modification of path.'''
19
20     if os.path.exists(path):
21         return (time.time() - os.stat(path).st_mtime)/86400.
22     else:
23         return time.time()/86400.
24
25 def rsnapshot(mode):
26     argv = ['rsnapshot']
27     if verbose:
28         argv.append('-v')
29     argv += ['-c', rsnapshot_conffile, mode]
30     if verbose:
31         print argv
32     subprocess.call(argv)
33
34 # daily
35 if days_mod(os.path.join(rsnapshot_basedir, 'daily.0')) >= 1:
36     rsnapshot('daily')
37 elif days_mod(os.path.join(rsnapshot_basedir, 'weekly.0')) >= 7:
38     rsnapshot('weekly')
39 elif days_mod(os.path.join(rsnapshot_basedir, 'monthly.0')) >= 30:
40     rsnapshot('monthly')