redhat

How To Make Writing to .bash_history Immediate

In Red Hat Linux, this is accomplished by adding a small script to the system’s profile.d directory. It is possible to edit existing files like bashrc but you may lose those changes when upgrading. I usually create a custom profile script that contains this as well as other global variables and place it at /etc/profile.d. For this post, we’ll create global-prof.sh and get this setup.

It’s best if you login or su as root before you begin.

Create the file for the script

vim /etc/profile.d/global-prof.sh

Add this to the script

#!/bin/bash

# Location of this script: /etc/profile.d/

# Purpose of script is to makes writing to .bash_history immediate
shopt -s histappend
PROMPT_COMMAND="history -a;$PROMPT_COMMAND"

# End of script

Save the script and exit by typing

:wq!

and press Enter. This writes and quits out of the vim editor.

Set permissions and ownership of file

chown root:root /etc/profile.d/global-prof.sh
chmod 644 /etc/profile.d/global-prof.sh

Make the script executable

chmod +x /etc/profile.d/global-prof.sh

Log completely out, log back in, run a couple of commands (any commands like history, ls, etc.), and then check the history by running:

cat .bash_history

You should see the last couple of commands you just ran.

 

A quick break-down of the script itself

The first command changes the .bash_history file mode to append. And the second configures the ‘history -a’ command to be run at the shell. The ‘-a’ immediately writes the current/new lines to the .bash_history file.

CentOS 7

Simple Backup Script for Apache Web Server /w Mysql DB

Share on LinkedIn

This script may not be as clean as others but it gets the job done.

The purpose of this script is to backup both your entire web directory and database to a compressed file with date stamps. What you do with it from there is up to you.

Target: Web Directory and specific DB Instance – MUST know database name for this script

Environment Settings
Create /temp and /backups directories in /

mkdir temp
mkdir backups

See what’s in the directory by running the ls command

ls

Output will be similar to this:

backups bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys temp tmp usr var

Create Script in / directory

vi My-Backup-Script.sh

The only items that need to be changed in this script are the items in ‘red‘.

#! /bin/bash
TIMESTAMP=$(date +"%F")
BACKUP_DIR=/temp/My-Backup-$TIMESTAMPMYSQL_USER="your-db-username"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="your-db-username-password"
MYSQLDUMP=/usr/bin/mysqldump
DATABASE=your-db-name

mkdir -p "$BACKUP_DIR/mysql"
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD $DATABASE | gzip > "$BACKUP_DIR/mysql/$DATABASE.gz"

mkdir -p "$BACKUP_DIR/web_dir"
SRCDIR=/var/www/html/
DESTDIR=$BACKUP_DIR/web_dir/
FILENAME=My-WWW-Backup-$TIMESTAMP.tgz
tar --create --gzip --file=$DESTDIR$FILENAME $SRCDIR

tar --create --gzip --file=/backups/My-Backup-$TIMESTAMP.tgz $BACKUP_DIR

rm -rf /temp/*

wait
echo "Backup of DB and Web Directory Complete!"

Make Script Executable

chmod +x My-Backup-Script.sh

Run cron job every day at 1130pm

sudo crontab -e

Add the following to your cronjob:

MAILTO=""
30 23 * * * /bin/bash /My-Backup-Script.sh

To Extract Backup files, you will need tar -zxvf for the main file and web directory file. You will need to use gzip -d for the database file.

CentOS 7

Bash Shell Script to Backup a Directory

Share on LinkedIn

Here’s a simple little script to backup your web directory. This works on CentOS 7 with no problem. This script backs up the /var/www/html directory.

Create scripts directory

mkdir /home/scripts

Create directory to store backup files

mkdir /home/backups

Create the script

vi /home/scripts/www-backup.sh
#!/bin/bash
SRCDIR="/var/www/html/"
DESTDIR="/home/backups/"
FILENAME=www-backup-$(date +%-Y%-m%-d).tgz
tar --create --gzip --file=$DESTDIR$FILENAME $SRCDIR

Description of script elements
Source Directory
Destination Directory
Filename to include date stamp
Create and Compress file using tar command

Ensure execute permissions are set on the script

chmod +x www-backup.sh

Execute the script
To run the script, just type the following from within the /home/scripts directory

./www-backup.sh

This will create a file similar to www-backup-2015317.tgz in your /home/backups directory.

To extract or unzip from backup
Navigate to /home/backups and run the following tar command to extract:

tar -zxvf www-backup-2015317.tgz
CentOS 7

Show Display Date And Time For Command History

Share on LinkedIn

How would you display shell command history with date and time under Linux or UNIX operating systems? For instance, CentOS?

If the HISTTIMEFORMAT is set, the time stamp information associated with each history entry is written to the history file, marked with the history comment character. Defining the environment variable as follows:

HISTTIMEFORMAT="%d/%m/%y %T "

OR

echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile

To see history type

history

Sample Output:

1002 29/01/15 11:11:07 hwclock --systohc
1003 29/01/15 11:11:10 date
1004 29/01/15 11:11:22 hwclock -show
1005 29/01/15 11:11:25 hwclock -r
1006 29/01/15 11:11:32 history
1007 29/01/15 11:21:15 ip addr
1008 29/01/15 11:21:19 show ip addr
1009 29/01/15 11:21:28 history | grep ip
1010 29/01/15 11:22:34 ip addr sh
1011 29/01/15 11:22:42 hostname
1012 29/01/15 11:22:49 ifconfig
1013 29/01/15 11:24:01 history

Note: Date and Time will only appear for new commands. All previous commands will be stamped with the time and date you issued the ‘HISTTIMEFORMAT’ command.