Backing Up a Directory Structure With the tree Command in a Bash Script

Published: 2022 November 07

system administration Linux Bash shell script software development

Introduction

This post is part of a series about automating data backups in Linux.

In the previous post we automated the process to backup a .bashrc file with a Bash script.

Similarly, in this post we’ll automate the process of saving the layout of several specific directories with a Bash script.

The Script

We’ve developed Bash scripts for automating tasks in previous posts, so we’ll leverage that work and modify it for our current purposes.

Specifically, we’ll start with the script developed for backing up the .bashrc file since it’s requirements are so similar.

We’ll also use our understanding of the tree command that we developed in a previous post to leverage that tool for our current effort.

Script code

backup_directory_structure.sh

#!/bin/bash


# VARIABLES
inquiry_top_directory="/home/$USER/"
inquiry_subdirectories="a b c"

backup_top_directory="/home/$USER/backups/"
backup_filename="backup_of_directory_map_$(date +%Y-%m-%d).txt"


# VARIABLES, constructed
tree_command="tree -d -L 2 -a ${inquiry_subdirectories}"

backup_output_directory=$backup_top_directory$backup_filename


# FUNCTION
# Provides contextual output
script_info() {
	echo
	echo
	
	echo
	echo
	echo =============================================================
	echo ==================== script info - START ====================
	echo
	echo "Executing $BASH_SOURCE"
	echo
	echo ".bashrc backup script"
	echo
	echo "This script will output the directory structure of: $inquiry_top_directory"
	echo "for the subdirectories: $inquiry_subdirectories"
	echo "to the file: $backup_output_directory"
	echo
	echo ===================== script info - END =====================
	echo =============================================================
	echo
	echo
}


# FUNCTION
# Creates a backup of the layout of specific directories.
create_backup_files() {
	
	cd $inquiry_top_directory
	
	echo
	echo "Backup started."
	echo
	
	#Display the results
	${tree_command}
	
	#Output the results to a file
	${tree_command} > $backup_output_directory
	
	echo
	echo "Backup finished."
	echo

}


# MAIN program flow
script_info
create_backup_files

Script description

As with the previous backup script despite the script length, the core functionality is very simple. We are just running the tree command with the relevant options and subdirectories. (The relevant sections are highlighted.)

The tree command and output path are constructed in variables to provide a simple way to make future updates.


inquiry_top_directory="/home/$USER/" - Defines a variable that points to the directory that contains the subdirectories we are interested in.

inquiry_subdirectories="a b c" - Defines a variable that lists the specific subdirectories we are interested in.


backup_top_directory="/home/$USER/backups/" - Defines a variable that points to the directory where we want to output the backup file.

backup_filename="backup_of_directory_map_$(date +%Y-%m-%d)" - Defines a variable that specifies the name of the backup file. In this case, the literal string backup_of_directory_map, followed by the current date, followed by the literal string .txt.


tree_command="tree -d -L 2 -a ${inquiry_subdirectories}" - Defines a variable for the desired tree command by combining a literal string with the variable listing the specific subdirectories of interest. In this case, it constructs the command tree -d -L 2 -a a b c with options for returning directories only, searching up to 2 subdirectories deep, and only looking at the a, b, and c subdirectories.

backup_output_directory=$backup_top_directory$backup_filename - Defines a variable for the output filepath and filename.


${tree_command} - Runs the specified tree command for the purpose of outputting the result to the user.

${tree_command} > $backup_output_directory - Runs the specified tree command again, this time for the purpose of outputting the result to the specified file.


Everything else in the script is output to the user for clarity.

Conclusion

This script backups the structure of specific directories to a file, allowing reproducibility and creating the possibility of further automation of backup tasks.

The finalized script can be found here.

Copying Directory Structure With a Bash Script - With Help From ChatGPT AI

Published: 2024 January 22

Setting Up a Raspberry Pi as a Linux Print Server with CUPS

Published: 2024 January 19

Setting Up a Raspberry Pi Zero 2 W with Ubuntu Server

Published: 2024 January 04