Rsync implementation, formatting

This commit is contained in:
gospodar 2023-02-12 13:26:12 +01:00
parent 6c40c9898d
commit 8e2e28602f
3 changed files with 187 additions and 220 deletions

View File

@ -15,7 +15,8 @@ All configuration options can be found in the `backup.cfg` file. The script has
| Name | Value | Specifics | | Name | Value | Specifics |
| --- | --- | --- | | --- | --- | --- |
| Enabled | true/false | Disable the main function | | enabled | true/false | Disable the main function |
| backup_path | ------> | Set where to save the backup |
| www_backup | true/false | Backup of the webroot directory | | www_backup | true/false | Backup of the webroot directory |
| www_dir | ------> | Path to the webroot | | www_dir | ------> | Path to the webroot |
| vhost_backup | true/false | Backup of the vhost configuration | | vhost_backup | true/false | Backup of the vhost configuration |
@ -31,6 +32,7 @@ All configuration options can be found in the `backup.cfg` file. The script has
| target_host | ------> | Backup push target host | | target_host | ------> | Backup push target host |
| target_user | ------> | Backup push target username | | target_user | ------> | Backup push target username |
| target_key | ------> | Backup target ssh key | | target_key | ------> | Backup target ssh key |
| target_dir | ------> | Backup target push to location |
| docker_enable | true/false | Enable Docker backups | | docker_enable | true/false | Enable Docker backups |
| docker_images | true/false | Backup Docker images | | docker_images | true/false | Backup Docker images |
| docker_volumes | true/false | Backup Docker volumes | | docker_volumes | true/false | Backup Docker volumes |
@ -40,11 +42,4 @@ All configuration options can be found in the `backup.cfg` file. The script has
| db_username | ------> | Username for DB access | | db_username | ------> | Username for DB access |
| db_password | ------> | Password for DB access | | db_password | ------> | Password for DB access |
| db_name | ------> | Name of database | | db_name | ------> | Name of database |
| db_all | ------> | Dumb all databases instead of specific one | | db_all | ------> | Dump all databases instead of specific one |
To-Do List
----------
- [ ] Rsync implementation via shell
- [ ] Rsync implementation via Docker
- [ ] Cron scheduler

View File

@ -3,6 +3,7 @@
# Please double check Your settings # Please double check Your settings
# -------------------------------------------------------- # --------------------------------------------------------
enabled=false #enable main function enabled=false #enable main function
backup_path='/opt/backify/' # location of backups
www_backup=false # backup wwwroot www_backup=false # backup wwwroot
www_dir='xyz' # wwwroot location www_dir='xyz' # wwwroot location
vhost_backup=false # backup vhost config vhost_backup=false # backup vhost config
@ -18,6 +19,7 @@ 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

392
main.sh
View File

