-
Notifications
You must be signed in to change notification settings - Fork 19
/
restore_unixstat_to_resource.py
80 lines (67 loc) · 2.89 KB
/
restore_unixstat_to_resource.py
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
import argparse
import os
import subprocess
from irods.session import iRODSSession
def log(x):
if (args.verbose == True):
print(x)
def recover_collection(c):
log(['collection',c.path])
for d in c.data_objects:
recover_data_object(d)
for s in c.subcollections:
recover_collection(s)
def recover_data_object(d):
log(['data_object', d.path])
for r in d.replicas:
if (r.resource_name == args.target_resource):
for m in d.metadata.items():
if (m.name == 'filesystem::perms'):
log(['PERMS',m.value,r.path])
subprocess.Popen(['chmod',m.value,r.path]).communicate()
if (m.name == 'filesystem::owner'):
log(['OWNER',m.value,r.path])
subprocess.Popen(['chown',m.value,r.path]).communicate()
if (m.name == 'filesystem::group'):
log(['GROUP',m.value,r.path])
subprocess.Popen(['chgrp',m.value,r.path]).communicate()
if (m.name == 'filesystem::atime'):
log(['ATIME',m.value,r.path])
subprocess.Popen(['touch','-c',r.path,'-a','-d',m.value]).communicate()
if (m.name == 'filesystem::mtime'):
log(['MTIME',m.value,r.path])
subprocess.Popen(['touch','-c',r.path,'-m','-d',m.value]).communicate()
# set up arguments
d = '''Recover filesystem attributes for iRODS data objects.
Will restore permissions, owner, group, atime, and ctime from
the iRODS Catalog to files located on target_resource.
Must be run as root (due to chown).
1) Create valid local iRODS environment JSON file (e.g. rootenv.json)
{
"irods_host": "x.x.x.x",
"irods_port": 1247,
"irods_user_name": "xxxxx",
"irods_zone_name": "xxxxxZone",
"irods_authentication_file": "/full/path/to/rootA"
}
2) Initialize iRODS authentication file
$ sudo IRODS_ENVIRONMENT_FILE=rootenv.json iinit
Enter your current iRODS password:
3) Execute this script via sudo
$ sudo IRODS_ENVIRONMENT_FILE=rootenv.json python restore_unixstat_to_resource.py <logical_path> <resource_name>
'''
parser = argparse.ArgumentParser(description=d, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('logical_path', help='Logical Path (Collection)')
parser.add_argument('target_resource', help='Target Resource Name')
parser.add_argument('-v', '--verbose', help="print more information to stdout", action="store_true")
args = parser.parse_args()
# get environment
try:
env_file = os.environ['IRODS_ENVIRONMENT_FILE']
except KeyError:
env_file = os.path.expanduser('~/.irods/irods_environment.json')
# connect and apply catalog filesystem metadata to collection
with iRODSSession(irods_env_file=env_file) as session:
coll = session.collections.get(args.logical_path.rstrip('/'))
log(['initial',coll.path])
recover_collection(coll)