Backup Users Home Directory In Linux Using Tar Command

Published on

Introduction

We often need to do things that may put us at risk of losing data in Linux, nevertheless, it's always better to take systematic backups from time to time.

Today we will be learning about how to use the powerful tool called tar in Linux to help in the backup process.

Tar Command

Let's take a quick look at the tar command and the flags that we will be using with it.

tar -zcvpf /[Backup_Location]/[Backup_Filename] /[User_Home_Directory_Location]

Now let's understand these flags.

  • z : Compress the backup file with ‘gzip’ to make it a small size.
  • c : Create a new backup archive.
  • v : verbosely list files that are processed.
  • p : Preserves the permissions of the files put in the archive for later restoration.
  • f : use archive file or device ARCHIVE.

Usage

Let us now backup the home directory, in my case, the user name is akash.

tar -zcvpf /backup/akash-backup-$(date +%d-%m-%Y).tar.gz /home/akash

This will produce the output (for the /backup directory) as

ls -lh /backup

total 15G
-rw-r--r--. 1 root root 15G Mar  1 12:09 akash-backup-01-03-2021.tar.gz

If you wish to exclude some directory to be archived, you can use --exclude flag, something like this

tar --exclude='/home/akash/Documents/test-folder' -zcvpf /backup/akash-backup-$(date +%d-%m-%Y).tar.gz /home/akash

This will archive everything excluding the test-folder inside Documents.

And that's it, this will help you create a backup and store it with the date inside /backup directory.

Bonus

Doing this once is fine, but what if there was a way to regularly backup your content and automatically wipe out the old backups, wouldn't that be great. So let's just do that.

We will use a shell script and cron job to automate this task for us.

Backup Script

First, let's move into a better directory where we will store our script.

nano /opt/scripts/home-dir-backup.sh

Now copy the mentioned script, it is pretty straightforward.

#!/bin/bash
DATE=$(date +%d-%m-%Y)
BACKUP_DIR="/backup"

# To backup akash's home directory
tar -zcvpf $BACKUP_DIR/akash-$DATE.tar.gz /home/akash

# To delete files older than 15 days
find $BACKUP_DIR/* -mtime +15 -exec rm {} \;

Set executable permissions for this file.

chmod +x home-dir-backup.sh

Now your script is ready to be used.

Cron Job

The final step is to set up the cron job to run this script automatically. We will enter into crontab using

crontab -e

Inside it paste the following lines at the bottom.

0 12 * * 5 /opt/scripts/home-dir-backup.sh

This will run this script every Friday at 12:00. To learn more about how to configure these values, I would highly recommend using crontab.guru.

Hope you found this helpful, see you in the next one.

Updates straight in your inbox!

A periodic update about my life, recent blog posts, TIL (Today I learned) related stuff, things I am building and more!

No spam - unsubscribe at any time!

Share with others

Liked it?

Views

You may also like

  • bashlinux

    Increase Root Partition Size On Fedora

    Learn how to increase root partition size on fedora in 2 simple steps.

    3 min read
  • bashpostgres

    3 simple steps to create a local backup of Postgres database using Bash script

    In this article, I will show you how I back up the Postgres database hosted on Supabase (ideally can be anywhere) to the local system and automate this process.

    6 min read
  • cloudflaresecurity

    How to protect your website from attacks using Cloudflare WAF and Rate Limiting

    In this article, I will explain how I was able to protect my website from attackers and how you can too using Cloudflare WAF and rate limiting.

    6 min read