/usr/share/backintime/common/tools.py is in backintime-common 1.0.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | # Back In Time
# Copyright (C) 2008-2009 Oprea Dan, Bart de Koning, Richard Bailey
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import os.path
import os
import sys
import subprocess
import hashlib
import commands
ON_AC = 0
ON_BATTERY = 1
POWER_ERROR = 255
def get_backintime_path( path ):
return os.path.join( os.path.dirname( os.path.abspath( os.path.dirname( __file__ ) ) ), path )
def register_backintime_path( path ):
path = get_backintime_path( path )
if not path in sys.path:
sys.path = [path] + sys.path
def read_file( path, default_value = None ):
ret_val = default_value
try:
file = open( path )
ret_val = file.read()
file.close()
except:
pass
return ret_val
def read_file_lines( path, default_value = None ):
ret_val = default_value
try:
file = open( path )
ret_val = file.readlines()
file.close()
except:
pass
return ret_val
def read_command_output( cmd ):
ret_val = ''
try:
pipe = os.popen( cmd )
ret_val = pipe.read().strip()
pipe.close()
except:
return ''
return ret_val
def check_command( cmd ):
cmd = cmd.strip()
if len( cmd ) < 1:
return False
if os.path.isfile( cmd ):
return True
cmd = read_command_output( "which \"%s\"" % cmd )
if len( cmd ) < 1:
return False
if os.path.isfile( cmd ):
return True
return False
def make_dirs( path ):
path = path.rstrip( os.sep )
if len( path ) <= 0:
return
if not os.path.isdir( path ):
try:
os.makedirs( path )
except:
pass
def process_exists( name ):
output = read_command_output( "ps -o pid= -C %s" % name )
return len( output ) > 0
def check_x_server():
return 0 == os.system( 'xdpyinfo >/dev/null 2>&1' )
def prepare_path( path ):
path = path.strip( "/" )
path = os.sep + path
return path
def power_status_available():
"""Uses the on_ac_power command to detect if the the system is able
to return the power status."""
try:
rt = subprocess.call( 'on_ac_power' )
if rt == ON_AC or rt == ON_BATTERY:
return True
except:
pass
return False
def on_battery():
"""Checks if the system is on battery power."""
if power_status_available ():
return subprocess.call ( 'on_ac_power' ) == ON_BATTERY
else:
return False
def get_snapshots_list_in_folder( folder, sort_reverse = True ):
biglist = []
#print folder
try:
biglist = os.listdir( folder )
#print biglist
except:
pass
list = []
for item in biglist:
#print item + ' ' + str(len( item ))
if len( item ) != 15 and len( item ) != 19:
continue
if os.path.isdir( os.path.join( folder, item, 'backup' ) ):
#print item
list.append( item )
list.sort( reverse = sort_reverse )
return list
def get_nonsnapshots_list_in_folder( folder, sort_reverse = True ):
biglist = []
#print folder
try:
biglist = os.listdir( folder )
#print biglist
except:
pass
list = []
for item in biglist:
#print item + ' ' + str(len( item ))
if len( item ) != 15 and len( item ) != 19:
list.append( item )
else:
if os.path.isdir( os.path.join( folder, item, 'backup' ) ):
#print item
continue
else:
list.append( item )
list.sort( reverse = sort_reverse )
return list
def move_snapshots_folder( old_folder, new_folder ):
'''Moves all the snapshots from one folder to another'''
print "\nMove snapshots from %s to %s" %( old_folder, new_folder )
# Fetch a list with snapshots for verification
snapshots_to_move = get_snapshots_list_in_folder( old_folder )
snapshots_already_there = []
if os.path.exists( new_folder ) == True:
snapshots_already_there = get_snapshots_list_in_folder( new_folder )
else:
tools.make_dirs( new_folder )
print "To move: %s" % snapshots_to_move
print "Already there: %s" % snapshots_already_there
snapshots_expected = snapshots_to_move + snapshots_already_there
print "Snapshots expected: %s" % snapshots_expected
# Check if both folders are within the same os
device_old = os.stat( old_folder ).st_dev
device_new = os.stat( new_folder ).st_dev
if device_old == device_new:
# Use move
for snapshot in snapshots_to_move:
cmd = "mv -f \"%s/%s\" \"%s\"" %( old_folder, snapshot, new_folder )
_execute( cmd )
else:
# Use rsync
# Prepare hardlinks
if len( snapshots_already_there ) > 0:
first_snapshot_path = os.path.join( new_folder, snapshots_to_move[ len( snapshots_to_move ) - 1 ] )
snapshot_to_hardlink_path = os.path.join( new_folder, snapshots_already_there[0] )
_execute( "find \"%s\" -type d -exec chmod u+wx {} \\;" % snapshot_to_hardlink_path )
cmd = "cp -al \"%s\" \"%s\"" % ( snapshot_to_hardlink_path, first_snapshot_path )
_execute( cmd )
# Prepare excludes
nonsnapshots = get_nonsnapshots_list_in_folder( old_folder )
print "Nonsnapshots: %s" % nonsnapshots
items = []
for nonsnapshot in nonsnapshots:
for item in items:
if nonsnapshot == item:
break
items.append( "--exclude=\"%s\"" % nonsnapshot )
rsync_exclude = ' '.join( items )
#print rsync_exclude
# Move move move
cmd = "rsync -aEAXHv --delete " + old_folder + " " + new_folder + " " + rsync_exclude
_execute( cmd )
_execute ( "find \"%s\" \"%s\" -type d -exec chmod a-w {} \\;" % ( snapshots_to_hardlink_path, first_snapshot_path ) )
# Remove old ones
snapshots_not_moved = []
for snapshot in snapshots_to_move:
if os.path.exists( os.path.join( new_folder, snapshot, "backup" ) ):
if os.path.exists( os.path.join( old_folder, snapshot) ):
print "Remove: %s" %snapshot
path_to_remove = os.path.join( old_folder, snapshot )
cmd = "find \"%s\" -type d -exec chmod u+wx {} \\;" % path_to_remove #Debian patch
_execute( cmd )
cmd = "rm -rfv \"%s\"" % path_to_remove
_execute( cmd )
else:
print "%s was already removed" %snapshot
else:
snapshots_not_moved.append( snapshot )
# Check snapshot list
if len( snapshots_not_moved ) == 0:
print "Succes!\n"
return True
else:
print "Error! Not moved: %s\n" %snapshots_not_moved
return False
def _execute( cmd, callback = None, user_data = None ):
ret_val = 0
if callback is None:
ret_val = os.system( cmd )
else:
pipe = os.popen( cmd, 'r' )
while True:
line = temp_failure_retry( pipe.readline )
if len( line ) == 0:
break
callback( line.strip(), user_data )
ret_val = pipe.close()
if ret_val is None:
ret_val = 0
if ret_val != 0:
print "Command \"%s\" returns %s" % ( cmd, ret_val )
else:
print "Command \"%s\" returns %s" % ( cmd, ret_val )
return ret_val
def is_process_alive( pid ):
try:
os.kill( pid, 0 ) #this will raise an exception if the pid is not valid
except:
return False
return True
def get_rsync_caps():
data = read_command_output( 'rsync --version' )
si = data.find( 'Capabilities:' )
if si < 0:
return []
si = data.find( '\n', si )
if si < 0:
return []
ei = data.find( '\n\n', si )
if ei < 0:
return []
data = data[ si + 1 : ei - 1 ]
data = data.split( '\n' )
all_caps = ''
for line in data:
line = line.strip()
if len( line ) <= 0:
continue
if len( all_caps ) > 0:
all_caps = all_caps + ' '
all_caps = all_caps + line
caps = all_caps.split( ", " )
#print caps
#print ( "ACLs" in get_rsync_caps() )
return caps
def use_rsync_fast( config ):
return not (config.preserve_acl() or config.preserve_xattr())
def get_rsync_prefix( config ):
caps = get_rsync_caps()
#cmd = 'rsync -aEH'
cmd = 'rsync'
cmd = cmd + ' -rtDH'
if config.use_checksum():
cmd = cmd + ' --checksum'
if config.copy_unsafe_links():
cmd = cmd + ' --copy-unsafe-links'
if config.copy_links():
cmd = cmd + ' --copy-links'
else:
cmd = cmd + ' --links'
no_perms = True
if config.preserve_acl() and "ACLs" in caps:
cmd = cmd + ' -A'
no_perms = False
if config.preserve_xattr() and "xattrs" in caps:
cmd = cmd + ' -X'
no_perms = False
if no_perms:
cmd = cmd + ' --no-p --no-g --no-o'
else:
cmd = cmd + ' -pEgo'
return cmd + ' '
def temp_failure_retry(func, *args, **kwargs):
while True:
try:
return func(*args, **kwargs)
except (os.error, IOError), ex:
if ex.errno == errno.EINTR:
continue
else:
raise
def _get_md5sum_from_path(path):
'''return md5sum of path, af available system command md5sum()'''
if check_command("md5sum"):
status,output = commands.getstatusoutput("md5sum '" + path + "'")
if status == 0:
md5sum = output.split(" ")[0]
return md5sum
# md5sum unavailable or command failed; raise an exception ? a message ? use std lib ?
print "warning: md5sum() fail ! used (st_size, st_mttime) instead of md5sum."
obj = os.stat(path)
unique_key = (obj.st_size, int(obj.st_mtime))
return unique_key
#
#
class UniquenessSet:
'''a class to check for uniqueness of snapshots of the same [item]'''
def __init__(self, dc = False, follow_symlink = False):
self.deep_check = dc
self.follow_sym = follow_symlink
self._uniq_dict = {} # if not self._uniq_dict[size] -> size already checked with md5sum
self._size_inode = set() # if (size,inode) in self._size_inode -> path is a hlink
def check_for(self, input_path, verb = False):
'''store a unique key for path, return True if path is unique'''
# follow symlinks ?
path = input_path
if self.follow_sym and os.path.islink(input_path):
path = os.readlink(input_path)
# check
if self.deep_check:
dum = os.stat(path)
size,inode = dum.st_size, dum.st_ino
# is it a hlink ?
if (size, inode) in self._size_inode:
if verb: print "[deep test] : skip, it's a duplicate (size, inode)"
return False
self._size_inode.add( (size,inode) )
if size not in self._uniq_dict.keys():
# first item of that size
unique_key = size
if verb: print "[deep test] : store current size ?"
else:
prev = self._uniq_dict[size]
if prev:
# store md5sum instead of previously stored size
md5sum_prev = _get_md5sum_from_path(prev)
self._uniq_dict[size] = None
self._uniq_dict[md5sum_prev] = prev
if verb:
print "[deep test] : size duplicate, remove the size, store prev md5sum"
unique_key = _get_md5sum_from_path(path)
if verb: print "[deep test] : store current md5sum ?"
else:
# store a tuple of (size, modification time)
obj = os.stat(path)
unique_key = (obj.st_size, int(obj.st_mtime))
# print "..", path, unique_key
# store if not already present, then return True
if unique_key not in self._uniq_dict.keys():
if verb: print " >> ok, store !"
self._uniq_dict[unique_key] = path
return True
if verb: print " >> skip (it's a duplicate)"
return False
|