February 14, 20245 min read

Complete Guide to Linux Backup: How to Backup System Files and Databases

Complete Guide to Linux Backup: How to Backup System Files and Databases
DevOpsAutomationDB BackupBackup

Backup Script

This Bash script provides a simple and comprehensive backup solution for files and databases, including MySQL, PostgreSQL, and MongoDB.


Configuration

  1. Run the script in the terminal.
  2. Enter the backup directory path when prompted.
  3. Specify the files or directories to be backed up.


Database Configuration

Configure database credentials and connection details:

  • MySQL:
  • Set mysql_user to your MySQL username.
  • Set mysql_password to your MySQL password.
  • PostgreSQL:
  • Set postgres_user to your PostgreSQL username.
  • Set postgres_password to your PostgreSQL password.
  • MongoDB:
  • Set mongodb_host to your MongoDB host.
  • Set mongodb_port to your MongoDB port.


Backup Process

The script performs the following backup tasks:

  1. Files Backup:
  2. Compress specified files or directories into a timestamped tarball.
  3. MySQL Backup:
  4. Dump all MySQL databases into a timestamped SQL file.
  5. PostgreSQL Backup:
  6. Dump all PostgreSQL databases into a timestamped SQL file.
  7. MongoDB Backup:
  8. Dump all MongoDB databases into a timestamped directory.
  9. Compress the MongoDB dump into a tarball.


#!/bin/bash


# Configuration
read -p "Where to backup?: " backup_dir
read -p "Which files need to backup?: " backup_file


mysql_user="root_pass"
mysql_password=""
postgres_user="postgres_user"
postgres_password="postgres_pass"
mongodb_host="localhost"
mongodb_port="27017"


# Create a timestamp for the backup
timestamp=$(date +"%Y%m%d_%H%M%S")


# Create backup directory if it doesn't exist
mkdir -p "$backup_dir"


# Backup files
files_backup_filename="files_backup_$timestamp.tar.gz"
tar -czf "$backup_dir/$files_backup_filename" "$backup_file"


# Backup MySQL databases
mysql_backup_filename="mysql_backup_$timestamp.sql"
sudo mysqldump -u "$mysql_user" -p"$mysql_password" --all-databases >"$backup_dir/$mysql_backup_filename"


if [ $? -eq 0 ]; thenecho "MySQL backup completed successfully: $mysql_backup_filename"
elseecho "Error: MySQL backup failed."exit 1
fi


# Backup PostgreSQL databases
postgres_backup_filename="postgres_backup_$timestamp.sql"
pg_dumpall -U "$postgres_user" -h localhost -w -f "$backup_dir/$postgres_backup_filename"


if [ $? -eq 0 ]; thenecho "PostgreSQL backup completed successfully: $postgres_backup_filename"
elseecho "Error: PostgreSQL backup failed."exit 1
fi


# Backup MongoDB databases
mongodb_backup_filename="mongodb_backup_$timestamp.tar.gz"
mongodump --host "$mongodb_host" --port "$mongodb_port" --out "$backup_dir/mongodb_dump"
tar -czf "$backup_dir/$mongodb_backup_filename" -C "$backup_dir" mongodb_dump


if [ $? -eq 0 ]; thenecho "MongoDB backup completed successfully: $mongodb_backup_filename"
elseecho "Error: MongoDB backup failed."exit 1
fi


# Optional: Clean up old backups (e.g., keep only the last 7 days)
find "$backup_dir" -type f -name "mysql_backup_*" -mtime +7 -delete
find "$backup_dir" -type f -name "postgres_backup_*" -mtime +7 -delete
find "$backup_dir" -type f -name "mongodb_backup_*" -mtime +7 -delete


echo "Backup process completed successfully."



Usage

  1. Run the Script:
  • Open a terminal and navigate to the directory containing the script.
  • Execute the script by running the following command:
chmod +x backup_script.sh

./backup_script.sh
  1. Provide Configuration:
  • Follow the prompts to provide the required configuration parameters.
  1. Backup Execution:
  • The script will create backups of files, MySQL databases, PostgreSQL databases, and MongoDB databases based on the provided configuration.
  • Each backup file will be named based on the current timestamp.
  1. View Backup Completion:
  • Upon completion, the script will display messages indicating the success or failure of each backup operation.
  • Error messages will be displayed if any of the backup operations fail.
  1. Optional: Clean Up Old Backups:
  • The script includes an optional feature to clean up old backups.
  • By default, it keeps only the backups created within the last 7 days.


Optional: Clean Up Old Backups

By default, the script keeps only the last 7 days of backups. You can adjust this duration in the script.


Important Notes

  • Ensure that you have the necessary permissions to perform backup operations on the specified files and databases.
  • Review the configuration parameters carefully to ensure that they are accurate.
  • Monitor the backup process and review the backup files to ensure that the data is backed up correctly.
  • Make necessary adjustments to the script as per your specific requirements and environment.


By following these instructions, you can effectively use the backup script to create backups of your files and databases in a secure and efficient manner.

Share this article:

Read more blogs

Find the blogs that you are looking for.

Simplifying User Management on Linux with a Bash Script

Simplifying User Management on Linux with a Bash Script

LinuxShell ScriptDevOps

Managing user accounts and groups on a Linux system is a fundamenta

Unleashing the Power of Linux Shell Scripting Language for DevOps

Unleashing the Power of Linux Shell Scripting Language for DevOps

LinuxShell ScriptDevOps

Linux, renowned for its flexibility and robustness, empowers users