๐ Day 9 of 90 Days of DevOps Challenge: Directory Backup with Rotation ๐
Hey there, DevOps enthusiasts! ๐ Today, on Day 9 of our 90 Days of DevOps challenge, we are diving into an essential topic: Directory Backup with Rotation using shell scripting. ๐๐
๐ What is Directory Backup with Rotation? ๐ค
Imagine you have many important files in a directory, and you want to ensure they're safe. ๐ A backup is a copy of these files saved somewhere else in case something goes wrong. But if you keep making new backups every day, you might run out of space! ๐ฌ
That's where backup rotation comes in. Instead of saving every backup forever, we keep a limited number of recent backups. For example, we can keep the last 7 backups and delete the older ones. This way, we save space while keeping our data safe. ๐ฆ๐พ
๐ ๏ธ Let's Create a Backup Script! ๐ฅ๏ธ
We'll go through the script step by step to understand each part in detail.
1. Set Variables ๐
Variables are used to store values that our script will use. This makes the script flexible and easy to change without modifying the entire code.
#!/bin/bash
SOURCE_DIR="/path/to/source"
BACKUP_DIR="/path/to/backup"
MAX_BACKUPS=7
#!/bin/bash
: This line tells the system that the script should be run using the bash shell.SOURCE_DIR
: This is the directory we want to back up.BACKUP_DIR
: This is where we will store our backups.MAX_BACKUPS
: This is the maximum number of backups we want to keep.
2. Create a New Backup ๐ฆ
We use the cp
(copy) command to create a backup. To ensure each backup is unique, we add a timestamp to the backup directory name.
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
NEW_BACKUP="$BACKUP_DIR/backup-$TIMESTAMP"
cp -r $SOURCE_DIR $NEW_BACKUP
echo "Backup created: $NEW_BACKUP"
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
: This command generates a timestamp in the formatYYYYMMDDHHMMSS
(Year, Month, Day, Hour, Minute, Second). This ensures each backup has a unique name.NEW_BACKUP="$BACKUP_DIR/backup-$TIMESTAMP"
: This creates the full path for the new backup directory.cp -r $SOURCE_DIR $NEW_BACKUP
: This command copies the entire source directory to the new backup directory. The-r
flag means "recursive," so it copies all subdirectories and files.echo "Backup created: $NEW_BACKUP"
: This prints a message confirming the backup was created.
3. Rotate Old Backups ๐
Next, we manage old backups to ensure we don't exceed the limit set by MAX_BACKUPS
.
cd $BACKUP_DIR
BACKUPS_COUNT=$(ls -1 | wc -l)
if [ $BACKUPS_COUNT -gt $MAX_BACKUPS ]; then
OLDEST_BACKUP=$(ls -1t | tail -1)
rm -rf $OLDEST_BACKUP
echo "Deleted oldest backup: $OLDEST_BACKUP"
fi
cd $BACKUP_DIR
: This changes the current directory to the backup directory.BACKUPS_COUNT=$(ls -1 | wc -l)
: This counts the number of backups in the backup directory.ls -1
: Lists all files and directories in the current directory, one per line.wc -l
: Counts the number of lines.
if [ $BACKUPS_COUNT -gt $MAX_BACKUPS ]; then
: This checks if the number of backups is greater than the maximum allowed.OLDEST_BACKUP=$(ls -1t | tail -1)
: This finds the oldest backup.ls -1t
: Lists files and directories sorted by modification time, newest first.tail -1
: Gets the last line of the output, which is the oldest backup.
rm -rf $OLDEST_BACKUP
: This removes the oldest backup directory.echo "Deleted oldest backup: $OLDEST_BACKUP"
: This prints a message confirming the deletion.
4. Combine Everything ๐๐
Putting it all together, we get the complete script:
#!/bin/bash
SOURCE_DIR="/path/to/source"
BACKUP_DIR="/path/to/backup"
MAX_BACKUPS=7
# Create a new backup
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
NEW_BACKUP="$BACKUP_DIR/backup-$TIMESTAMP"
cp -r $SOURCE_DIR $NEW_BACKUP
echo "Backup created: $NEW_BACKUP"
# Rotate old backups
cd $BACKUP_DIR
BACKUPS_COUNT=$(ls -1 | wc -l)
if [ $BACKUPS_COUNT -gt $MAX_BACKUPS ]; then
OLDEST_BACKUP=$(ls -1t | tail -1)
rm -rf $OLDEST_BACKUP
echo "Deleted oldest backup: $OLDEST_BACKUP"
fi
๐ Congratulations! ๐
We have just created a script that backs up a directory and rotates old backups! ๐ This simple automation can save you a lot of time and keep your data safe.๐ช
Keep following the journey, and let's learn and grow together! ๐ฑ Don't forget to share your progress and connect with me on LinkedIn. Let's inspire and help each other on this DevOps adventure! ๐
Happy Learning! ๐
Subscribe to my newsletter
Read articles from Ritesh Dolare directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ritesh Dolare
Ritesh Dolare
๐ Hi, I'm Ritesh Dolare, a DevOps enthusiast dedicated to mastering the art of DevOps.