February 14, 20245 min read
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
- Run the script in the terminal.
- Enter the backup directory path when prompted.
- 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:
- Files Backup:
- Compress specified files or directories into a timestamped tarball.
- MySQL Backup:
- Dump all MySQL databases into a timestamped SQL file.
- PostgreSQL Backup:
- Dump all PostgreSQL databases into a timestamped SQL file.
- MongoDB Backup:
- Dump all MongoDB databases into a timestamped directory.
- 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
- 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
- Provide Configuration:
- Follow the prompts to provide the required configuration parameters.
- 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.
- 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.
- 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:

