#! /bin/bash function init { echo "Backify is starting, looking for configuration file..." >&2 config='backup.cfg' config_secured='sbackup.cfg' 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 fi if grep -E -q -v '^#|^[^ ]*=[^;]*' "$config"; then echo "Config file is unclean, cleaning it..." >&2 grep -E '^#|^[^ ]*=[^;&]*' "$config" > "$config_secured" config="$config_secured" fi source "$config" echo "Configuration file loaded" >&2 if [ "$EUID" -ne 0 ] then echo "Please run as root" exit fi } function system { 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 echo "Discovered $system based OS..." >&2 } function makedir { timestamp=$(date +%Y%m%d_%H%M) mkdir /tmp/backify-$timestamp tmpdir="/tmp/backify-$timestamp" } function wwwbackup { if [ "$www_backup" = true ] then echo "Backing up wwwroot..." >&2 mkdir -p $tmpdir/wwwdata cp -r $www_dir/ $tmpdir/wwwdata/ echo "Finished" >&2 fi } function vhostbackup { if [ "$vhost_backup" = true ] then echo "Backing up vhosts..." >&2 mkdir -p $tmpdir/vhosts cp -r $vhost_dir/ $tmpdir/vhosts/ echo "Finished" >&2 fi } function logbackupcentos { 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/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 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/httpd/* 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 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 push { if [ "$rsync_push" = true ] then #Push - Dockerized if [ "push_clean" = true ] then rm /opt/backify-$timestamp.tar.gz fi fi } function dockerbackup { if [ "$docker_enabled" = true] then 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 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 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 runbackup { # init, config check init # run system detection system if [ "$enabled" = true ] then # step 1 : create directory makedir # step 2 : www backup wwwbackup # step 3 : vhost backup vhostbackup # step 4: log backup if [ $system = "rhel" ] then logbackuprhel fi if [ $system = "ubuntu" ] then logbackupubuntu fi # step 5: docker backup dockerbackup # 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 } runbackup