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.
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"
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
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
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
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
Contact Us