How to backup users home directory in Linux?

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

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

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

If no, we would like to write a small shell script to achieve it. For demonstration purposes, 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.

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.

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 this was originally designed for creating archives to store files on magnetic tape.

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

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

In this example, we will 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 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 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

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 *