0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Backup Files Easily and Safely

Use a Bash script like cp -r /path/to/source /path/to/backup/backup_$(date +%Y%m%d_%H%M%S) to copy files into a timestamped backup folder.
📋

Examples

Input/home/user/documents
OutputCopies all files from /home/user/documents to /home/user/backup/backup_YYYYMMDD_HHMMSS
Input/var/log
OutputCopies all log files from /var/log to /var/backups/backup_YYYYMMDD_HHMMSS
Input/tmp/emptyfolder
OutputCreates an empty backup folder /backup/backup_YYYYMMDD_HHMMSS with no files if source is empty
🧠

How to Think About It

To backup files with a Bash script, think about copying the source files to a safe place. Use a timestamp in the backup folder name to avoid overwriting old backups. The script should handle folders and files recursively.
📐

Algorithm

1
Get the source directory path from the user or set it in the script
2
Create a backup directory with a timestamp to keep backups unique
3
Copy all files and folders from the source to the backup directory
4
Print a message confirming the backup location
💻

Code

bash
#!/bin/bash

SOURCE_DIR="$1"
BACKUP_DIR="$HOME/backup/backup_$(date +%Y%m%d_%H%M%S)"

mkdir -p "$BACKUP_DIR"
cp -r "$SOURCE_DIR"/* "$BACKUP_DIR"/
echo "Backup completed to $BACKUP_DIR"
Output
Backup completed to /home/username/backup/backup_20240601_123456
🔍

Dry Run

Let's trace backing up /home/user/docs through the script

1

Set source and backup paths

SOURCE_DIR="/home/user/docs" BACKUP_DIR="/home/user/backup/backup_20240601_123456"

2

Create backup directory

mkdir -p /home/user/backup/backup_20240601_123456

3

Copy files

cp -r /home/user/docs/* /home/user/backup/backup_20240601_123456/

StepActionValue
1SOURCE_DIR/home/user/docs
1BACKUP_DIR/home/user/backup/backup_20240601_123456
2mkdirCreated backup directory
3cpCopied all files from source to backup
💡

Why This Works

Step 1: Create unique backup folder

Using $(date +%Y%m%d_%H%M%S) adds a timestamp so each backup folder is unique and old backups are not overwritten.

Step 2: Copy files recursively

The cp -r command copies all files and subfolders from the source to the backup folder, preserving the structure.

Step 3: Confirm backup completion

Printing a message with the backup path helps the user know where the files were saved.

🔄

Alternative Approaches

Using tar to create a compressed backup
bash
#!/bin/bash
SOURCE_DIR="$1"
BACKUP_FILE="$HOME/backup/backup_$(date +%Y%m%d_%H%M%S).tar.gz"
tar -czf "$BACKUP_FILE" -C "$SOURCE_DIR" .
echo "Backup archive created at $BACKUP_FILE"
Creates a compressed archive, saving space but requires extraction to restore files.
Using rsync for incremental backup
bash
#!/bin/bash
SOURCE_DIR="$1"
BACKUP_DIR="$HOME/backup/backup_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"
rsync -av --delete "$SOURCE_DIR"/ "$BACKUP_DIR"/
echo "Backup completed with rsync to $BACKUP_DIR"
Efficiently copies only changed files, good for large backups but requires rsync installed.

Complexity: O(n) time, O(n) space

Time Complexity

The script copies all files and folders once, so time grows linearly with the number of files (n).

Space Complexity

Backup requires space proportional to the size of the source files, so space is O(n).

Which Approach is Fastest?

Using rsync is faster for repeated backups because it copies only changed files, while cp copies everything every time.

ApproachTimeSpaceBest For
cp recursive copyO(n)O(n)Simple full backups
tar compressed archiveO(n)Less than O(n)Space-saving backups
rsync incremental backupO(changed files)O(n)Efficient repeated backups
💡
Always include a timestamp in your backup folder name to avoid overwriting previous backups.
⚠️
Forgetting to quote variables like "$SOURCE_DIR" can cause errors if paths contain spaces.