Bash script for advanced server backup
Go to file
2023-02-27 09:54:23 +01:00
.gitignore Bug fixes / Git ignore for parsed backup.cfg 2023-02-10 21:44:40 +01:00
backup.cfg DB backup implement host and port parameter / backup path fix 2023-02-22 20:05:34 +01:00
main.sh readme and slight adjustments 2023-02-27 09:54:23 +01:00
README.MD readme and slight adjustments 2023-02-27 09:54:23 +01:00

Backify 🗃️

What is Backify? 👾

Backify is a shell script that helps you automate the backup process of all kinds of data from Linux systems. It differs from other backup scripts because it gives you the flexibility to choose what you want to save, ranging from system logs to containers. The script was tailored to meet personal needs as there was no complete solution for the specific use case.

Prerequisites 👷

  • The script must be executed as root.

  • A configuration file named backup.cfg must exist in the same directory as the script.

  • The system must be either a Red Hat-based or an Ubuntu-based distribution.

  • mysqldump / pgdump if dumping database on a diferent host

Configuration 🧙‍♂️

All configuration options can be found in the backup.cfg file. The script has an integrity check in place to ensure that no external commands can be embedded into it by malware. The following table provides an overview of the available configuration options:

Name Value Specifics
enabled true/false Disable the main function
backup_path ------> Set where to save the backup, make sure it DOESNT end with backslash
www_backup true/false Backup of the webroot directory
www_dir ------> Path to the webroot
vhost_backup true/false Backup of the vhost configuration
vhost_dir ------> Path to the vhost files
log_backup true/false Backup log files
log_to_backup array Array of logs to backup, see below for options
rsync_push true/false Push the backup file to a remote server
push_clean true/false Delete the backup file after push
target_host ------> Backup push target host
target_user ------> Backup push target username
target_key ------> Backup target ssh key
target_dir ------> Backup target push to location
docker_enable true/false Enable Docker backups
docker_images true/false Backup Docker images
docker_volumes true/false Backup Docker volumes
docker_data true/false Backup container information
db_backup true/false Backup database
database_type mysql/postgresql Database type
db_host ------> Database host
db_port ------> Port for DB access
db_username ------> Username for DB access
db_password ------> Password for DB access
db_name ------> Name of database
db_all ------> Dump all databases instead of specific one
custom_backup true/false Enable backup of custom files
custom_dirs ------> Array of files/directories to backup

Logs to backup array 📚

Option Specifics
apache Apache access and error logs
nginx Nginx access and error logs
fail2ban Fail2ban log
alternatives Alternatives log
pckg_mngr Logs from Yum/Apt package manager
auth Authentications log
dmesg Kernel log
dpkg Package changes log
letsencrypt Let's encrypt logs
php Logs from all installed PHPs
syslog System event data
purge Empty all the logs after backing up

Script Execution 🪄

To execute the script, simply run the following command in the terminal:

./backify.sh

The script will first initialize by checking for the existence of the configuration file, loading its parameters, and verifying that the script is being executed as root.

Then, it will determine whether the system is a Red Hat-based or an Ubuntu-based distribution.

Finally, the script will create a new directory with a timestamped name in the backup_path directory, where the backups will be stored.

The components specified in the configuration file will then be backed up to the newly created directory.

Automation 🤖

Here's an example of how you can use cron on Linux to run your script every day at 12 PM:

Open the terminal and type crontab -e to open the cron table for editing.

Add the following line to the end of the file:

0 12 * * * /path/to/your/script.sh

Save and exit the file.

MySQL user 🛢️

If You want to dump all of MySQL databases, read only user is recommended for that action.

It can be created with the following MySQL command:

GRANT LOCK TABLES, SELECT ON DATABASE_NAME.* TO 'BACKUP_USER'@'%' IDENTIFIED BY 'PASSWORD';

Buy me a beer 🍻

One pale ale won't hurt, will it ?

0x4046979a1E1152ddbfa4a910b1a98F73625a77ae ETH / BNB and Polygon chains