Writing Your First Shell Script: Backing up Multiple Folders

Jan 22, 2015 00:00 · 575 words · 3 minute read Bash

Preface

One of the most useful features the command line is repeatability. In conversation we don’t often enjoy having to repeat ourselves but so many people will repeat themselves daily on the command line. In general, most things are actually fairly easy to script since all you do is make a file that contains the exact commands that you would type out in the shell. If you are ever curious to know if it it is worth the time to automate a task just consult the link to see how much time you could save. This little tutorial is for someone who has never written a shell script on linux or mac and would like a quick how-to to show them how those commands they type everyday could be scripted for ease.

Basic Script Example

Script, the word itself sounds so daunting to beginner. We hear references all the times to scripts but not everyone understands what a script actually is. It’s quite literally a text file with some instructions written inside of it. Here’s a small example, sometimes I want to clear out a bunch of temporary vim files, I type in each of the following commands to the terminal:

$ cd ~/.vim/swap-files
$ rm *.swp

These commands are short but the premise here is to keep things simple. I could create a new text file called my-script.sh and enter the following content:

#!/bin/bash
cd ~/.vim/swap-files
rm *.swp

You enter the first line of code at the top to tell the system which interpreter to use, more detailed answers. That’s it, 3 lines of text and you have already created your first script, it really is that simple. Now if you want to make the script executable you will have to enable the execute flag permission on the file with this command:

> chmod +x my-script.sh

You now have your very first executable shell script. You can now hopefully see how useful this could be when you have something to do that takes more that 10 commands, like say setting up a server. To run your script maked sure that you are in the same directory and type:

> ./my-script

You should now see that all the files in ~/.vim/swap-files is now empty. If you don’t have vim files to clear you can simply create a dummy folder with some dummy files to test the script on.

A Backup Script

In the previous example I showed how to automate a really simple process but in actual practice you will probaly more complexity. This example will give you some insight on how to create more robust scripts. I will try to explain as much as possible to give you a good understanding of what is going on. This example script is modified from House of Lan.

#!/bin/bash
# This was taken from http://hol.net.nz/blog/backup-your-lamp and modified

#Setting up some variables of places we want to back up 
HOME="/home/"
ETC="/etc/"
WEB="/var/www/"

#Backup Destination folder variable
BACKTO="/mnt/backup-mount/"

#Variable for the name of the folder that will house the backups
BACKUPNAME="backup-"$(date +"%Y-%m-%d")

#Create the Backup Directory
mkdir $BACKTO$BACKUPNAME

#Create zips of the folders we preivously defined
zip -r $BACKTO$BACKUPNAME/home.zip $HOME
zip -r $BACKTO$BACKUPNAME/etc.zip $ETC
zip -r $BACKTO$BACKUPNAME/web.zip $WEB

#MySQL login credentials
MUSER="username"
MPASS="password"

#MySQL dump
mysqldump -u $MUSER -p $MPASS --all-databases > $BACKTO$BACKUPNAME/dumpfile.sql

Now that you’ve gone through the basics you should be able to start writing your own scripts which will hopefully save you time and prevent headaches.