Your cart is currently empty!
How to Create a Backup Script for Important Files in Linux
Losing important files can be devastating. Whether it’s family photos, business documents, or code you’ve poured hours into, a sudden hardware failure or accidental deletion can wipe it all away. That’s why every Linux user should have an automated backup plan. One of the simplest and most flexible ways to protect your data is with a custom backup script.
In this post, I’ll show you how to write a basic backup script in Bash, schedule it to run automatically, and keep your files safe—no fancy software required.
Step 1: Decide What You Need to Back Up
First, figure out which folders and files are critical. Most people want to back up their home directory, but you might also have special folders like /etc
for config files or custom scripts elsewhere.
Example directories:
/home/yourusername/Documents
/home/yourusername/Pictures
/etc
Step 2: Create the Backup Script
Open a terminal and use your favorite text editor (nano, vim, etc.) to create your script. Let’s call it backup.sh
:
nano ~/backup.sh
Paste the following script as a starting point:
#!/bin/bash
# Directories to back up
SOURCE="/home/yourusername/Documents /home/yourusername/Pictures"
# Backup destination
DEST="/home/yourusername/Backups"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
BACKUP_FILE="$DEST/backup_$DATE.tar.gz"
# Create backup directory if it doesn't exist
mkdir -p "$DEST"
# Create the backup
tar -czvf "$BACKUP_FILE" $SOURCE
# Optional: Delete backups older than 7 days
find "$DEST" -type f -mtime +7 -name '*.tar.gz' -exec rm {} \;
echo "Backup completed at $BACKUP_FILE"
What this script does:
- Archives your important directories.
- Saves the backup in a folder called
Backups
in your home directory. - Adds a timestamp for easy sorting.
- Optionally deletes backups older than 7 days to save space.
Don’t forget to make your script executable:
chmod +x ~/backup.sh
Step 3: Test Your Script
Run it manually to make sure it works:
~/backup.sh
Check your Backups
folder for a new .tar.gz
file. If it’s there and contains your files, you’re good.
Step 4: Automate with Cron
Backups are only useful if they’re regular. Use cron to run your script automatically—daily, weekly, or as often as you like.
Edit your crontab:
crontab -e
Add a line to run the script every day at 2 am:
0 2 * * * /home/yourusername/backup.sh
Save and exit. Your backups are now on autopilot.
Tips for Next-Level Backups
- Backup to an external drive or cloud: Change the
DEST
variable to an external mount point or userclone
for cloud services. - Encrypt sensitive backups: Pipe your tarball through
gpg
for extra security. - Monitor your backups: Set up email notifications or integrate with monitoring tools.
Wrapping Up
A simple Bash script is often all you need to keep your files safe in Linux. With a few lines of code and a scheduled cron job, you can protect yourself from data loss. Remember, one backup is good, but two is better—so always keep a copy offsite if possible.
Got questions or want more advanced backup tips? Drop a comment below!
Tech enthusiast and content creator passionate about making technology simple for everyone. I share practical tips, guides, and reviews on the latest in computers, software, and gadgets. Let’s explore the digital world together!