There are several options for backing up your data on Linux. You can use some freeware programs like fwbackups and Sbackup However, there is a simple method for backing up a directory without installing additional software.
We’ll create a shell script using variables, tar command and date command to create a dated backup file of a directory with its subdirectories.
A shell script is essentially a file containing a list of commands that are executed in sequence. If you have a series of commands that you regularly run in order, it is helpful to create a shell script that contains those commands. Then you only need to run the script file to execute the commands.
Create shell script file
In this example, we are going to create a shell script to back up the directory containing the files for the user manual. We are using the Gnome environment on Ubuntu.
First, go to your home directory by choosing Home Folder from the Tags menu. The file browser will open in your home directory.
– /
We are going to create a new empty file in which we will enter commands to perform the backup. Right-click in the right pane and select New Document | Clear file in popup menu.
The file is added to the list and is ready to be renamed. Enter a file name with a .sh extension.
In this example, we named our file user_guide_backups.sh.
Now we need to add commands to the file. Right-click the file name and choose Open with gedit from the pop-up menu.
The file opens in gedit. Enter the following lines into the file and click Save. The purpose of each line is shown below.
NOTE. You can also copy the following text and paste it into gedit. Make sure to change to your username.
#! / Bin / bash SRCDIR = “/ home / / Documents / my_work /” DESTDIR = “/ home / / Backups /” FILENAME = ug – $ (date +% – Y% -m% -d) – $ (date +% – T) .tgz tar –create –gzip –file = $ DESTDIR $ FILENAME $ SRCDIR
Description line by line
The following table describes each line in a shell script file.
Line # | Description | ||||||
1 | This line should be the first line in a bash shell script, which is the default script type. | ||||||
2 | This line sets a variable named SRCDIR and sets its value to the directory to be backed up. NOTE. Be sure to replace with your username. |
||||||
3 | This line sets a variable named DESTDIR and sets its value to the directory where the backup file will be written. NOTE. Make sure to replace with your username. |
||||||
4 | This line sets a variable named 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 could be ug-20100212-13: 03: 45.tgz. NOTE. When using a variable, always start with a dollar sign ($). If you are using a command as part of a variable, enclose the command and parameters for the command in parentheses. |
||||||
5 | This line is a tar command with the following function and added parameters.
|
Edit the permissions in the Script Shell 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 above and right click on the shell script file. Select Properties from the pop-up menu.
The Properties dialog box opens. Make sure the Run box is checked.
Click Close.
Run the shell script
To run a shell script, open a terminal window by choosing Accessories | Terminal from the Applications menu.
When the terminal window opens, you should be in your home folder by default. Typing pwd at the command line and pressing Enter confirms this fact. At the command prompt, type ./user_guide_backups.sh and press Enter.
You should have a .tgz file in your Backups folder in your home folder. If you right-click the file name, you will see several options to open the archive in one of the available archiving programs, or to extract the files directly to the Backups folder using the Extract Here command.
more information
The links below provide more information on shell scripts, tar and date commands, and other Linux commands.
Scripts
Scripting
A quick guide to scripting with the bash shell
Bash Shell Scripting – 10 Second Guide | Everything about Linux
Linux Commands
Linux commands
Tar MAN page
Date MAN Page
bash commands – Linux MAN Pages
Examining these pages will help you create your own useful bash shell scripts.
–