Shell Script to save/restore files and dir permissions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to save/restore files and dir permissions
# 1  
Old 09-09-2007
Shell Script to save/restore files and dir permissions

Hello all:

I need found script to save and restore file permissions and owner:group... this is for backup my server...

I have one problem in my server and file permissions and owner:group, mess up..

For this reason i need found one way to secure this aspect of the server...

Thanks!
# 2  
Old 01-13-2009
Wrote this little guy up for you

Smilie

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 (<STATE_IN>)
{
  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 #>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to find files in dir and updation

Hi, I have the shell script requirement mentioned below : List all java and c files or all files in directory and sub directories' in folder structure in current dir. then search for pattren1 in all files globally and replace with other string . And also check the date... (3 Replies)
Discussion started by: ammulu
3 Replies

2. Shell Programming and Scripting

Save all the file of current date in another dir?

Hi i want to copy all the files of current date in another directory. for example, below i want to save all the file of 26 march to copied in debug dir. $ ls -lrt | tail -5 -rwxrwxrwx 1 khare guest 73 Jan 6 12:35 chk -rw-r--r-- 1 khare guest 770 Mar 26 02:21 cc1... (2 Replies)
Discussion started by: scriptor
2 Replies

3. HP-UX

How to restore standard default permissions on /etc?

Hello HP Admins, Somehow Permissions of files inside /etc set to 777 in HP-UX 11. Due to this, system malfunctioning. I would like to know if there exists any command/script that sets all system files (or /etc atleast) to its default standard permissions. I just don't want to run 755 on /etc... (3 Replies)
Discussion started by: prvnrk
3 Replies

4. Shell Programming and Scripting

Help- writing to dir with no write permissions (script)

hello, I have a question regarding the below script.. I just simply trying to write to a file in directory that does not have write permissions but this is failing .. it still writes to it regardless .. Many thanks in advance.. #!/bin/bash #check if dir exists, if so write to it ... (5 Replies)
Discussion started by: Reb0rn
5 Replies

5. Shell Programming and Scripting

Script to move all files in a dir into a certain dir

Hello all, I'm very new to shell scripting and need quite urgently to do this thing for my student job. I have a directory called "vectors" with a bunch of files all named ".vector". also i have for each of those files a directory with the name . I now want to move each of those *.vector files... (2 Replies)
Discussion started by: sherresh
2 Replies

6. Shell Programming and Scripting

Get Permissions and save to data

Hi all; I have the following code which gives me kind of what I need: #!/usr/bin/perl use Fcntl ':mode'; # if ($ARGV ne "") { $filename = $ARGV; } else { print "Please specify a file!\n"; exit; } # if... (2 Replies)
Discussion started by: gvolpini
2 Replies

7. Shell Programming and Scripting

Have files take on DIR permissions

Hello all; I have a question based on a ticket I have from a user... K...first some background...this client needed a generic user setup to allow them to ftp files; user is called "msifpt"...so here's what they want to do now with this account: 1. Client will use the user id ‘msiftp'... (1 Reply)
Discussion started by: gvolpini
1 Replies

8. Shell Programming and Scripting

Need a script to move the files from one dir to other other dir

Need a script to move the files from one dir to other dir and at the same time it has to read the log in the source dir. Please help me ASAP. (4 Replies)
Discussion started by: viswanathkishor
4 Replies

9. Shell Programming and Scripting

Dir permissions

Hi All, I have a script which will be run by another user say user 2 which will create a new dir say dir 4 in my work place and copy certains files from his dir. /t1/t2/t3/dir4 I want to allow the user to create the dir dir4 and copy the files,but bring the control back to my name say user... (2 Replies)
Discussion started by: prasperl
2 Replies

10. Shell Programming and Scripting

A script to find dir, delete files in, and then del dir?

Hello!! I have directories from 2008, with files in them. I want to create a script that will find the directoried from 2008 (example directory: drwxr-xr-x 2 isplan users 1024 Nov 21 2008 FILES_112108), delete the files within those directories and then delete the directories... (3 Replies)
Discussion started by: bigben1220
3 Replies
Login or Register to Ask a Question