Create a Backup Script
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.
Explanation:
#!/bin/bashis the shebang line, telling the system this script should be run with Bash.BACKUP_DIRandBACKUP_FILEare 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.…
No comments yet. Add the first comment to start the discussion.