Backup Script: Create a Backup of Important Files and Directories

  • Read
  • Discuss

This tutorial will teach us how to create a simple backup script in Bash to back up important files and directories. The script will allow us to copy files and directories to a backup location, preserving the original directory structure and file permissions.

Prerequisites

Before we begin, you should have access to a Unix/Linux system with Bash installed and have basic knowledge of shell scripting and the cp command.

Creating the Backup Script

Let’s start by creating a new script file using a text editor of your choice. We’ll call the script backup.sh.

#!/bin/bash

# Define the source and destination directories
src_dir="./important_files"
dst_dir="./backups/important_files"

# Create the destination directory if it doesn't exist
if [ ! -d $dst_dir ]; then
  mkdir -p $dst_dir
fi

# Copy the files and directories to the destination directory
cp -r $src_dir $dst_dir

# Print a message indicating that the backup is complete
echo "Backup complete!"

The following will be the output of the above.

Explanation of the Script

The script starts with the shebang line #!/bin/bash, which indicates that the script should be interpreted by the Bash shell.

Next, we define two variables: src_dir and dst_dir. These variables specify the source and destination directories for the backup. In this example, the source directory is ~/important_files and the destination directory is ~/backups/important_files.

We then use the if statement to check if the destination directory exists. If it does not exist, we create it using the mkdir -p command. The -p option ensures that any necessary parent directories are also created.

Finally, we use the cp -r command to copy the files and directories from the source to the destination. The -r option recursively copies directories, preserving the original directory structure.

After the backup is complete, we print a message indicating that the backup is complete using the echo command.

Running the Backup Script

Now that we have created the backup script, we can run it by making it executable and executing it in the terminal:

chmod +x backup.sh
./backup.sh

The script will run and create a backup of the important files and directories in the destination directory.

Automating the Backup Script

To make the backup process more convenient, we can automate the script to run at regular intervals. This can be achieved using the cron utility, which allows us to schedule tasks to run automatically.

To add the script to the cron schedule, open the crontab file using the following command:

crontab -e

This will open the crontab file in a text editor. Add the following line to schedule the script to run daily at 2:00 AM:

0 2 * * * /path/to/backup.sh

Save and close the crontab file, and the script will now run automatically every day at 2:00 AM.

Bash Script backup mysql

1. Open a text editor and create a new file.

2. Add the following code to the file:

#!/bin/bash

# MySQL credentials

mysql_user="username"

mysql_password="password"

mysql_database="database_name"

# Backup location

backup_dir="path/to/backup/"

# Create the backup directory if it doesn't exist

mkdir -p "$backup_dir"

# Create the backup file with the current date and time

backup_file="$backup_dir$mysql_database-$(date +%F_%T).sql"

# Dump the MySQL database to a file

mysqldump -u "$mysql_user" -p"$mysql_password" "$mysql_database" > "$backup_file"

Explanation:

  • The first line specifies the interpreter to be used, in this case, the bash shell.
  • The mysql_user, mysql_password, and mysql_database variables hold the credentials for the MySQL database to be backed up. Replace username, password, and database_name with the actual values.
  • The backup_dir variable holds the location where the backup file will be stored. Replace path/to/backup/ with the actual path.
  • The mkdir -p “$backup_dir” command creates the backup directory if it doesn’t exist.
  • The backup_file variable holds the name of the backup file, which is constructed using the database name, current date, and time.
  • The mysqldump command is used to dump the database to a file. The -u “$mysql_user” -p”$mysql_password” “$mysql_database” options specify the user, password, and database, and the > “$backup_file” redirects the output to the backup file.

3. Save the file with a .sh extension, for example, mysql_backup.sh.

4. Make the script executable by running the following command in the terminal:

chmod +x mysql_backup.sh

5. Run the script by executing the following command:

./mysql_backup.sh

This script will create a backup of the MySQL database specified in the mysql_database variable and store it in the backup_dir location. The backup file will have a unique name based on the current date and time.

Leave a Reply

Scroll to Top