Skip to content

5000+ Great Articles

Best Articles & Essays: Interesting Articles to Read Online

  • Best business calculator 2020 black
  • Increase Convenience and Productivity with Android’s Picture-in-Picture Mode Smartphones
  • Five Differences Between Static IP and Dynamic IP Computer Tips
  • How to Upload and Tag Pictures and Photos in FaceBook technology
  • Can you browse the Internet on Amazon Fire tablet? Reviews
  • Here are the Top 10 highest paid YouTubers of 2017 How-To
  • sorry, no manipulations with clipboard allowed macOS
  • 6 Tips To Manage Your Massive Gaming Backlog Gaming

Back Up a Directory in Linux using a Shell Script

Posted on October 9, 2020 By blog_786 No Comments on Back Up a Directory in Linux using a Shell Script

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.

– /

Back Up a Directory in Linux using a Shell Script

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.

Back Up a Directory in Linux using a Shell Script

The file is added to the list and is ready to be renamed. Enter a file name with a .sh extension.

Back Up a Directory in Linux using a Shell Script

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

Back Up a Directory in Linux using a Shell Script

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

Back Up a Directory in Linux using a Shell Script

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

Back Up a Directory in Linux using a Shell Script

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.

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

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.

Back Up a Directory in Linux using a Shell Script

The Properties dialog box opens. Make sure the Run box is checked.

Back Up a Directory in Linux using a Shell Script

Click Close.

Run the shell script

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

Back Up a Directory in Linux using a Shell Script

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.

Back Up a Directory in Linux using a Shell Script

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.

Back Up a Directory in Linux using a Shell Script

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

Bash Reference Manual

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.

–

Share this:

  • Twitter
  • Facebook
Linux Tips

Post navigation

Previous Post: Block File Downloads in Internet Explorer
Next Post: Add Shortcuts to the Right-Click Context Menu in Ubuntu

Related Posts

  • How To Install a Linux OS On Your Android Phone Linux Tips
  • How to Install Ubuntu in VirtualBox Linux Tips
  • How to Find, Download, and Install Custom Icons in Linux Linux Tips
  • HDG Explains : What Is Sudo & Root On Linux? Linux Tips
  • Fedora vs Ubuntu 2021 Linux Tips
  • An Introduction to Linux for Beginners Linux Tips

Leave a Reply Cancel reply

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

Archives

  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • March 2021
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • December 2019
  • July 2019
  • May 2019
  • April 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018

Categories

  • AI Tools & Guides
  • Amazon Web Services
  • Apple Watch
  • Computer Tips
  • Cool Websites
  • Featured Posts
  • Free Software Downloads
  • Gadgets
  • Gaming
  • General Software
  • Google Software/Tips
  • Hardware
  • Help Desk
  • How-To
  • iOS
  • iPad
  • iPhone
  • islamic Books
  • Linux
  • Linux Tips
  • Mac OS X
  • macOS
  • MS Office Tips
  • Networking
  • Office Tips
  • OS X
  • Product Reviews
  • Reviews
  • Safari
  • Smart Home
  • Smartphones
  • Software Reviews
  • technology
  • text
  • Tools Review
  • Troubleshooting
  • Tutorials
  • Uncategorized
  • Urdu Books PDF
  • Web Site Tips
  • Windows
  • Windows 10
  • Windows 7
  • Windows XP Tips
  • Wordpress
  • How to reset your SIM card
  • This Simple Trick Lets You Play YouTube in the Background on iOS
  • How to go Back to the old YouTube Layout (2013)
  • How to Bypass Chromecast DNS and Circumvent Geo Blocking
  • 5 Cool Websites to Find Good Movies and TV Shows on Netflix
DMCA.com Protection Status

Recent Posts

  • Pokemon Sleep: what is it and how to play
  • How to Respond to Messages on Instagram (Mobile and PC)
  • How to reset your SIM card
  • This Simple Trick Lets You Play YouTube in the Background on iOS
  • How to Bypass Chromecast DNS and Circumvent Geo Blocking

Recent Comments

  1. This Simple Trick Lets You Play YouTube in the Background on iOS on YouTube Not Working? Here Are Quick Fixes To Try
  2. How to Bypass Chromecast DNS and Circumvent Geo Blocking on 5 Cool Websites to Find Good Movies and TV Shows on Netflix
  3. 5 Cool Websites to Find Good Movies and TV Shows on Netflix on check netflix video quality internet explorer
  4. SmartDNS vs VPN “What’s the Difference?” on How to Watch Apple TV on Roku
  5. How to go Back to the old YouTube Layout (2013) on 10 Things to Check Before Publishing a YouTube Video
  • How To Make a One Page Landscape In Word MS Office Tips
  • How to Block Websites on Google Chrome on Windows How-To
  • Fix “Unsupported wireless network device detected. System Halted” Error Help Desk
  • What to Look for When Buying PC Speakers Reviews
  • How to Play Windows Games on Mac 2022 macOS
  • 12 Places for the Best Free YouTube Movies for Kids technology
  • How to Fix If Apple TV Not Responding to Remote? Hardware
  • How to Quickly Unsubscribe From Bulk Mail How-To

Copyright © 2023 How To Blog.

Powered by PressBook News WordPress theme

Go to mobile version