Shell Scripts

April 13, 2019

In Linux, most of the console commands that can be run from the command line can be scripted and run by an automation program called shell. The shell reads this file and carries out the commands as though they have been entered directly on the command line.

Shell Script Commands

Create/edit a script file using text editor like Nano.

nano /home/semaphore/backups/ontology.sh

Make the shell script executable.

chmod u+x /home/semaphore/backups/ontology.sh

Run the shell script manually by using the sh command

sh thescript.sh

or run without using command by referencing the script from outside current directory

./thescript.sh

Shell Script Examples

Backup script

#!/bin/bash
# What to backup.
type="DEV"
file="semaphoreWorkbenchHome"
source="/opt/semaphore/$file"

# Where to backup to.
destination="/opt/semaphore"

# Current date (yyyymmdd)
day=$(date +%Y%m%d)

# Create archive filename.
archive="$source-$day$type.tgz"

# Print start status message.
echo "Backing up $source to $archive"
date
echo

# Backup files locally using tar.
tar czf $archive $source

# Backup the files to remote server using tar and ssh.
#tar czf - $archive | ssh semaphore@u90403.providence.org "cat > $archive"

# Copy the Backup the files to remote server using tar and ssh.
sudo scp -r $archive.tgz semaphore@u9451.wa.providence.org:$archive

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in destination to check file sizes.
ls -lha $destination

References

Backup Shell Scripts – MySQL

Backup Shell Scripts

Leave a Reply

Your email address will not be published. Required fields are marked *