Compare commits
33 Commits
c47c48ed1e
...
main
Author | SHA1 | Date | |
---|---|---|---|
1bff525466 | |||
35255156bd | |||
f53b1d381d | |||
472e490ea1 | |||
fa3ca3cf63 | |||
ed5f806720 | |||
6dcc7eb8e0 | |||
65b5b20b18 | |||
685b149045 | |||
e6adba12b2 | |||
aef0f7dc32 | |||
225a9d4db4 | |||
bc0480689d | |||
629e3c1631 | |||
89b861c7f7 | |||
2f66ba386f | |||
5203f65147 | |||
a428d3bc9e | |||
8e2e28602f | |||
6c40c9898d | |||
0478ce746a | |||
5183b9bc7d | |||
59a370ba74 | |||
223eff0d1b | |||
6fa438e81e | |||
f7b736b101 | |||
89086a68d3 | |||
f5d1d86a3a | |||
46dc8c171c | |||
8f72e7ff98 | |||
c4cf0729b6 | |||
337ccd9d71 | |||
7dae7cb970 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
sbackup.cfg
|
119
README.MD
119
README.MD
@ -1 +1,118 @@
|
|||||||
### TODO
|
# 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
|
38
backup.cfg
38
backup.cfg
@ -1,33 +1,33 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# --------------------------------------------------------
|
# --------------------------------------------------------
|
||||||
# Please double check Your settings
|
# Please double check Your settings
|
||||||
# --------------------------------------------------------
|
# --------------------------------------------------------
|
||||||
|
enabled=false #enable the script
|
||||||
enabled=false #enable main function
|
backup_path='/opt/backify' # where do you want backups saved, make sure it doesnt end in backslash
|
||||||
|
|
||||||
www_backup=false # backup wwwroot
|
www_backup=false # backup wwwroot
|
||||||
www_dir='xyz' # wwwroot location
|
www_dir='xyz' # location of wwwroot to backup
|
||||||
|
vhost_backup=false # backup vhost configurations
|
||||||
vhost_backup=false # backup vhost config
|
vhost_dir='/etc/httpd/sites-enabled' # location of active vhost files
|
||||||
vhost_dir='/etc/httpd/sites-enabled' # vhost location
|
|
||||||
|
|
||||||
log_backup=false # backup logs
|
log_backup=false # backup logs
|
||||||
log_backup_web=false # backup webapp logs
|
log_to_backup=("apache" "nginx" "fail2ban" "pckg_mngr" "auth" "dmesg" "dpkg" "letsencrypt" "php" "syslog" "purge")
|
||||||
apache=false # apache log backup
|
# logs to backup, options: apache, nginx, fail2ban, pckg_mngr, auth, dmesg, dpkg, letsencrypt, php, syslog, purge (truncate all))
|
||||||
nginx=false # nginx log backup
|
|
||||||
fail2ban_log=false # fail2ban log backup
|
|
||||||
log_purge=false # purge logs after backup
|
|
||||||
|
|
||||||
rsync_push=false # enable push to remote server
|
rsync_push=false # enable push to remote server
|
||||||
push_clean=false # clean backup file after push
|
push_clean=false # clean backup file after push
|
||||||
target_host="127.0.0.1" # rsync target host
|
target_host="127.0.0.1" # rsync target host
|
||||||
target_user="backup" # rsync target user
|
target_user="backup" # rsync target user
|
||||||
target_key='/home/xyz/.ssh/rsync' # rsync key
|
target_key='/home/xyz/.ssh/rsync' # rsync key
|
||||||
|
target_dir='/opt/backups/srvyxyz/' # rsync target host path
|
||||||
docker_enabled=false # will you use docker backup
|
docker_enabled=false # will you use docker backup
|
||||||
docker_images=false # backup docker images
|
docker_images=false # backup docker images
|
||||||
docker_volumes=false #backup docker volumes
|
docker_volumes=false #backup docker volumes
|
||||||
|
docker_data=false #backup container information
|
||||||
#Rsync
|
db_backup=false #backup databases
|
||||||
#Daily cron
|
database_type=mysql #mysql or postgresql
|
||||||
#Mysql dbs
|
db_host='localhost' #hostname of mysql server
|
||||||
|
db_port=3306 #port for db access
|
||||||
|
db_username=user #database user
|
||||||
|
db_password=user #database password
|
||||||
|
db_all=false #dumps all databases if true
|
||||||
|
db_name=user #name of the database
|
||||||
|
custom_backup=false #backup custom files or directories
|
||||||
|
custom_dirs=("/opt/example" "/var/log/script.log") #array of custom files and/or directories to backup
|
424
main.sh
424
main.sh
@ -1,158 +1,358 @@
|
|||||||
#! /bin/bash
|
#! /bin/bash
|
||||||
|
|
||||||
echo "Backify is starting, looking for configuration file..." >&2
|
function init {
|
||||||
|
echo "Backify is starting, looking for configuration file..." >&2
|
||||||
|
|
||||||
config='backup.cfg'
|
config='backup.cfg'
|
||||||
config_secured='sbackup.cfg'
|
secured_config='sbackup.cfg'
|
||||||
|
|
||||||
if config -f "$config"; then
|
if [ ! -f "$config" ]; then
|
||||||
echo "Configuration found." >&2
|
echo "Error: Config file not found: $config" >&2
|
||||||
else
|
echo "Please create a config file or specify the location of an existing file." >&2
|
||||||
echo "Configuration not found" >&2
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if grep -E -q -v '^#|^[^ ]*=[^;]*' "$config"; then
|
||||||
|
echo "Config file is unclean, cleaning it..." >&2
|
||||||
|
grep -E '^#|^[^ ]*=[^;&]*' "$config" >"$secured_config"
|
||||||
|
config="$secured_config"
|
||||||
|
fi
|
||||||
|
|
||||||
|
source "$config"
|
||||||
|
|
||||||
|
echo "Configuration file loaded" >&2
|
||||||
|
|
||||||
|
if [ "$EUID" -ne 0 ]; then
|
||||||
|
echo "Please run as root"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
if egrep -q -v '^#|^[^ ]*=[^;]*' "$config"; then
|
function system {
|
||||||
echo "Config file is unclean, cleaning it..." >&2
|
|
||||||
egrep '^#|^[^ ]*=[^;&]*' "$config" > "$config_secured"
|
|
||||||
config="$config_secured"
|
|
||||||
fi
|
|
||||||
|
|
||||||
source "$config"
|
if [ -f /etc/redhat-release ]; then
|
||||||
|
echo "Discovered Red Hat-based OS..."
|
||||||
|
system='rhel'
|
||||||
|
elif [ -f /etc/lsb-release ]; then
|
||||||
|
echo "Discovered Debian-based OS..."
|
||||||
|
system='debian'
|
||||||
|
else
|
||||||
|
echo "Error: Unable to detect OS type."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Configuration file loaded" >&2
|
}
|
||||||
|
|
||||||
if [ "$EUID" -ne 0 ]
|
|
||||||
then echo "Please run as root"
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
function makedir {
|
function makedir {
|
||||||
timestamp=$(date +%Y%m%d_%H%M)
|
timestamp=$(date +%Y%m%d_%H%M)
|
||||||
mkdir /tmp/backify-$timestamp
|
mkdir -p "$backup_path/backify-$timestamp"
|
||||||
tmpdir="/tmp/backify-$timestamp"
|
tmpdir="$backup_path/backify-$timestamp"
|
||||||
}
|
}
|
||||||
|
|
||||||
function wwwbackup {
|
function wwwbackup {
|
||||||
if [ "$www_backup" = true ] then
|
if [ "$www_backup" = true ]; then
|
||||||
echo "Backing up wwwroot..." >&2
|
echo "Backing up wwwroot..." >&2
|
||||||
mkdir -p $tmpdir/wwwdata
|
mkdir -p "$tmpdir/wwwdata"
|
||||||
cp -r $www_dir/ $tmpdir/wwwdata/
|
cp -r "$www_dir/" "$tmpdir/wwwdata/"
|
||||||
echo "Finished" >&2
|
echo "Finished" >&2
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function vhostbackup {
|
function vhostbackup {
|
||||||
if [ "$vhost_backup" = true ] then
|
if [ "$vhost_backup" = true ]; then
|
||||||
echo "Backing up vhosts..." >&2
|
echo "Backing up vhosts..." >&2
|
||||||
mkdir -p $tmpdir/vhosts
|
mkdir -p "$tmpdir/vhosts"
|
||||||
cp -r $vhost_dir/ $tmpdir/vhosts/
|
cp -avr "$vhost_dir/" "$tmpdir/vhosts/"
|
||||||
echo "Finished" >&2
|
echo "Finished" >&2
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function logbackup {
|
function logbackup {
|
||||||
if [ "$log_backup" = true ] then
|
if [ "$log_backup" = true ]; then
|
||||||
echo "Backing up system logs..." >&2
|
echo "Backing up system logs..." >&2
|
||||||
mkdir -p $tmpdir/syslogs
|
mkdir -p "$tmpdir/syslogs"
|
||||||
cp /var/log/syslog $tmpdir/syslogs/
|
|
||||||
cp /var/log/message $tmpdir/syslogs/
|
|
||||||
|
|
||||||
if [ "$fail2ban_log" = true ] then
|
case $system in
|
||||||
cp /var/log/fail2ban.log $tmpdir/syslogs/
|
"rhel")
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$log_backup_web" = true] then
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[fail2ban]} " ]]; then
|
||||||
if [ "$apache" = true ] then
|
cp /var/log/fail2ban.log "$tmpdir/syslogs/"
|
||||||
mkdir -p $tmpdir/apachelogs
|
fi
|
||||||
cp -r /var/log/httpd $tmpdir/apachelogs
|
|
||||||
fi
|
|
||||||
if [ "$nginx" = true ] then
|
|
||||||
mkdir -p $tmpdir/nginxlogs
|
|
||||||
cp -r /var/log/nginx $tmpdir/nginxlogs
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$log_purge" = true] then
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[apache]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/apachelogs"
|
||||||
|
cp -r /var/log/httpd "$tmpdir/apachelogs"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[nginx]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/nginxlogs"
|
||||||
|
cp -r /var/log/nginx "$tmpdir/nginxlogs"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[pckg_mngr]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/syslogs/"
|
||||||
|
mkdir -p "$tmpdir/syslogs/yum"
|
||||||
|
cp -r /var/log/yum/* "$tmpdir/syslogs/yum/"
|
||||||
|
cp -r /var/log/dnf* "$tmpdir/syslogs/yum/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[letsencrypt]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/syslogs/"
|
||||||
|
mkdir -p "$tmpdir/syslogs/letsencrypt"
|
||||||
|
cp -r /var/log/letsencrypt/* "$tmpdir/syslogs/letsencrypt/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[php]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/syslogs/"
|
||||||
|
cp -r /var/log/php*.log "$tmpdir/syslogs/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[syslog]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/syslogs/"
|
||||||
|
cp -r /var/log/secure "$tmpdir/syslogs/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[purge]} " ]]; then
|
||||||
echo "Purging logs..." >&2
|
echo "Purging logs..." >&2
|
||||||
truncate -s 0 /var/log/syslog
|
truncate -s 0 /var/log/syslog
|
||||||
truncate -s 0 /var/log/message
|
truncate -s 0 /var/log/message
|
||||||
if [ "$apache" = true ] then
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[apache]} " ]]; then
|
||||||
# TODO: removal for .1.2.3 logs
|
truncate -s 0 /var/log/httpd/*
|
||||||
truncate -s 0 /var/log/httpd/*
|
rm /var/log/httpd/*.gz
|
||||||
fi
|
fi
|
||||||
if [ "$nginx" = true ] then
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[nginx]} " ]]; then
|
||||||
# TODO: removal for .1.2.3 logs
|
truncate -s 0 /var/log/nginx/*
|
||||||
truncate -s 0 /var/log/nginx/*
|
rm /var/log/nginx/*.gz
|
||||||
fi
|
fi
|
||||||
if [ "$fail2ban_log" = true ] then
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[fail2ban]} " ]]; then
|
||||||
truncate -s 0 /var/log/fail2ban.log
|
truncate -s 0 /var/log/fail2ban.log
|
||||||
fi
|
fi
|
||||||
fi
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[pckg_mngr]} " ]]; then
|
||||||
echo "Finished" >&2
|
truncate -s 0 /var/log/yum/*
|
||||||
fi
|
truncate -s 0 /var/log/dnf*
|
||||||
|
fi
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[letsencrypt]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/letsencrypt/*
|
||||||
|
fi
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[php]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/php*.log
|
||||||
|
fi
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[syslog]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/secure
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
"debian")
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[fail2ban]} " ]]; then
|
||||||
|
cp /var/log/fail2ban.log "$tmpdir/syslogs/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[apache]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/apachelogs"
|
||||||
|
cp -r /var/log/apache2 "$tmpdir/apachelogs"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[nginx]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/nginxlogs"
|
||||||
|
cp -r /var/log/nginx "$tmpdir/nginxlogs"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[pckg_mngr]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/syslogs/"
|
||||||
|
mkdir -p "$tmpdir/syslogs/apt"
|
||||||
|
cp -r /var/log/apt/* "$tmpdir/syslogs/apt/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[auth]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/syslogs/"
|
||||||
|
cp -r /var/log/auth.log "$tmpdir/syslogs/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[dmesg]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/syslogs/"
|
||||||
|
cp -r /var/log/dmesg "$tmpdir/syslogs/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[dpkg]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/syslogs/"
|
||||||
|
cp -r /var/log/dpkg.log "$tmpdir/syslogs/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[letsencrypt]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/syslogs/"
|
||||||
|
mkdir -p "$tmpdir/syslogs/letsencrypt"
|
||||||
|
cp -r /var/log/letsencrypt/* "$tmpdir/syslogs/letsencrypt/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[php]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/syslogs/"
|
||||||
|
cp -r /var/log/php*.log "$tmpdir/syslogs/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[syslog]} " ]]; then
|
||||||
|
mkdir -p "$tmpdir/syslogs/"
|
||||||
|
cp -r /var/log/syslog "$tmpdir/syslogs/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[purge]} " ]]; then
|
||||||
|
echo "Purging logs..." >&2
|
||||||
|
truncate -s 0 /var/log/syslog
|
||||||
|
truncate -s 0 /var/log/message
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[apache]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/apache2/*
|
||||||
|
rm /var/log/apache2/*.gz
|
||||||
|
fi
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[nginx]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/nginx/*
|
||||||
|
rm /var/log/nginx/*.gz
|
||||||
|
fi
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[fail2ban]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/fail2ban.log
|
||||||
|
fi
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[pckg_mngr]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/apt/*
|
||||||
|
fi
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[auth]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/auth.log
|
||||||
|
fi
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[dmesg]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/dmesg
|
||||||
|
fi
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[dpkg]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/dpkg.log
|
||||||
|
fi
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[letsencrypt]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/letsencrypt/*
|
||||||
|
fi
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[php]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/php*.log
|
||||||
|
fi
|
||||||
|
if [[ " ${log_to_backup[*]} " =~ " ${log_to_backup[syslog]} " ]]; then
|
||||||
|
truncate -s 0 /var/log/syslog
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function push {
|
function push {
|
||||||
if [ "rsync_push" = true ] then
|
if [ "$rsync_push" = true ]; then
|
||||||
#Push - Dockerized
|
echo "Pushing the backup package to $target_host..." >&2
|
||||||
if [ "push_clean" = true ] then
|
rsync -avz -e "ssh -i $target_key" $backup_path/backify-$timestamp.tar.gz $target_user@$target_host:$target_dir
|
||||||
rm /opt/backify-$timestamp.tar.gz
|
if [ "$push_clean" = true ]; then
|
||||||
fi
|
echo "Removing archive..." >&2
|
||||||
|
rm "$backup_path/backify-$timestamp.tar.gz"
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function dockerbackup {
|
function dockerbackup {
|
||||||
if [ "docker_enabled" = true] then
|
if [ "$docker_enabled" = true ]; then
|
||||||
if [ "docker_images" = true] then
|
if [ "$docker_images" = true ]; then
|
||||||
echo "Backing up Docker images..." >&2
|
echo "Backing up Docker images..." >&2
|
||||||
for i in `docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/`
|
for i in $(docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/); do
|
||||||
do container_name=$i
|
container_name=$i
|
||||||
echo -n "$container_name - "
|
echo -n "$container_name - "
|
||||||
container_image=`docker inspect --format='{{.Config.Image}}' $container_name`
|
container_image=$(docker inspect --format='{{.Config.Image}}' $container_name)
|
||||||
mkdir -p $tmpdir/containers/$container_name
|
mkdir -p $tmpdir/containers/$container_name
|
||||||
save_dir="$tmpdir/containers/$container_name/$container_name-image.tar"
|
save_dir="$tmpdir/containers/$container_name/$container_name-image.tar"
|
||||||
docker save -o $save_dir $container_image
|
docker save -o $save_dir $container_image
|
||||||
echo "Finished" >&2
|
echo "Finished" >&2
|
||||||
done
|
done
|
||||||
fi
|
|
||||||
if [ "docker_volumes" = true ] then
|
|
||||||
echo "Backing up Docker volumes..." >&2
|
|
||||||
for i in `docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/`
|
|
||||||
do container_name=$i
|
|
||||||
mkdir -p $tmpdir/containers/$container_name
|
|
||||||
echo -n "$container_name - "
|
|
||||||
docker run --rm --userns=host \
|
|
||||||
--volumes-from $container_name \
|
|
||||||
-v $backup_path:/backup \
|
|
||||||
-e TAR_OPTS="$tar_opts" \
|
|
||||||
piscue/docker-backup \
|
|
||||||
backup "$tmpdir/containers/$container_name/$container_name-volume.tar.xz"
|
|
||||||
echo "Finished" >&2
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
if [ "$docker_volumes" = true ]; then
|
||||||
|
echo "Backing up Docker volumes..." >&2
|
||||||
|
#Thanks piscue :)
|
||||||
|
for i in $(docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/); do
|
||||||
|
container_name=$i
|
||||||
|
mkdir -p $tmpdir/containers/$container_name
|
||||||
|
echo -n "$container_name - "
|
||||||
|
docker run --rm --userns=host \
|
||||||
|
--volumes-from $container_name \
|
||||||
|
-v $tmpdir/containers/$container_name:/backup \
|
||||||
|
-e TAR_OPTS="$tar_opts" \
|
||||||
|
piscue/docker-backup \
|
||||||
|
backup "$container_name-volume.tar.xz"
|
||||||
|
echo "Finished" >&2
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
if [ "$docker_data" = true ]; then
|
||||||
|
echo "Backing up container information..." >&2
|
||||||
|
for i in $(docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/); do
|
||||||
|
container_name=$i
|
||||||
|
echo -n "$container_name - "
|
||||||
|
container_data=$(docker inspect $container_name)
|
||||||
|
mkdir -p $tmpdir/containers/$container_name
|
||||||
|
echo $container_data >$tmpdir/containers/$container_name/$container_name-data.txt
|
||||||
|
echo "Finished" >&2
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function backup_db {
|
||||||
|
mkdir -p $tmpdir/db
|
||||||
|
if [ "$db_all" = true ]; then
|
||||||
|
if [ "$database_type" = "mysql" ]; then
|
||||||
|
mysqldump -u "$db_username" -p"$db_password" -h "$db_host" -P"$db_port" --all-databases >$tmpdir/db/db_all.sql
|
||||||
|
elif [ "$database_type" = "postgresql" ]; then
|
||||||
|
pg_dumpall -U "$db_username" -h "$db_host" -f $tmpdir/db/db_all.sql
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [ "$database_type" = "mysql" ]; then
|
||||||
|
mysqldump -u "$db_username" -p"$db_password" -h "$db_host" -P"$db_port" "$db_name" >$tmpdir/db/$db_name.sql
|
||||||
|
elif [ "$database_type" = "postgresql" ]; then
|
||||||
|
pg_dump -U "$db_username" -h "$db_host" "$db_name" -f $tmpdir/db/$db_name.sql
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function custombackup {
|
||||||
|
if [ "$custom_backup" = "true" ]; then
|
||||||
|
mkdir -p "$tmpdir/custom"
|
||||||
|
for i in "${custom_dirs[@]}"
|
||||||
|
do
|
||||||
|
cp -r $i $tmpdir/custom/
|
||||||
|
done
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function runbackup {
|
function runbackup {
|
||||||
if [ "enabled" = true] then
|
# init, config check
|
||||||
# step 1 : create directory
|
init
|
||||||
makedir
|
# run system detection
|
||||||
# step 2 : www backup
|
system
|
||||||
wwwbackup
|
if [ "$enabled" = true ]; then
|
||||||
# step 3 : vhost backup
|
# step 1 : create directory
|
||||||
vhostbackup
|
makedir
|
||||||
# step 4: log backup
|
# step 2 : www backup
|
||||||
logbackup
|
wwwbackup
|
||||||
# step 5: docker backup
|
# step 3 : vhost backup
|
||||||
dockerbackup
|
vhostbackup
|
||||||
# archive data
|
# step 4: log backup
|
||||||
echo "Creating backup archive..." >&2
|
logbackup
|
||||||
tar -czvf /opt/backify-$timestamp.tar.gz $tmpdir
|
# step 5: docker backup
|
||||||
# push data to server
|
dockerbackup
|
||||||
push
|
# step 6: db backup
|
||||||
echo "Voila, enjoy the rest of the day" >&2
|
if [ "$db_backup" = true ]; then
|
||||||
|
backup_db
|
||||||
fi
|
fi
|
||||||
|
# step 7 : custom backup
|
||||||
|
custombackup
|
||||||
|
# archive data
|
||||||
|
echo "Creating backup archive..." >&2
|
||||||
|
tar -czvf $backup_path/backify-$timestamp.tar.gz $tmpdir >> /var/log/backify-compress.log
|
||||||
|
# push data to server
|
||||||
|
push
|
||||||
|
# remove temp files
|
||||||
|
rm -r $tmpdir
|
||||||
|
echo "Voila, enjoy the rest of the day" >&2
|
||||||
|
else
|
||||||
|
echo "Backup is disabled in the configuration" >&2
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
runbackup
|
runbackup
|
||||||
|
Reference in New Issue
Block a user