Backify/main.sh

296 lines
7.5 KiB
Bash
Raw Normal View History

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
if grep -E -q -v '^#|^[^ ]*=[^;]*' "$config"; then
2023-02-10 13:49:35 +00:00
echo "Config file is unclean, cleaning it..." >&2
grep -E '^#|^[^ ]*=[^;&]*' "$config" > "$config_secured"
2023-02-10 13:49:35 +00:00
config="$config_secured"
fi
source "$config"
echo "Configuration file loaded" >&2
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 14:43:57 +00:00
function makedir {
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 {
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 {
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 {
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
if [ "$fail2ban_log" = true ]
then
2023-02-10 15:51:49 +00:00
cp /var/log/fail2ban.log $tmpdir/syslogs/
fi
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
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
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
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
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
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
}
function push {
2023-02-10 20:51:28 +00:00
if [ "$rsync_push" = true ]
then
2023-02-10 15:51:49 +00:00
#Push - Dockerized
if [ "push_clean" = true ]
then
rm /opt/backify-$timestamp.tar.gz
fi
fi
}
function dockerbackup {
2023-02-10 20:51:28 +00:00
if [ "$docker_enabled" = true]
then
2023-02-10 20:51:28 +00:00
if [ "$docker_images" = true]
then
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
fi
2023-02-10 20:51:28 +00:00
if [ "$docker_volumes" = true ]
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 ]
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
fi
}
2023-02-11 10:49:22 +00:00
function backup_db {
2023-02-11 10:58:21 +00:00
if [ "$db_all" = true ]
2023-02-11 10:49:22 +00:00
then
2023-02-11 10:58:21 +00:00
if [ "$database_type" = "mysql" ]
then
mysqldump -u "$db_username" -p"$db_password" --all-databases > $tmpdir/db/db_all.sql
elif [ "$database_type" = "postgresql" ]
then
pg_dumpall -U "$db_username" -f $tmpdir/db/db_all.sql
fi
else
if [ "$database_type" = "mysql" ]
then
mysqldump -u "$db_username" -p"$db_password" "$db_name" > $tmpdir/db/$db_name.sql
elif [ "$database_type" = "postgresql" ]
then
pg_dump -U "$db_username" "$db_name" -f $tmpdir/db/$db_name.sql
fi
2023-02-11 10:49:22 +00:00
}
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 ]
then
# step 1 : create directory
makedir
# step 2 : www backup
wwwbackup
# step 3 : vhost backup
vhostbackup
# step 4: log backup
2023-02-11 10:13:17 +00:00
if [ $system = "rhel" ]
then
logbackuprhel
fi
if [ $system = "ubuntu" ]
then
logbackupubuntu
fi
# step 5: docker backup
dockerbackup
2023-02-11 10:58:21 +00:00
# step 6: db backup
if [ "$db_backup" = true ]
then
backup_db
fi
# 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
else
echo "Backup is disabled in the configuration" >&2
fi
2023-02-10 14:43:57 +00:00
}
runbackup