@ -1,42 +1,39 @@
#! /bin/bash #! /bin/bash
function init { function init {
echo "Backify is starting, looking for configuration file..." >&2 echo "Backify is starting, looking for configuration file..." >&2
config='backup.cfg' config='backup.cfg'
secured_config='sbackup.cfg' secured_config='sbackup.cfg'
if [ ! -f "$config" ] if [ ! -f "$config" ]; then
then
echo "Error: Config file not found: $config" >&2 echo "Error: Config file not found: $config" >&2
echo "Please create a config file or specify the location of an existing file." >&2 echo "Please create a config file or specify the location of an existing file." >&2
exit 1 exit 1
fi fi
if grep -E -q -v '^#|^[^ ]*=[^;]*' "$config"; then if grep -E -q -v '^#|^[^ ]*=[^;]*' "$config"; then
echo "Config file is unclean, cleaning it..." >&2 echo "Config file is unclean, cleaning it..." >&2
grep -E '^#|^[^ ]*=[^;&]*' "$config" > "$secured_config" grep -E '^#|^[^ ]*=[^;&]*' "$config" >"$secured_config"
config="$secured_config" config="$secured_config"
fi fi
source "$config" source "$config"
echo "Configuration file loaded" >&2 echo "Configuration file loaded" >&2
if [ "$EUID" -ne 0 ] if [ "$EUID" -ne 0 ]; then
then echo "Please run as root" echo "Please run as root"
exit exit
fi fi
} }
function system { function system {
if [ -f /etc/redhat-release ] if [ -f /etc/redhat-release ]; then
then
echo "Discovered Red Hat-based OS..." echo "Discovered Red Hat-based OS..."
system='rhel' system='rhel'
elif [ -f /etc/lsb-release ] elif [ -f /etc/lsb-release ]; then
then
echo "Discovered Ubuntu-based OS..." echo "Discovered Ubuntu-based OS..."
system='ubuntu' system='ubuntu'
else else
@ -44,245 +41,218 @@ function system {
exit 1 exit 1
fi fi
echo "Discovered $system based OS..." >&2 echo "Discovered $system based OS..." >&2
} }
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 ] if [ "$www_backup" = true ]; then
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 ] if [ "$vhost_backup" = true ]; then
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 -r $vhost_dir/ $tmpdir/vhosts/
echo "Finished" >&2 echo "Finished" >&2
fi fi
} }
function logbackup { function logbackup {
if [ "$log_backup" = true ] if [ "$log_backup" = true ]; then
then
echo "Backing up system logs..." >&2 echo "Backing up system logs..." >&2
mkdir -p $tmpdir/syslogs mkdir -p $tmpdir/syslogs
case $system in case $system in
"rhel") "rhel")
cp /var/log/syslog $tmpdir/syslogs/ cp /var/log/syslog $tmpdir/syslogs/
cp /var/log/message $tmpdir/syslogs/ cp /var/log/message $tmpdir/syslogs/
if [ "$fail2ban_log" = true ] if [ "$fail2ban_log" = true ]; then
then cp /var/log/fail2ban.log $tmpdir/syslogs/
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 fi
if [ "$nginx" = true ]; then
if [ "$log_backup_web" = true ] mkdir -p $tmpdir/nginxlogs
then cp -r /var/log/nginx $tmpdir/nginxlogs
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 fi
fi
if [ "$log_purge" = true ] if [ "$log_purge" = true ]; then
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 [ "$apache" = true ] truncate -s 0 /var/log/httpd/*
then rm /var/log/httpd/*.gz
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 fi
;; if [ "$nginx" = true ]; then
truncate -s 0 /var/log/nginx/*
"ubuntu") rm /var/log/nginx/*.gz
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 fi
if [ "$fail2ban_log" = true ]; then
if [ "$log_backup_web" = true ] truncate -s 0 /var/log/fail2ban.log
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 fi
fi
;;
if [ "$log_purge" = true ] "ubuntu")
then cp /var/log/syslog $tmpdir/syslogs/
echo "Purging logs..." >&2 cp /var/log/message $tmpdir/syslogs/
truncate -s 0 /var/log/syslog
truncate -s 0 /var/log/message if [ "$fail2ban_log" = true ]; then
if [ "$apache" = true ] cp /var/log/fail2ban.log $tmpdir/syslogs/
then fi
truncate -s 0 /var/log/apache2/*
rm /var/log/apache2/*.gz if [ "$log_backup_web" = true ]; then
fi if [ "$apache" = true ]; then
if [ "$nginx" = true ] mkdir -p $tmpdir/apachelogs
then cp -r /var/log/apache2 $tmpdir/apachelogs
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 fi
esac if [ "$nginx" = true ]; then
fi 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
;;
esac
fi
} }
function push { function push {
if [ "$rsync_push" = true ] if [ "$rsync_push" = true ]; then
then echo "Pushing the backup package to $target_host..." >&2
#Push - Dockerized rsync -avz -e "ssh -i $target_key" $backup_path/backify-$timestamp.tar.gz $target_user@$target_host:$target_dir
if [ "$push_clean" = true ] if [ "$push_clean" = true ]; then
then echo "Removing archive..." >&2
rm /opt/backify-$timestamp.tar.gz rm $backup_path/backify-$timestamp.tar.gz
fi
fi fi
fi
} }
function dockerbackup { function dockerbackup {
if [ "$docker_enabled" = true] if [ "$docker_enabled" = true]; then
then if [ "$docker_images" = true]; then
if [ "$docker_images" = true] echo "Backing up Docker images..." >&2
then for i in $(docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/); do
echo "Backing up Docker images..." >&2 container_name=$i
for i in `docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/` echo -n "$container_name - "
do container_name=$i container_image=$(docker inspect --format='{{.Config.Image}}' $container_name)
echo -n "$container_name - " mkdir -p $tmpdir/containers/$container_name
container_image=`docker inspect --format='{{.Config.Image}}' $container_name` save_dir="$tmpdir/containers/$container_name/$container_name-image.tar"
mkdir -p $tmpdir/containers/$container_name docker save -o $save_dir $container_image
save_dir="$tmpdir/containers/$container_name/$container_name-image.tar" echo "Finished" >&2
docker save -o $save_dir $container_image done
echo "Finished" >&2
done
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 $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 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 $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 backup_db { function backup_db {
if [ "$db_all" = true ] if [ "$db_all" = true ]; then
then if [ "$database_type" = "mysql" ]; then
if [ "$database_type" = "mysql" ] mysqldump -u "$db_username" -p"$db_password" --all-databases >$tmpdir/db/db_all.sql
then elif [ "$database_type" = "postgresql" ]; then
mysqldump -u "$db_username" -p"$db_password" --all-databases > $tmpdir/db/db_all.sql pg_dumpall -U "$db_username" -f $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 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 fi
fi
} }
function runbackup { function runbackup {
# init, config check # init, config check
init init
# run system detection # run system detection
system system
if [ "$enabled" = true ] if [ "$enabled" = true ]; then
then # step 1 : create directory
# step 1 : create directory makedir
makedir # step 2 : www backup
# step 2 : www backup wwwbackup
wwwbackup # step 3 : vhost backup
# step 3 : vhost backup vhostbackup
vhostbackup # step 4: log backup
# step 4: log backup logbackup
logbackup # step 5: docker backup
# step 5: docker backup dockerbackup
dockerbackup # step 6: db backup
# step 6: db backup if [ "$db_backup" = true ]; then
if [ "$db_backup" = true ] backup_db
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 fi
# archive data
echo "Creating backup archive..." >&2
tar -czvf $backup_path/backify-$timestamp.tar.gz $tmpdir
# 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