2023-02-10 13:49:35 +00:00
|
|
|
#! /bin/bash
|
|
|
|
|
2023-02-11 10:13:17 +00:00
|
|
|
function init {
|
2023-02-10 13:49:35 +00:00
|
|
|
echo "Backify is starting, looking for configuration file..." >&2
|
|
|
|
|
|
|
|
config='backup.cfg'
|
|
|
|
config_secured='sbackup.cfg'
|
|
|
|
|
2023-02-11 10:45:36 +00:00
|
|
|
if [ ! -f "$config" ]
|
|
|
|
then
|
|
|
|
echo "Error: Config file not found: $config" >&2
|
|
|
|
echo "Please create a config file or specify the location of an existing file." >&2
|
|
|
|
exit 1
|
2023-02-10 13:49:35 +00:00
|
|
|
fi
|
|
|
|
|
2023-02-10 20:44:40 +00:00
|
|
|
if grep -E -q -v '^#|^[^ ]*=[^;]*' "$config"; then
|
2023-02-10 13:49:35 +00:00
|
|
|
echo "Config file is unclean, cleaning it..." >&2
|
2023-02-10 20:44:40 +00:00
|
|
|
grep -E '^#|^[^ ]*=[^;&]*' "$config" > "$config_secured"
|
2023-02-10 13:49:35 +00:00
|
|
|
config="$config_secured"
|
|
|
|
fi
|
|
|
|
|
|
|
|
source "$config"
|
|
|
|
|
|
|
|
echo "Configuration file loaded" >&2
|
|
|
|
|
2023-02-10 19:33:42 +00:00
|
|
|
if [ "$EUID" -ne 0 ]
|
|
|
|
then echo "Please run as root"
|
|
|
|
exit
|
|
|
|
fi
|
2023-02-11 10:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function system {
|
|
|
|
|
2023-02-11 10:45:36 +00:00
|
|
|
if [ -f /etc/redhat-release ]
|
|
|
|
then
|
|
|
|
echo "Discovered Red Hat-based OS..."
|
|
|
|
system='rhel'
|
|
|
|
elif [ -f /etc/lsb-release ]
|
|
|
|
then
|
|
|
|
echo "Discovered Ubuntu-based OS..."
|
|
|
|
system='ubuntu'
|
|
|
|
else
|
|
|
|
echo "Error: Unable to detect OS type."
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-02-11 10:13:17 +00:00
|
|
|
|
|
|
|
echo "Discovered $system based OS..." >&2
|
|
|
|
}
|
2023-02-10 19:33:42 +00:00
|
|
|
|
2023-02-10 14:43:57 +00:00
|
|
|
function makedir {
|
2023-02-10 15:36:51 +00:00
|
|
|
timestamp=$(date +%Y%m%d_%H%M)
|
2023-02-10 15:38:42 +00:00
|
|
|
mkdir /tmp/backify-$timestamp
|
|
|
|
tmpdir="/tmp/backify-$timestamp"
|
2023-02-10 14:43:57 +00:00
|
|
|
}
|
2023-02-10 13:49:35 +00:00
|
|
|
|
2023-02-10 14:43:57 +00:00
|
|
|
function wwwbackup {
|
2023-02-10 20:44:40 +00:00
|
|
|
if [ "$www_backup" = true ]
|
|
|
|
then
|
2023-02-10 13:49:35 +00:00
|
|
|
echo "Backing up wwwroot..." >&2
|
|
|
|
mkdir -p $tmpdir/wwwdata
|
|
|
|
cp -r $www_dir/ $tmpdir/wwwdata/
|
|
|
|
echo "Finished" >&2
|
|
|
|
fi
|
2023-02-10 14:43:57 +00:00
|
|
|
}
|
2023-02-10 13:49:35 +00:00
|
|
|
|
2023-02-10 14:43:57 +00:00
|
|
|
function vhostbackup {
|
2023-02-10 20:44:40 +00:00
|
|
|
if [ "$vhost_backup" = true ]
|
|
|
|
then
|
2023-02-10 13:49:35 +00:00
|
|
|
echo "Backing up vhosts..." >&2
|
|
|
|
mkdir -p $tmpdir/vhosts
|
|
|
|
cp -r $vhost_dir/ $tmpdir/vhosts/
|
|
|
|
echo "Finished" >&2
|
2023-02-10 14:43:57 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:13:17 +00:00
|
|
|
function logbackupcentos {
|
2023-02-10 20:44:40 +00:00
|
|
|
if [ "$log_backup" = true ]
|
|
|
|
then
|
2023-02-10 14:43:57 +00:00
|
|
|
echo "Backing up system logs..." >&2
|
|
|
|
mkdir -p $tmpdir/syslogs
|
|
|
|
cp /var/log/syslog $tmpdir/syslogs/
|
|
|
|
cp /var/log/message $tmpdir/syslogs/
|
2023-02-10 15:51:49 +00:00
|
|
|
|
2023-02-10 20:44:40 +00:00
|
|
|
if [ "$fail2ban_log" = true ]
|
|
|
|
then
|
2023-02-10 15:51:49 +00:00
|
|
|
cp /var/log/fail2ban.log $tmpdir/syslogs/
|
|
|
|
fi
|
|
|
|
|
2023-02-10 20:44:40 +00:00
|
|
|
if [ "$log_backup_web" = true]
|
|
|
|
then
|
|
|
|
if [ "$apache" = true ]
|
|
|
|
then
|
2023-02-10 15:51:49 +00:00
|
|
|
mkdir -p $tmpdir/apachelogs
|
|
|
|
cp -r /var/log/httpd $tmpdir/apachelogs
|
2023-02-10 14:43:57 +00:00
|
|
|
fi
|
2023-02-10 20:44:40 +00:00
|
|
|
if [ "$nginx" = true ]
|
|
|
|
then
|
2023-02-10 15:51:49 +00:00
|
|
|
mkdir -p $tmpdir/nginxlogs
|
|
|
|
cp -r /var/log/nginx $tmpdir/nginxlogs
|
2023-02-10 14:43:57 +00:00
|
|
|
fi
|
|
|
|
fi
|
2023-02-10 15:51:49 +00:00
|
|
|
|
2023-02-10 20:44:40 +00:00
|
|
|
if [ "$log_purge" = true]
|
|
|
|
then
|
2023-02-10 14:43:57 +00:00
|
|
|
echo "Purging logs..." >&2
|
|
|
|
truncate -s 0 /var/log/syslog
|
|
|
|
truncate -s 0 /var/log/message
|
2023-02-10 20:44:40 +00:00
|
|
|
if [ "$apache" = true ]
|
|
|
|
then
|
2023-02-10 15:51:49 +00:00
|
|
|
truncate -s 0 /var/log/httpd/*
|
2023-02-11 10:13:17 +00:00
|
|
|
rm /var/log/httpd/*.gz
|
|
|
|
fi
|
|
|
|
if [ "$nginx" = true ]
|
|
|
|
then
|
|
|
|
truncate -s 0 /var/log/nginx/*
|
|
|
|
rm /var/log/nginx/*.gz
|
|
|
|
fi
|
|
|
|
if [ "$fail2ban_log" = true ]
|
|
|
|
then
|
|
|
|
truncate -s 0 /var/log/fail2ban.log
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
echo "Finished" >&2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function logbackupubuntu {
|
|
|
|
if [ "$log_backup" = true ]
|
|
|
|
then
|
|
|
|
echo "Backing up system logs..." >&2
|
|
|
|
mkdir -p $tmpdir/syslogs
|
|
|
|
cp /var/log/syslog $tmpdir/syslogs/
|
|
|
|
cp /var/log/message $tmpdir/syslogs/
|
|
|
|
|
|
|
|
if [ "$fail2ban_log" = true ]
|
|
|
|
then
|
|
|
|
cp /var/log/fail2ban.log $tmpdir/syslogs/
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$log_backup_web" = true]
|
|
|
|
then
|
|
|
|
if [ "$apache" = true ]
|
|
|
|
then
|
|
|
|
mkdir -p $tmpdir/apachelogs
|
|
|
|
cp -r /var/log/apache2 $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
|
|
|
|
echo "Purging logs..." >&2
|
|
|
|
truncate -s 0 /var/log/syslog
|
|
|
|
truncate -s 0 /var/log/message
|
|
|
|
if [ "$apache" = true ]
|
|
|
|
then
|
|
|
|
truncate -s 0 /var/log/apache2/*
|
|
|
|
rm /var/log/apache2/*.gz
|
2023-02-10 14:43:57 +00:00
|
|
|
fi
|
2023-02-10 20:44:40 +00:00
|
|
|
if [ "$nginx" = true ]
|
|
|
|
then
|
2023-02-10 15:51:49 +00:00
|
|
|
truncate -s 0 /var/log/nginx/*
|
2023-02-11 10:13:17 +00:00
|
|
|
rm /var/log/nginx/*.gz
|
2023-02-10 15:51:49 +00:00
|
|
|
fi
|
2023-02-10 20:44:40 +00:00
|
|
|
if [ "$fail2ban_log" = true ]
|
|
|
|
then
|
2023-02-10 15:51:49 +00:00
|
|
|
truncate -s 0 /var/log/fail2ban.log
|
2023-02-10 14:43:57 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
echo "Finished" >&2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-02-10 15:36:51 +00:00
|
|
|
function push {
|
2023-02-10 20:51:28 +00:00
|
|
|
if [ "$rsync_push" = true ]
|
2023-02-10 20:44:40 +00:00
|
|
|
then
|
2023-02-10 15:51:49 +00:00
|
|
|
#Push - Dockerized
|
2023-02-10 20:44:40 +00:00
|
|
|
if [ "push_clean" = true ]
|
|
|
|
then
|
2023-02-10 15:36:51 +00:00
|
|
|
rm /opt/backify-$timestamp.tar.gz
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-02-10 19:33:42 +00:00
|
|
|
function dockerbackup {
|
2023-02-10 20:51:28 +00:00
|
|
|
if [ "$docker_enabled" = true]
|
2023-02-10 20:44:40 +00:00
|
|
|
then
|
2023-02-10 20:51:28 +00:00
|
|
|
if [ "$docker_images" = true]
|
2023-02-10 20:44:40 +00:00
|
|
|
then
|
2023-02-10 19:33:42 +00:00
|
|
|
echo "Backing up Docker images..." >&2
|
|
|
|
for i in `docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/`
|
|
|
|
do container_name=$i
|
|
|
|
echo -n "$container_name - "
|
|
|
|
container_image=`docker inspect --format='{{.Config.Image}}' $container_name`
|
|
|
|
mkdir -p $tmpdir/containers/$container_name
|
|
|
|
save_dir="$tmpdir/containers/$container_name/$container_name-image.tar"
|
|
|
|
docker save -o $save_dir $container_image
|
|
|
|
echo "Finished" >&2
|
2023-02-10 19:40:11 +00:00
|
|
|
done
|
2023-02-10 19:33:42 +00:00
|
|
|
fi
|
2023-02-10 20:51:28 +00:00
|
|
|
if [ "$docker_volumes" = true ]
|
2023-02-10 20:44:40 +00:00
|
|
|
then
|
2023-02-10 19:40:11 +00:00
|
|
|
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
|
2023-02-10 20:51:28 +00:00
|
|
|
if [ "$docker_data" = true ]
|
2023-02-10 20:44:40 +00:00
|
|
|
then
|
2023-02-10 19:53:40 +00:00
|
|
|
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
|
2023-02-10 19:33:42 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:49:22 +00:00
|
|
|
function backup_db {
|
|
|
|
if [ "$db_backup" = true ]
|
|
|
|
then
|
|
|
|
echo "Backing up database..." >&2
|
|
|
|
mkdir -p $tmpdir/db
|
|
|
|
if [ "$database_type" = "mysql" ]
|
|
|
|
then
|
|
|
|
mysqldump -u "$db_username" -p"$db_password" "$db_name" > $tmpdir/db/db.sql
|
|
|
|
elif [ "$database_type" = "postgresql" ]
|
|
|
|
echo "soon"
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:43:57 +00:00
|
|
|
function runbackup {
|
2023-02-11 10:13:17 +00:00
|
|
|
# init, config check
|
|
|
|
init
|
|
|
|
# run system detection
|
|
|
|
system
|
2023-02-10 20:51:28 +00:00
|
|
|
if [ "$enabled" = true ]
|
2023-02-10 20:44:40 +00:00
|
|
|
then
|
2023-02-10 15:36:51 +00:00
|
|
|
# step 1 : create directory
|
|
|
|
makedir
|
2023-02-10 19:33:42 +00:00
|
|
|
# step 2 : www backup
|
2023-02-10 15:36:51 +00:00
|
|
|
wwwbackup
|
2023-02-10 19:33:42 +00:00
|
|
|
# step 3 : vhost backup
|
2023-02-10 15:36:51 +00:00
|
|
|
vhostbackup
|
2023-02-10 19:33:42 +00:00
|
|
|
# step 4: log backup
|
2023-02-11 10:13:17 +00:00
|
|
|
if [ $system = "rhel" ]
|
|
|
|
then
|
|
|
|
logbackuprhel
|
|
|
|
fi
|
|
|
|
if [ $system = "ubuntu" ]
|
|
|
|
then
|
|
|
|
logbackupubuntu
|
|
|
|
fi
|
2023-02-10 19:33:42 +00:00
|
|
|
# step 5: docker backup
|
|
|
|
dockerbackup
|
2023-02-10 15:36:51 +00:00
|
|
|
# archive data
|
|
|
|
echo "Creating backup archive..." >&2
|
|
|
|
tar -czvf /opt/backify-$timestamp.tar.gz $tmpdir
|
|
|
|
# push data to server
|
|
|
|
push
|
|
|
|
echo "Voila, enjoy the rest of the day" >&2
|
2023-02-10 20:46:46 +00:00
|
|
|
else
|
|
|
|
echo "Backup is disabled in the configuration" >&2
|
2023-02-10 15:36:51 +00:00
|
|
|
fi
|
2023-02-10 14:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
runbackup
|