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.

Learn Python For Free

I’m constantly working on honing my existing skill but I’m just as active in learning new ones. I try to follow industry trends by staying active in a handful of forums and get a ton of email subscriptions. So many it’s hard to even keep up most of the time.

One very important skill set to learn or stay current with as a System Administrator, System Engineer, Cloud Engineer, etc is knowing how to script. I’ll be the first to admit that scripting is not currently one of my strengths. I’ve worked with Bash Shell scripting and PowerShell a little. I’m making the plunge to learn Python. It’s a programming language and a very powerful scripting tool. I’ve heard beginners find the clean syntax and indentation structure easy to learn.

I will update this post with new information as it comes available. Maybe some of my readers will be willing to add some resources they’ve used to learn Python. In any case, here’s a good starting point:

Get your resources here:
Python.org

Learn Python basics here:
Learn Python at CodeAcademy.com

Test your code, not just Python!
Coding Ground

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