How to create File System Backup using Bash Script on Debian 12

To Create File System Backup Using Bash Script On Debian 12

Introduction:

A file system backup is a critical procedure for duplicating and safeguarding vital data from a computer or device to an alternative location. This practice guarantees that in the event the primary data is compromised, destroyed, or corrupted, it can be recovered from the backup. Backups are commonly saved on distinct devices such as external hard drives, cloud storage platforms, or network storage devices. They act as a protective measure against data loss stemming from hardware malfunctions, inadvertent deletions, cyber threats, or unforeseen events. Consistently conducting backups is imperative for safeguarding valuable data and maintaining seamless business operations.

Procedure:

Step 1: Check the OS version by using the following command.

root@linuxhelp:~# cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL=https://bugs.debian.org/

Step 2: Create Destination Directory for the Backup File by using the following command.

root@linuxhelp:~# mkdir -p /var/Backup/CompressedFilesystem

Step 3: Create Sample File System Directory by using the following command.

root@linuxhelp:~# mkdir -p /Backup/Filesystem1/{filesystem1.1,filesystem1.2,filesystem1.3}

Step 4: Again create another File System Directory by using the following command.

root@linuxhelp:~# mkdir -p /Backup/Filesystem2/{filesystem2.1,filesystem2.2,filesystem2.3}

Step 5: Check the Created Directory by using the following command.'

root@linuxhelp:~# ls -la /Backup/Filesystem2
total 20
drwxr-xr-x 5 root root 4096 Jul  7 11:16 .
drwxr-xr-x 5 root root 4096 Jul  7 11:16 ..
drwxr-xr-x 2 root root 4096 Jul  7 11:16 filesystem2.1
drwxr-xr-x 2 root root 4096 Jul  7 11:16 filesystem2.2
drwxr-xr-x 2 root root 4096 Jul  7 11:16 filesystem2.3

Step 6: Create script to take backup automatically by using the following command.

root@linuxhelp:~# vim filesystembackup
#!/bin/bash
# Destination folder path specify using variable
Backup_Destination="/var/Backup/CompressedFilesystem"
# Specify File System with the path using variable
File_System="/Backup/Filesystem1 /Backup/Filesystem2"
# Get date for name of compressed file
Date=$(date +%F)
# Get Hostname for name of compressed file
Hostname=$(hostname -s)
# Specify the structure of compressed file with path using variable
Compressed_File=$Backup_Destination/$Hostname-$Date.tar.gz
# Display Information of Backup Process
echo "Backup Started From $File_System To $Compressed_File"
echo 
echo "Day $(date)"
echo 
# Take Backup using tar command 
tar czf $Compressed_File $File_System
# Display the Information about the Backup Completion
echo "Backup Completed"
echo 
echo "$(date)"
echo
# Display the Backup file with the size in a user readable format
echo 
ls -lh $Backup_Destination
echo

Step 7: Make the executable permission to the script file by using following ocmmand.

root@linuxhelp:~# chmod +x filesystembackup

Step 8: Run the script file by using following command.

root@linuxhelp:~# ./filesystembackup 
Backup Started From /Backup/Filesystem1 /Backup/Filesystem2 To /var/Backup/CompressedFilesystem/linuxhelp-2024-07-07.tar.gz
Day Sunday 07 July 2024 11:33:33 AM IST
tar: Removing leading `/' from member names
Backup Completed
Sunday 07 July 2024 11:33:33 AM IST
total 4.0K
-rw-r--r-- 1 root root 200 Jul  7 11:33 linuxhelp-2024-07-07.tar.gz

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to create File System Backup using Bash script on Debian 12. Your feedback is much welcome.

FAQ
Q
How do I ensure my backups are secure?
A
To ensure backup security, consider encryption for data both in transit (during backup) and at rest (stored backups). Use strong passwords and access controls for backup storage locations. Regularly test and verify backups to ensure they can be successfully restored when needed.
Q
How often should I perform backups?
A
The frequency of backups depends on how often your data changes and how critical it is. For most users, daily or weekly backups are sufficient. For businesses dealing with critical data, backups may be performed more frequently, even multiple times a day.
Q
Where should I store my backups?
A
It's recommended to store backups in multiple locations. Options include external hard drives, network-attached storage (NAS), cloud storage services (like Google Drive, Dropbox, or AWS S3), or offsite backups (physically separate from your primary location to protect against disasters affecting the primary site).
Q
What is the best backup method?
A
The best backup method depends on your needs. Common methods include full backups (copying all data), incremental backups (only new or changed files since the last backup), and differential backups (copies files changed since the last full backup). A combination of these methods often provides robust protection
Q
Why do I need to backup my files?
A
Backups are essential to protect against data loss from hardware failures, accidental deletion, malware attacks, and natural disasters. They ensure you can recover important files and maintain business continuity.