How to Backup users Home directory in Linux?

Taking regular backups of your important data is a good practice, which allows you to restore it whenever required.

In preparation for a backup, it’s a good idea to remove unwanted files from your system as this will reduce the size of the archive, which eventually saves reasonable disk space on your system.

If you already have a backup solution, use it accordingly such as EMC, netapp, Commvault, etc.

If not, let’s write a small shell script to achieve it. For demonstration purpose, we will take a backup of user’s home directory, but it’s not limited only to user’s home directory and you can backup any directories by changing the backup source.

What is tar command?

tar’ is one of the best command for a local or manual backup solution. It creates tar files by adding a group of files into an archive and it was originally designed for creating archives, to store files on magnetic tape.

If you run a server with web hosting control panels such as cPanel, plesk, you do not need to worry about backups as there is a way to schedule backups.

Once the backup is created with the tar command, you need to sync the backup to a remote location for better availability.

Common syntax for tar

# 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 specific user’s home directory?

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

In this example, we will backup our test user – '2daygeek‘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 shown below:

# 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 from backing up, then use the following format:

Note: The below example will exclude the entire '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

Similarly, if you would like to exclude some of the pattern files or group of files, then use the following format:

Note: The below example will exclude the '.mp3 & .avi' files from 'demo' directory and archive the rest of the files.

# tar --exclude= {'*.mp3','*.avi'} ’/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 as shown below:

# sh home-dir-backup.sh

You can verify the generated backup file using ls command as shown below:

# 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 directory using shell script?

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

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

# vi /opt/user-list.txt

daygeek
sudha
u1
u2
u3
u4
u5
user1
user2

Create a small small shell script as shown below, 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 shown below:

# 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

Over to You

In this guide, we have explained how to backup user’s home directory on Linux, but it’s not limited only to user’s home directory and you can backup any directories by changing the backup source.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *