Linux Shell Script to Backup Files and Directory

Backing up our important files and directories is crucial to safeguarding our data. In this article, we’ll create a simple shell script that uses the tar utility to create an archive file for backup purposes that we can use whenever we require. The shell script we’ll build allows us to specify which directories to back up and where to store the backup. We’ll use the tar command to create an archive file containing the selected files and directories.

Linux Shell Script to Backup Files and Directory

  • What is a shell script?
  • Backup Script Creation:
    • Step 1: Create a shell script
    • Step 2: Write the script
      • Define the start of the shell script
      • Define the directories to backup
      • Specify the backup destination
      • Create an archive filename based on the current day
      • Backup the files using tar
    • Step 3: Make the script executable
    • Step 4: Run the script from the terminal

What is a shell script?

A shell script is a program used on Unix to automate our work. These scripts are used by people to create scripts for file manipulation, program execution backing up files, etc. The shebang (#!) at the beginning of a script specifies the interpreter to execute the file, allowing it to act as a fully normal Unix command.

Backup Script Creation:

Below is the full process for the creation of a shell script for backing up files and directories in Linux.

Step 1: Create a shell script

To start open a text editor of our choice and create a filename automate.sh. We can either use Nano or Vim or any other text editor of our choice. For this purpose, We will use the nano text editor.

Create a script

Step 2: Write the script

Define the start of the shell script

The shebang (#!) indicates the start of the shell script.

#!/bin/bash

Define the directories to backup

we can define all the directories and files that we want to backup within a variable. we can change the info inside if we want to backup other files or directory.

Here, We am backing up my Downloads folder , boot folder and program.c file from my Home.

backup="/home /kali/Downloads/ /root /boot / /home/kali/program.c"

Specify the backup destination

After that define the destination where the backup file should be stored.We can choose the destination folder wherever we want .Just make sue the folder is writable and we have necessary permission to write in the given folder.

dest="/mnt/backup"

Create an archive filename based on the current day

Then we can specify the day using the date and format specifier %A used to display full weekdays where the full day name will be displayed like Saturday. Then we store only Hostname excluding the domain name using -s command in hostname variable and give the archived file name hostname-day .This Naming convention makes it easier to Know when the Backup was taken and make the backup look cleaner.

day=$(date +%A)
hostname=$(hostname -s)
archive="$hostname-$day.tgz"

Backup the files using tar

we can use tar utility to backup the given file and create a .tgz file and save it for later to restore the backup.And we can also add a echo line to make the user aware of completing of backup.

tar czf $dest/$archive $backup
echo "Backup finished"

After adding all these lines on our script we can save and exit our script.

The overall script will be :

#!/bin/bash
backup="/home /kali/Downloads/ /root /boot / /home/kali/program.c"
dest="/mnt/backup"
day=$(date +%A)
hostname=$(hostname -s)
archive="$hostname-$day.tgz
tar czf $dest/$archive $backup
echo "Backup finished"

script file

Step 3: Make the script executable

we can make the script executable by giving it permission using chmod. we can type following lines in the terminal where chmod is used to change the permission to the file and +x is used to add execute permission.

 sudo chmod+x automate.sh

Giving permission

Step 4: Run the script from the terminal

Finally run the script to make the backup complete without error and we get our backup file named as DESKTOP-K1P15LD-Saturday.tgz at /mnt/backup directory. We can cd into given directory to see our backup file.

 ./automate.sh

Running the script

Now if we want to restore the file backup using the tar utility,we can make use of same utility with different flag and we get our file extracted.

 tar xzf DESKTOP-K1P15LD-Saturday.tgz

Extracting the backup files and directories

Conclusion

In this article, we learn a simple method to create a shell script to automate backup in Linux, we learned the process one by one with steps for each method on how we can create script for backing up files and directories in Linux, we can also customize this script as per our need changing the source and destination , please make sure to follow the entire article to have better understanding.

Script to Backup Files and Directory – FAQs

What does the shebang (#!) at the beginning of the script do?

The shebang specifies the interpreter (shell) to execute the script. Wet allows the script to be run as a normal Unix command.

Can We back up multiple directories using this script?

Yes! Modify the backup_files variable to include additional directories we want to back up.

How can We automate backups using cron?

Edit our crontab (sudo crontab -e) and add an entry to specify when the backup script should run. For example, to run the script daily at midnight:

0 0 * * * /path/to/automate.sh

Can We add the name of file to backup when running the script rather than within script ?

Yes! we can change the backup variable used in tar utility with $1 and remove the backup variable declaration within the script and update the script.