Shell Script to zip users cmd history log files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to zip users cmd history log files
# 1  
Old 02-02-2012
Shell Script to zip users cmd history log files

I admit I am terrible with scripting, so when I was asked to store users' command history lines and zip them on monthly basis what I did was to create a file "user_history_Feb" with the following contents:

Part A
Code:
[root@H99XXX bin]# more user_history_Feb
cp -p /var/log/user_history/*history /var/log/user_history/old_logs/
gzip -9 /var/log/user_history/old_logs/*history
cd /var/log/user_history/old_logs
mv *gz /var/log/user_history/old_logs/02
[root@H99XXX bin]#

and a cronjob for it to run end of Feb:

Part B
Code:
[root@H99 bin]# crontab -l | grep user_history_Feb
01 22 29 2 * /usr/local/bin/user_history_Feb
[root@H99 bin]# uname -a
Linux H99 2.6.18-274.17.1.el5 #1 SMP Wed Jan 4 22:45:44 EST 2012 x86_64 x86_64 x86_64 GNU/Linux
[root@H99 bin]#

I am using RHEL 5.7

I know my "script" is no script, and would be very thankful if someone could help me into scriptifying part A so I can adjust the cron Smilie
# 2  
Old 02-02-2012
PART A:

Script user_whtever.sh
Code:
#!/bin/bash
 
mkdir -p /var/log/user_history/old_logs # If the folder is not present, it will be created automatically
 
cp -p /var/log/user_history/*history /var/log/user_history/old_logs/
gzip -9 /var/log/user_history/old_logs/*history
mv /var/log/user_history/old_logs/*.gz  /var/log/user_history/old_logs/02

After you have created the above script, change its permission:
Code:
 
[root@H99XXX bin]# chmod 750 user_whtever.sh
 
[root@H99 bin]# crontab -l | grep user_
01 22 29 2 * /usr/local/bin/user_whtever.sh

[PS: /var/log/user_history/old_logs/02 is file or folder?]
# 3  
Old 02-02-2012
It's a directory

Code:
[root@H99 ~]# ls -ld /var/log/user_history/old_logs/02
drwxr-xr-x 2 root root 4096 Feb  1 14:52 /var/log/user_history/old_logs/02
[root@H99 ~]# date
Thu Feb  2 16:48:38 JST 2012

I've created all the folders in the script I provided in the post above, but how do I create a subsequent directory according to the next month

e.g 03 for March, 04 for April...

So the end results would be like this

1. all the *history files from /var/log/user_history/ dumped into something like this:

Code:
/var/log/user_history/old_logs/02/*gz
/var/log/user_history/old_logs/03/*gz
/var/log/user_history/old_logs/04/*gz


Last edited by hedkandi; 02-02-2012 at 03:53 AM.. Reason: removed server name
# 4  
Old 02-02-2012
You can do like this for the Destination Directory:

Code:
#!/bin/bash
 
OLD_HIST_DIR="/var/log/user_history/old_logs"
HIST_DIR="/var/log/user_history"
 
MNTH_DEST_DIR="/var/log/user_history/old_logs/$(date +'%m')"
# date +'%m' gives number corresponding to the current month in 01,02,...11,12 fashion
 
mkdir -p "${MNTH_DEST_DIR}"  "${OLD_HIST_DIR}" # This will create the directory if not present
 
cp -p ${HIST_DIR}/*history ${OLD_HIST_DIR}
 
gzip -9 ${OLD_HIST_DIR}/*history

mv ${OLD_HIST_DIR}/*.gz  ${MNTH_DEST_DIR}

This User Gave Thanks to knight_eon For This Post:
# 5  
Old 02-02-2012
hang on, let me try this out on my DEV

---------- Post updated at 01:14 AM ---------- Previous update was at 01:02 AM ----------

For some reason I am getting error and the zipped files are being stored in /var/log/user_history/old_logs/ instead of /var/log/user_history/old_logs/02

The error:

Code:
[root@H99 02]# /usr/local/bin/testing
/usr/local/bin/testing: line 7: fg: no job control
mv: `/var/log/user_history/old_logs/d12adm_history.gz' and `/var/log/user_history/old_logs/d12adm_history.gz' are the same file

the script:

Code:
[root@H99 old_logs]# more /usr/local/bin/testing
#!/bin/bash

OLD_HIST_DIR="/var/log/user_history/old_logs"

HIST_DIR="/var/log/user_history"

MNTH_DEST_DIR="/var/log/user_history/old_logs/$(date +`%m`)"

cp -p ${HIST_DIR}/*history ${OLD_HIST_DIR}

gzip -9 ${OLD_HIST_DIR}/*history

mv ${OLD_HIST_DIR}/*.gz ${MNTH_DEST_DIR}
[root@H99 old_logs]#

---------- Post updated at 01:18 AM ---------- Previous update was at 01:14 AM ----------

OK, I figured out what went wrong. I used the ` ` instead of ' ' for the month

Thanks alot, works like charm! Smilie
# 6  
Old 02-03-2012
Computer how can you fetech the cmd history log

how can you fetech the cmd history log of the all login,where is the path to store the users performed command.Smilietake me know!thank you!
# 7  
Old 02-08-2012
It would be nice if you can sentence your request properly.

I have C Shell and Bash Shell users, whose .cshrc and .bashrc are modified as such

C Shell

Code:
set histfile=/var/log/user_history/db2s12_history

Bash Shell

Code:
export HISTFILE=/var/log/user_history/db2ash_history

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to zip csv files having specific pattern in a directory using UNIX shell script?

I have files in a Linux directory . Some of the file is listed below -rw-rw-r--. 1 roots roots 0 Dec 23 02:17 zzz_123_00000_A_1.csv -rw-rw-r--. 1 roots roots 0 Dec 23 02:18 zzz_121_00000_A_2.csv -rw-rw-r--. 1 roots roots 0 Dec 23 02:18 zzz_124_00000_A_3.csv drwxrwxr-x. 2 roots roots 6 Dec 23... (4 Replies)
Discussion started by: Balraj
4 Replies

2. Shell Programming and Scripting

Using Shell Script in place of Perl script to Unzip the zip files.

Hi Expert, We have some shell scripts which Internally uses Perl Script to Unzip the source zip files which comes to inbound directory. So now our requirement is to avoid the dependency on Perl Script and us Shell Script to unzip the files. I have the Perl script with me attached can some one... (3 Replies)
Discussion started by: naveen.dasu
3 Replies

3. UNIX for Dummies Questions & Answers

[Solved] History cmd

When using the history cmd it appears to be limited to the last 16 lines. Is it possible to increase this limit? (6 Replies)
Discussion started by: nosuchluck
6 Replies

4. Shell Programming and Scripting

how to use reset_server (siebel server) cmd in shell script

hello frnds, im using a script to stop, reset and to start the siebel server.. i'm able to extract the enterprise name, siebel server name and siebel root path.. when i try to use these info in the script im unable to perform the reset_server -r <siebel root> -e <enterprise name> +-M... (3 Replies)
Discussion started by: kaushik_87
3 Replies

5. Shell Programming and Scripting

Shell script for to view all users & groups history in root

Dear All, I want to know all users & group history in one file, for root terminal through shell or any other option (5 Replies)
Discussion started by: kpoobathi
5 Replies

6. UNIX for Dummies Questions & Answers

How to zip up a series of log files in one shot?

Hi, I'm trying to zip up the first half year's application logs into one zip file. The logs are in the format of log.2011-01-01 to log.2011-06-30. I tried to use this command at ksh: zip logs.jan-jun2011.zip *2011-01* *2011-02* *2011-03* *2011-04* *2011-05* *2011-06* It works. However, I... (4 Replies)
Discussion started by: hyang
4 Replies

7. Shell Programming and Scripting

To write a shell script which groups files with certain pattern, create a tar and zip

Hi Guru's, I have to write a shell script which groups file names based upon the certain matching string pattern, then creates the Tar file for that particular group of files and then zips the Tar file created for the respective group of files. For example, In the given directory these files... (3 Replies)
Discussion started by: rahu_sg
3 Replies

8. Shell Programming and Scripting

open terminal to run cmd using shell script

i want the shell script to open the terminal and in that terminal i want to run a command specified in the script... how can it be done... (2 Replies)
Discussion started by: chandrabhushan
2 Replies

9. Shell Programming and Scripting

batch shell script to zip individual files in directory - help

help trying to figure out a batch shell script to zip each file in a directory into its own zip file using this code but it does not work tryed this also nothing seems to work , just ends without zipping any files i have over 3000 files i need to zip up individualy ... (7 Replies)
Discussion started by: wingchun22
7 Replies

10. Shell Programming and Scripting

Bourne Shell script - log for users loggin on and off

Hello all, I'm new to shell scripting and want to make a script that I can write to log the users logging on and off the a unix system. I have had a good look over the past few days to crack it, I think I am getting close. I want a script that runs an infinite loop to check every 5 seconds... (14 Replies)
Discussion started by: noodlesoup
14 Replies
Login or Register to Ask a Question