python - Extracting tar file with original time stamp -
my objective extract archive file members different location original timestamps using python 2.6.
for example :
tar -tzvf nas_archive_11-15-12.tgz -rw-r--r-- appins/app 2337 2012-11-15 10:25:09 nfs4exports/opt/prod/fmac/files/inbound/test_archive/a.txt.gpg
i want restore above file "/nfs4exports/opt/prod/fmac/files/inbound/test_archive/11-15-12/a.txt.gpg" since last modified on 15th november 2012.
i have created following script :
#!/usr/bin/python import os, tarfile datetime import datetime, date, timedelta import datetime a_path = '/home/appins/.scripts/' root, subfolders, files in os.walk(a_path): file in files: if file.startswith("nas_archive"): print file, " getting extracted" mytar = tarfile.open(file) member in mytar.getmembers(): if member.isreg(): myname = '/' + member.name path_list = myname.rsplit('/')[1:] print path_list member.name = os.path.basename(member.name) i_base = 'inbound' = -1 index,value in enumerate(path_list): if value == i_base: = index break if == -1: print i_base, " not found" else: path_list.insert(index + 2, '11-15-12') path_list.remove(member.name) print path_list newpath = '/' + os.path.join(*path_list) print newpath mytar.extract(member,newpath)
this script working expected extracting files today's timestamps rather original time stamp. here won't able use extractall method since calculating path separately each file based on original path. there way me restore file new location original timestamps?
you need @ the mtime
attribute of tarinfo
objects:
import tarfile tar = tarfile.open('tarfile.tar') member in tar.getmembers(): print member.name, member.mtime
given mtime
value, can use os.utime
set both access , modification timestamps thus:
import os os.utime('file', (mtime, mtime))
Comments
Post a Comment