Tuesday, August 10, 2010

save file permission

saveState.pl






Code:

my $tree = $ARGV[0];



foreach $file (`/usr/bin/find ${tree}`)

{

chomp($file);

(undef, $inode, $mode, undef, $uid, $gid, $undef, $size, undef, undef, undef, undef, undef) = stat($file);

$permissions = $mode & 07777;

printf "%i:%i:%04o:%i:%i:%s\n", $inode, $size, $permissions, $uid, $gid, $file;

}

applyState.pl





Code:

#Does not save ACL...



my $stateFile = $ARGV[0];



open(STATE_IN, "<$stateFile");



foreach $line ()

{

chomp($line);

my ($inode, $size, $permissions, $uid, $gid, $file) = split(':', $line, 6);

if( -f $file )

{

#add inode / size check here if desired

chmod(oct($permissions), $file);

chown($uid, $gid, $file);

}

}



close(STATE_IN);

########################## Example #################





Code:

9:45am root@hyperion /research/src/state_saver #>perl saveState.pl /research/src/state_saver/ > test



9:45am root@hyperion /research/src/state_saver #>cat test

1846211:512:0755:0:0:/research/src/state_saver/

1846214:0:0644:0:0:/research/src/state_saver/test

1846213:365:0644:0:0:/research/src/state_saver/applyState.pl

1846212:332:0644:0:0:/research/src/state_saver/saveState.pl



9:45am root@hyperion /research/src/state_saver #>l

total 10

1846211 drwxr-xr-x 2 root root 512 Jan 13 09:45 .

1846210 drwxr-xr-x 3 root root 512 Jan 13 09:30 ..

1846213 -rw-r--r-- 1 root root 365 Jan 13 09:44 applyState.pl

1846212 -rw-r--r-- 1 root root 332 Jan 13 09:44 saveState.pl

1846214 -rw-r--r-- 1 root root 219 Jan 13 09:45 test



9:45am root@hyperion /research/src/state_saver #>chmod 777 saveState.pl



9:45am root@hyperion /research/src/state_saver #>l

total 10

1846211 drwxr-xr-x 2 root root 512 Jan 13 09:45 .

1846210 drwxr-xr-x 3 root root 512 Jan 13 09:30 ..

1846213 -rw-r--r-- 1 root root 365 Jan 13 09:44 applyState.pl

1846212 -rwxrwxrwx 1 root root 332 Jan 13 09:44 saveState.pl

1846214 -rw-r--r-- 1 root root 219 Jan 13 09:45 test



9:45am root@hyperion /research/src/state_saver #>perl applyState.pl test



9:46am root@hyperion /research/src/state_saver #>l

total 10

1846211 drwxr-xr-x 2 root root 512 Jan 13 09:45 .

1846210 drwxr-xr-x 3 root root 512 Jan 13 09:30 ..

1846213 -rw-r--r-- 1 root root 363 Jan 13 09:46 applyState.pl

1846212 -rw-r--r-- 1 root root 332 Jan 13 09:44 saveState.pl

1846214 -rw-r--r-- 1 root root 219 Jan 13 09:45 test



9:46am root@hyperion /research/src/state_saver #>
 
 
 
 
--------------------------------------
http://www.unix.com/shell-programming-scripting/42750-shell-script-save-restore-files-dir-permissions.html
_____________________________

No comments:

Post a Comment