~kiddo/backintime/usability

« back to all changes in this revision

Viewing changes to common/snapshots.py

  • Committer: Dan
  • Date: 2010-11-02 20:51:40 UTC
  • Revision ID: dan@le-web.org-20101102205140-904sag1kuwvm2ie3
dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1192
1192
 
1193
1193
                return [ True, has_errors ]
1194
1194
 
1195
 
        def _smart_remove_keep_all_( self, snapshots, keep_snapshots, min_date ):
 
1195
        def _smart_remove_keep_all_( self, snapshots, keep_snapshots, min_date, max_date ):
1196
1196
                min_id = self.get_snapshot_id( min_date )
 
1197
                max_id = self.get_snapshot_id( max_date )
1197
1198
 
1198
 
                logger.info( "[smart remove] keep all >= %s" % min_id )
 
1199
                logger.info( "[smart remove] keep all >= %s and < %s" % ( min_id, max_id ) )
1199
1200
 
1200
1201
                for snapshot_id in snapshots:
1201
 
                        if snapshot_id >= min_id:
 
1202
                        if snapshot_id >= min_id and snapshot_id < max_id:
1202
1203
                                if snapshot_id not in keep_snapshots:
1203
1204
                                        keep_snapshots.append( snapshot_id )
1204
1205
 
1205
1206
                return keep_snapshots
1206
1207
 
1207
1208
        def _smart_remove_keep_first_( self, snapshots, keep_snapshots, min_date, max_date ):
1208
 
                max_id = self.get_snapshot_id( max_date + datetime.timedelta( days = 1 ) )
1209
1209
                min_id = self.get_snapshot_id( min_date )
 
1210
                max_id = self.get_snapshot_id( max_date )
1210
1211
 
1211
1212
                logger.info( "[smart remove] keep first >= %s and < %s" % ( min_id, max_id ) )
1212
1213
 
1218
1219
 
1219
1220
                return keep_snapshots
1220
1221
 
1221
 
        def smart_remove( self, now_full = None ):
 
1222
        def inc_month( self, date ):
 
1223
                y = date.year
 
1224
                m = date.month + 1
 
1225
                if m > 12:
 
1226
                        m = 1
 
1227
                        y = y + 1
 
1228
                return datetime.date( y, m, 1 )
 
1229
 
 
1230
        def dec_month( self, date ):
 
1231
                y = date.year
 
1232
                m = date.month - 1
 
1233
                if m < 1:
 
1234
                        m = 12
 
1235
                        y = y - 1
 
1236
                return datetime.date( y, m, 1 )
 
1237
 
 
1238
        def smart_remove( self, now_full, keep_all, keep_one_per_day, keep_one_per_week, keep_one_per_month ):
1222
1239
                snapshots = self.get_snapshots_list()
1223
1240
                logger.info( "[smart remove] considered: %s" % snapshots )
1224
1241
                if len( snapshots ) <= 1:
1228
1245
                if now_full is None:
1229
1246
                        now_full = datetime.datetime.today()
1230
1247
 
1231
 
                now = datetime.datetime( now_full.year, now_full.month, now_full.day )
 
1248
                now = now_full.date() 
1232
1249
 
1233
1250
                #keep the last snapshot
1234
1251
                keep_snapshots = [ snapshots[0] ]
1235
1252
 
1236
 
                #keep all from today and yesterday
1237
 
                keep_snapshots = self._smart_remove_keep_all_( snapshots, keep_snapshots, now - datetime.timedelta( days = 1 ) )
1238
 
 
1239
 
                #last week      
1240
 
                max_date = now - datetime.timedelta( days = now.weekday() + 1 )
1241
 
                min_date = max_date - datetime.timedelta( days = 6 )
1242
 
                keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, min_date, max_date )
1243
 
 
1244
 
                #2 weeks ago
1245
 
                max_date = max_date - datetime.timedelta( days = 7 )
1246
 
                min_date = min_date - datetime.timedelta( days = 7 )
1247
 
                keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, min_date, max_date )
1248
 
 
1249
 
                #one per month for all months of this year and last year
1250
 
                if now.date > 1:
1251
 
                        #this year months
1252
 
                        for month in xrange( 1, now.month ):
