Malcolm 'Max' DeRungs

Shell scripts

Linux shell scripts read and carry out the console commands from a .sh file as though they have been entered directly on the command line.

Shell Script Commands

Create/edit a script file using the VI editor
.vi /home/ontology.sh

Make the shell script executable
.chmod u+x /home/ontology.sh

Run the shell script manually
sh ontology.sh

Run by referencing the script from outside current directory
`../ontology.sh

Examples

Backup script
#!/bin/bash

# What to backup
type="DEV"
file="workbench"
source="/opt/$file"

# Where to backup to
destination="/opt"

# 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 user@servername "cat > $archive"

# Copy the Backup the files to remote server using tar and ssh
sudo scp -r $archive.tgz user@servername:$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 MySQL

Use SCP without a password for backups