There are several options for backing up your data in Linux. You can use some freely available software programs, such as fwbackups and Sbackup. However, there is a simple method of backing up a directory without installing any extra software.

We will create a shell script using variables, the tar command and the date command to create a dated backup file of a directory, with its subdirectories.

Table of Contents

    A shell script is essentially a file containing a list of commands that are run in sequence. If you have a series of commands you regularly run in order, it is helpful to create a shell script containing these commands. Then, you only have to run the script file to run the commands.

    Creating the Shell Script File

    For this example, we are going to create a shell script to backup a directory containing files for a user guide. We are using the Gnome environment in Ubuntu.

    First, access your home directory, by selecting Home Folder from the Places menu. The File Browser opens to your home directory.

    Opening the home folder

    We are going to create a new empty file in which we will enter the commands for performing the backup. Right-click in the right pane and select Create Document | Empty File from the pop-up menu.

    Creating an empty shell script file

    A file is added to the list and is ready to be renamed. Type in a name for the file, giving the file an extension of .sh.

    New file ready to be renamed

    For this example, we named our file user_guide_backups.sh.

    Shell script file renamed

    Now we need to add the commands to the file. Right-click on the name of the file and select Open with gedit from the pop-up menu.

    Opening shell script file with gedit

    The file opens in gedit. Enter the following lines into the file and click Save. The purpose of each line is listed below.

    NOTE: You can also copy the following text and paste it into gedit. Be sure to change <username> to your username.

    #!/bin/bash
     SRCDIR="/home/<username>/Documents/my_work/"
     DESTDIR="/home/<username>/Backups/"
     FILENAME=ug-$(date +%-Y%-m%-d)-$(date +%-T).tgz
     tar --create --gzip --file=$DESTDIR$FILENAME $SRCDIR

    Editing shell script file in gedit

    Line-by-Line Description

    The following table describes what each line is in the shell script file.

    Line # Description
    1 This line must be the first line in a bash shell script, which is the default type of script.
    2 This line sets up a variable called SRCDIR and sets the value of it to the directory to be backed up.
    NOTE: Be sure to replace <username> with your username.
    3 This line sets up a variable called DESTDIR and sets the value of it to the directory into which the backup file will be written.
    NOTE: Be sure to replace <username> with your username.
    4 This line sets up a variable called FILENAME and sets the value using text and variables containing the date command to add the current date and time to the filename. For example, the filename might be ug-20100212-13:03:45.tgz.
    NOTE: When using a variable, always start it with the dollar sign ($). If you use a command as part of a variable, enclose the command and the options for the command in parentheses.
    5 This line is the tar command with the following function and options added.

    –create This function creates a new archive (or truncates an old one if the filename specified already exists) and writes the named files or directory to it.
    –gzip This option tells tar to compress the files in the archive using the gzip utility.
    –file This option gives tar the filename to use. In this case, we assembled the filename using the DESTDIR, FILENAME, and SRCDIR variables.

    Editing the Permissions on the Shell Script File

    Before running your script, you need to make sure the file has the correct permissions. To do this, open your Home Folder again as mentioned above and right-click on the shell script file. Select Properties from the pop-up menu.

    Getting the properties of the shell script file

    The Properties dialog box displays. Make sure the Execute check box is selected.

    Shell script file permissions

    Click Close.

    Running the Shell Script

    To run the shell script, open a terminal window by selecting Accessories | Terminal from the Applications menu.

    Opening a Terminal window

    When the Terminal window opens, you should be in your Home Folder by default. Typing pwd on the command line and pressing enter confirms this fact. At the prompt, type ./user_guide_backups.sh and press Enter.

    Running the shell script

    You should have a .tgz file in the Backups folder in your Home Folder. If you right-click on the filename, you see several options for opening the archive in one of the available archiving programs, or extracting the files directly to the Backups folder using the Extract Here command.

    Pop-up menu options for .tgz file

    More information

    The links below provide more information about shell scripts, the tar and date commands, and other Linux commands.

    Scripting

    A quick guide to writing scripts using the bash shell

    Bash Shell Scripting – 10 Seconds Guide | All about Linux

    Bash Reference Manual

    Linux Commands

    tar MAN Page

    date MAN Page

    bash commands – Linux MAN Pages

    Exploring these pages will help you to construct your own useful bash shell scripts.

    Leave a Reply

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