1253
 
                                #print "month: %s" % month
1254
 
                                min_date = datetime.date( now.year, month, 1 )
1255
 
                                max_date = datetime.date( now.year, month + 1, 1 ) - datetime.timedelta( days = 1 )
1256
 
                                keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, min_date, max_date )
1257
 
 
1258
 
                        #last year months
1259
 
                        for month in xrange( 1, 12 ):
1260
 
                                #print "month: %s" % month
1261
 
                                min_date = datetime.date( now.year - 1, month, 1 )
1262
 
                                max_date = datetime.date( now.year - 1, month + 1, 1 ) - datetime.timedelta( days = 1 )
1263
 
                                keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, min_date, max_date )
1264
 
 
1265
 
                        min_date = datetime.date( now.year - 1, 12, 1 )
1266
 
                        max_date = datetime.date( now.year, 1, 1 ) - datetime.timedelta( days = 1 )
1267
 
                        keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, min_date, max_date )
1268
 
 
1269
 
                #one per year for all previous years
1270
 
                min_year = int( snapshots[ -1 ][ :4 ] )
1271
 
                for year in xrange( min_year, now.year - 1 ):
1272
 
                        #print "year: %s" % year
1273
 
                        min_date = datetime.date( year, 1, 1 )
1274
 
                        max_date = datetime.date( year + 1, 1, 1 ) - datetime.timedelta( days = 1 )
1275
 
                        keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, min_date, max_date )
 
1253
                #keep all for the last keep_all days
 
1254
                if keep_all > 0:
 
1255
                        keep_snapshots = self._smart_remove_keep_all_( snapshots, keep_snapshots, now - datetime.timedelta( days=keep_all-1), now + datetime.timedelta(days=1) )
 
1256
 
 
1257
                #keep one per days for the last keep_one_per_day days
 
1258
                if keep_one_per_day > 0:
 
1259
                        d = now
 
1260
                        for i in xrange( 0, keep_one_per_day ):
 
1261
                                keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, d, d + datetime.timedelta(days=1) )
 
1262
                                d = d - datetime.timedelta(days=1)
 
1263
 
 
1264
                #keep one per week for the last keep_one_per_week weeks
 
1265
                if keep_one_per_week > 0:
 
1266
                        d = now - datetime.timedelta( days = now.weekday() + 1 )
 
1267
                        for i in xrange( 0, keep_one_per_week ):
 
1268
                                keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, d, d + datetime.timedelta(days=8) )
 
1269
                                d = d - datetime.timedelta(days=7)
 
1270
 
 
1271
                #keep one per month for the last keep_one_per_month months
 
1272
                if keep_one_per_month > 0:
 
1273
                        d1 = datetime.date( now.year, now.month, 1 )
 
1274
                        d2 = self.inc_date( d1 )
 
1275
                        for i in xrange( 0, keep_one_per_month ):
 
1276
                                keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, d1, d2 )
 
1277
                                d2 = d1
 
1278
                                d1 = self.dec_month(1)
 
1279
 
 
1280
                #keep one per year for all years
 
1281
                first_year = int(snapshots[-1][ : 4])
 
1282
                for i in xrange( first_year, now.year()+1 ):
 
1283
                        keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, datetime.date(i,1,1), datetime.date(i+1,1,1) )
1276
1284
 
1277
1285
                logger.info( "[smart remove] keep snapshots: %s" % keep_snapshots )
1278
1286
 
1313
1321
                                del snapshots[0]
1314
1322
 
1315
1323
                #smart remove
1316
 
                if self.config.get_smart_remove():
 
1324
                smart_remove, keep_all, keep_one_per_day, keep_one_per_week, keep_one_per_month, keep_one_per_year = self.config.get_smart_remove()
 
1325
                if smart_remove:
1317
1326
                        self.set_take_snapshot_message( 0, _('Smart remove') )
1318
 
                        self.smart_remove( now )
 
1327
                        self.smart_remove( now, keep_all, keep_one_per_day, keep_one_per_week, keep_one_per_month, keep_one_per_year )
1319
1328
 
1320
1329
                #try to keep min free space
1321
1330
                if self.config.is_min_free_space_enabled():