Creating Secure Backups using Tar Command!

Creating Secure Backups using Tar Command!

Β·

2 min read

πŸ‘‹ Hey, fellow Linux enthusiasts! Today, we're diving into the world of creating backups in Linux, and we're making it as easy as enjoying your morning coffee! β˜•οΈπŸ§

Why Backup? πŸ€”

Data is precious, and accidents happen. Whether it's a sudden system crash or accidental deletion, keeping a backup is like a safety net for your files. Let's start the journey to create backup easily!

Step 1: Set the Stage 🎭

First, we need to identify our star performers:


#!/bin/bash

# Source directory to be backed up
source_directory="/path/to/source"

# Backup destination directory
backup_directory="/path/to/backup"

πŸ’‘ Explanation:

  • source_directory: This is where your important files reside.

  • backup_directory: The safe haven where we'll store our backup.

Step 2: Add Some Flair 🌈

Now, let's add a dash of uniqueness with a timestamp!

ashCopy code# Create a timestamp for the backup file
timestamp=$(date +"%Y%m%d_%H%M%S")

πŸ’‘ Explanation:

  • timestamp: Think of it as a time-stamp that uniquely identifies our backup. It's like naming our files with a date and time signature.

Step 3: Name the Hero πŸ¦Έβ€β™‚οΈ

Next, let's give our backup a superhero name!

bashCopy code# Backup filename with timestamp
backup_filename="backup_$timestamp.tar.gz"

πŸ’‘ Explanation:

  • backup_filename: The name we're giving to our backup. It's like calling our superhero to the rescue! The .tar.gz indicates it's a compressed archive.

Step 4: The Grand Finale πŸš€

Now, let's create the backup using the trusty tar command:

bashCopy code# Create the backup using tar
tar -czf "$backup_directory/$backup_filename" -C "$source_directory" .

πŸ’‘ Explanation:

  • tar: This command is like a magical bag that packs everything neatly.

  • -czf: These options tell tar to create a compressed archive file.

  • $backup_directory/$backup_filename: The destination where our backup will be stored.

  • -C "$source_directory" .: Specifies the source directory to be backed up. The dot . at the end ensures all files and directories inside are included.

Step 5: Celebrate! πŸŽ‰

bashCopy codeecho "Backup created: $backup_directory/$backup_filename"

πŸ’‘ Explanation:

  • A little victory shout-out! This line tells us that our backup has been successfully created and gives us the path to find it.

And there you have it – a backup script that's as easy as pie! Use it wisely, run it regularly, and rest easy knowing your data is safe and sound. Happy backing up! πŸš€

Did you find this article valuable?

Support CodeAryan by becoming a sponsor. Any amount is appreciated!

Β