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

Batch Files – Create New Folder Using Current Date and Time in Folder Name

Share on LinkedIn

This would be great for backup jobs or copying files where you want to keep track of when it was done.

In my simple batch file, I’m copying contents from a mapped network drive to my local machine.

Originally, I was using the following command:

xcopy S: C:\Users\james\Documents\Backup_WIN_Server\SysAdmin /c /v /f /i /s /e /h /y

If using this for a simple form of backup, the data will be overwritten each time I run the batch file.

In order to create a new directory each time with a time/date stamp, all I need to do is use a substring and the built-in %DATE% and %TIME% variables. In this example, I’m just adding the substring before my xcopy command.

@echo OFF
:: Use date /t and time /t from the command line to get the format of your date and
:: time; change the substring below as needed.
:: This will create a timestamp like yyyy-mm-dd-hh-mm-ss.
set TIMESTAMP=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%
@echo TIMESTAMP=%TIMESTAMP%

 

Finally, I just need to add that timestamp to my destination folder as such:

xcopy S: C:\Users\james\Documents\Backup_WIN_Server\"%1\%TIMESTAMP%-SysAdmin" /c /v /f /i /s /e /h /y
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.