0%

0/1 Lessons

Introduction to Linux Server Operating Systems

• 30min

0 / 3 lessons complete

Linux Server Installation and Lab Setup

• 23min

0 / 6 lessons complete

Working with the Linux Command Line Interface

• 1hr 30min

0 / 12 lessons complete

User and Group Management

• 44min

0 / 7 lessons complete

Linux Storage

• 30min

0 / 6 lessons complete

Linux Administration Basics

• 53min

0 / 8 lessons complete

Linux Networking

• 47min

0 / 8 lessons complete

Course Conclusion

• 5min

0 / 1 lessons complete

Create a backup script

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, you will learn how to create a bash script that automates the backup of your home directory to a specific folder, /backups, and manages these backups by deleting any that are older than 30 days. This script will help you maintain a regular backup regimen and manage disk space effectively.

Importance of Automated Backups

Automating backups is crucial for data safety. Regular backups protect against data loss due to hardware failure, accidental deletions, or corruption. Automating this process ensures backups are done consistently without manual intervention.

Setting Up the Backup Directory

Before we start writing the script, ensure that the /backups directory exists. This directory will store all our backups. You can create it using:

sudo mkdir /backups

Writing the Backup Script

Open a Text Editor: We'll use nano for this task. Open it with the following command:

nano backup_script.sh

Script Content: In the nano editor, you will write the script below. I have broken the script up into several parts and will explain each part.

The script starts by setting up the destination for our backups and the naming convention for the backup files. We choose /backups as our storage directory. The backup file name includes the current date for easy tracking.

#!/bin/bash
# This script backs up the /home directory to /backups

# Define the backup file path
BACKUP_DIR="/backups"
BACKUP_FILE="$BACKUP_DIR/home-$(date +%Y%m%d-%H%M%S).tar.gz"

Explanation:

  • #!/bin/bash is the shebang line, telling the system this script should be run with Bash.
  • BACKUP_DIR and BACKUP_FILE are variables. We use $(date +%Y%m%d-%H%M%S) to insert the current date and time into the filename, making each backup unique.

Next, we create the backup. The script compresses the user's home directory and saves it to the location we defined earlier. It then prints a message to indicate successful completion.

# Create a backup of the home directory
tar -czvf $BACKUP_FILE /home
echo "Backup of home directory completed successfully"

Explanation:

  • tar -czvf is a command to create (c) a compressed (z) archive, with verbose output (v), and specifies the filename (f).
  • /home is where our file system stores the users home directories excluding the root user. So backing up this directly will backup all non-root users home files.

Finally, the script handles the deletion of old backups. It searches for and deletes backups older than 30 days in our designated backup directory. This is a crucial step in managing storage space effectively.

# Delete backups older than 30 days
echo "Deleting backups older than 30 days"
sudo find $BACKUP_DIR -type f -name 'home-*.tar.gz' -mtime +30 -exec rm -f {} \;

Explanation:

  • find is a command to search for files. -type f looks for files, and -name 'home-*.tar.gz' specifies the filename pattern.
  • -mtime +30 finds files modified more than 30 days ago.
  • -exec rm {} \; tells the script to execute the rm (remove) command on each file found, where {} is a placeholder for each file name.

For testing, you can change the find command to the example below which will delete backups older than 5 minutes... just don't forget to put it back :D

sudo find $BACKUP_DIR -type f -name 'home-*.tar.gz' -mmin +5 -exec rm -f {} \;

This script does the following:

  • Defines the backup directory and file name, including a date stamp.
  • Uses tar to create a compressed archive of your home directory.
  • Uses find to delete backups older than 30 days.

Make the Script Executable: Save the file and exit nano. Then, change the script’s permissions to make it executable:

chmod +x backup_script.sh

Running the Script

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments