How to Backup Users Home Directory In Linux Using Tar Command?

Taking a regular backups of your important data is a good practice.

It allows you to restore it whenever you required.

If you have any backup solution then use it accordingly.

If not and would like to take users home directory then this tutorial will help you to achieve it.

It’s not limited only to users home directory and you can backup any directories.

tar is one of the best command for a local or manual backup solution.

It is one of the best suitable solution for the data backup.

We had added many useful shell scripts in the past. If you want to check those, navigate to the below link.

It’s applicable for small companies and personal servers. It’s not suitable for Big companies because data’s are very important for them and they may uses some third party software’s such as EMC, netapp, Commvault, etc, for backup solution.

If you are running a server with control panel like cPanel, plesk then you no need to worry about the backup since there is a option to schedule a backup.

You may need to sync the backups to remote location for safety purpose.

Common Syntax for tar Command

# tar -zcvpf /[Backup_File_Location]/[Backup_Filename] /[User's_Home_Directory_Location]

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

How to Backup a Particular User’s Home Directory?

Use the following command to take a backup of particular user’s home directory.

In this example, we are going to backup 2daygeek user’s home directory and the output file will be saved in the /backup directory.

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

You can verify the generated backup file using ls command as follow.

# ls -lh /backup/
total 24K
-rw-r--r--. 1 root root 23K Jun 18 00:30 daygeek-18-06-2019.tar.gz

Due to some reason if you would like to exclude some folders then use the following format. The below example will exclude the whole demo directory and archive the rest of the files and folders.

# tar --exclude=’/home/2daygeek/demo’ -zcvpf /backup/2daygeek-backup-$(date +%d-%m-%Y).tar.gz /home/2daygeek

The similar way if you would like to exclude some of the pattern files or group of file then use the following format. The below example will exclude the .mp3 & .avi files from demo directory and archive the rest of the files.

# tar --exclude=’/home/2daygeek/demo’ -zcvpf /backup/2daygeek-backup-$(date +%d-%m-%Y).tar.gz /home/2daygeek

How To Backup A Single User’s Home Directory Using Shell Script?

This shell script allows you to backup the given user’s home directory.

To do so, add the following shell script in a file.

# vi /opt/shell-scripts/home-dir-backup.sh

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

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

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

Set an executable permission to home-dir-backup.sh file.

# chmod +x home-dir-backup.sh

Finally run the script to achieve this.

# sh home-dir-backup.sh

You can verify the generated backup file using ls command as follow.

# ls -lh /backup/
total 24K
-rw-r--r--. 1 root root 23K Jun 18 00:30 daygeek-18-06-2019.tar.gz

How To Backup All Users Home Directories Using Shell Script?

This shell script allows you to backup all the users home directory.

Make sure you have added the users list in user-list.txt file. Each user should be in separate line.

# vi /opt/user-list.txt

daygeek
sudha
u1
u2
u3
u4
u5
user1
user2

Create a following small shell script to achieve this.

# vi /opt/shell-scripts/home-dir-backup-1.sh

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

#To create a new directory in the backup directory location
mkdir -p $BACKUP_DIR/$DATE

#To backup user's home directory
for user in `more /opt/user-list.txt`
do
tar -zcvpf $BACKUP_DIR/$DATE/$user-$DATE.tar.gz /home/$user
done

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

Set an executable permission to home-dir-backup-1.sh file.

# chmod +x home-dir-backup-1.sh

Finally run the script to achieve this.

# sh home-dir-backup-1.sh

You can verify the generated backup file using ls command as follow.

# ls -lh /backup/18-06-2019/
total 56K
-rw-r--r--. 1 root root 23K Jun 18 00:46 daygeek-18-06-2019.tar.gz
-rw-r--r--. 1 root root 500 Jun 18 00:46 sudha-18-06-2019.tar.gz
-rw-r--r--. 1 root root 490 Jun 18 00:46 u1-18-06-2019.tar.gz
-rw-r--r--. 1 root root 489 Jun 18 00:46 u2-18-06-2019.tar.gz
-rw-r--r--. 1 root root 490 Jun 18 00:46 u3-18-06-2019.tar.gz
-rw-r--r--. 1 root root 491 Jun 18 00:46 u4-18-06-2019.tar.gz
-rw-r--r--. 1 root root 489 Jun 18 00:46 u5-18-06-2019.tar.gz
-rw-r--r--. 1 root root 582 Jun 18 00:46 user1-18-06-2019.tar.gz
-rw-r--r--. 1 root root 582 Jun 18 00:46 user2-18-06-2019.tar.gz

Cronjob will make our life easier in executing programs on scheduled time without fail. The below cronjobs are scheduled to run @ 10AM &11AM everyday.

# crontab -e

0 10 * * * /opt/shell-scripts/home-dir-backup.sh
0 11 * * * /opt/shell-scripts/home-dir-backup-1.sh

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...