Back Up a Directory in Linux using a Shell Script
No need to download any software
There are several options for backing up your data in Linux. You can use some freely available software programs, such asfwbackupsandSbackup. However, there is a simple method of backing up a directory without installing any extra software.
We will create a shell script using variables, thetarcommand and thedatecommand 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 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 theGnomeenvironment inUbuntu.
First, access your home directory, by selectingHome Folderfrom thePlacesmenu. TheFile Browseropens to your home directory.
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 selectCreate Document | Empty Filefrom the pop-up menu.
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.
For this example, we named our fileuser_guide_backups.sh.
Now we need to add the commands to the file. Right-click on the name of the file and selectOpen with geditfrom the pop-up menu.
The file opens ingedit. Enter the following lines into the file and clickSave. The purpose of each line is listed below.
NOTE:You can also copy the following text and paste it intogedit. Be sure to changeto your username.
Line-by-Line Description
The following table describes what each line is in the shell script file.
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 yourHome Folderagain as mentioned above and right-click on the shell script file. SelectPropertiesfrom the pop-up menu.
ThePropertiesdialog box displays. Make sure theExecutecheck box is selected.
ClickClose.
Running the Shell Script
To run the shell script, open a terminal window by selectingAccessories | Terminalfrom theApplicationsmenu.
When theTerminalwindow opens, you should be in yourHome Folderby default. Typingpwdon the command line and pressing enter confirms this fact. At the prompt, type./user_guide_backups.shand pressEnter.
You should have a.tgzfile in the Backups folder in yourHome 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 theBackupsfolder using theExtract Herecommand.
More information
The links below provide more information about shell scripts, thetaranddatecommands, 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.
Founder of Help Desk Geek and managing editor. He began blogging in 2007 and quit his job in 2010 to blog full-time. He has over 15 years of industry experience in IT and holds several technical certifications.Read Aseem’s Full Bio
Welcome to Help Desk Geek- a blog full of tech tips from trusted tech experts. We have thousands of articles and guides to help you troubleshoot any issue. Our articles have been read over 150 million times since we launched in 2008.
HomeAbout UsEditorial StandardsContact UsTerms of Use
Copyright © 2008-2024 Help Desk Geek.com, LLC All Rights Reserved