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 |
| --- | --- | --- |
| 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_dir | ------> | Path to the webroot |
| 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_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 |
@ -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_password | ------> | Password for DB access |
| db_name | ------> | Name of database |
| db_all | ------> | Dumb all databases instead of specific one |
To-Do List
----------
- [ ] Rsync implementation via shell
- [ ] Rsync implementation via Docker
- [ ] Cron scheduler
| db_all | ------> | Dump all databases instead of specific one |

View File

@ -3,6 +3,7 @@
# Please double check Your settings
# --------------------------------------------------------
enabled=false #enable main function
backup_path='/opt/backify/' # location of backups
www_backup=false # backup wwwroot
www_dir='xyz' # wwwroot location
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_user="backup" # rsync target user
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_images=false # backup docker images
docker_volumes=false #backup docker volumes

140
main.sh
View File

@ -6,8 +6,7 @@ echo "Backify is starting, looking for configuration file..." >&2
config='backup.cfg'
secured_config='sbackup.cfg'
if [ ! -f "$config" ]
then
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
@ -23,20 +22,18 @@ source "$config"
echo "Configuration file loaded" >&2
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
}
function system {
if [ -f /etc/redhat-release ]
then
if [ -f /etc/redhat-release ]; then
echo "Discovered Red Hat-based OS..."
system='rhel'
elif [ -f /etc/lsb-release ]
then
elif [ -f /etc/lsb-release ]; then
echo "Discovered Ubuntu-based OS..."
system='ubuntu'
else
@ -49,13 +46,12 @@ echo "Discovered $system based OS..." >&2
function makedir {
timestamp=$(date +%Y%m%d_%H%M)
mkdir /tmp/backify-$timestamp
tmpdir="/tmp/backify-$timestamp"
mkdir -p $backup_path/backify-$timestamp
tmpdir="$backup_path/backify-$timestamp"
}
function wwwbackup {
if [ "$www_backup" = true ]
then
if [ "$www_backup" = true ]; then
echo "Backing up wwwroot..." >&2
mkdir -p $tmpdir/wwwdata
cp -r $www_dir/ $tmpdir/wwwdata/
@ -64,8 +60,7 @@ fi
}
function vhostbackup {
if [ "$vhost_backup" = true ]
then
if [ "$vhost_backup" = true ]; then
echo "Backing up vhosts..." >&2
mkdir -p $tmpdir/vhosts
cp -r $vhost_dir/ $tmpdir/vhosts/
@ -74,8 +69,7 @@ fi
}
function logbackup {
if [ "$log_backup" = true ]
then
if [ "$log_backup" = true ]; then
echo "Backing up system logs..." >&2
mkdir -p $tmpdir/syslogs
@ -84,42 +78,34 @@ function logbackup {
cp /var/log/syslog $tmpdir/syslogs/
cp /var/log/message $tmpdir/syslogs/
if [ "$fail2ban_log" = true ]
then
if [ "$fail2ban_log" = true ]; then
cp /var/log/fail2ban.log $tmpdir/syslogs/
fi
if [ "$log_backup_web" = true ]
then
if [ "$apache" = true ]
then
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
if [ "$nginx" = true ]; then
mkdir -p $tmpdir/nginxlogs
cp -r /var/log/nginx $tmpdir/nginxlogs
fi
fi
if [ "$log_purge" = true ]
then
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
if [ "$apache" = true ]; then
truncate -s 0 /var/log/httpd/*
rm /var/log/httpd/*.gz
fi
if [ "$nginx" = true ]
then
if [ "$nginx" = true ]; then
truncate -s 0 /var/log/nginx/*
rm /var/log/nginx/*.gz
fi
if [ "$fail2ban_log" = true ]
then
if [ "$fail2ban_log" = true ]; then
truncate -s 0 /var/log/fail2ban.log
fi
fi
@ -129,82 +115,72 @@ function logbackup {
cp /var/log/syslog $tmpdir/syslogs/
cp /var/log/message $tmpdir/syslogs/
if [ "$fail2ban_log" = true ]
then
if [ "$fail2ban_log" = true ]; then
cp /var/log/fail2ban.log $tmpdir/syslogs/
fi
if [ "$log_backup_web" = true ]
then
if [ "$apache" = true ]
then
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
if [ "$nginx" = true ]; then
mkdir -p $tmpdir/nginxlogs
cp -r /var/log/nginx $tmpdir/nginxlogs
fi
fi
if [ "$log_purge" = true ]
then
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
if [ "$apache" = true ]; then
truncate -s 0 /var/log/apache2/*
rm /var/log/apache2/*.gz
fi
if [ "$nginx" = true ]
then
if [ "$nginx" = true ]; then
truncate -s 0 /var/log/nginx/*
rm /var/log/nginx/*.gz
fi
if [ "$fail2ban_log" = true ]
then
if [ "$fail2ban_log" = true ]; then
truncate -s 0 /var/log/fail2ban.log
fi
fi
;;
esac
fi
}
function push {
if [ "$rsync_push" = true ]
then
#Push - Dockerized
if [ "$push_clean" = true ]
then
rm /opt/backify-$timestamp.tar.gz
if [ "$rsync_push" = true ]; then
echo "Pushing the backup package to $target_host..." >&2
rsync -avz -e "ssh -i $target_key" $backup_path/backify-$timestamp.tar.gz $target_user@$target_host:$target_dir
if [ "$push_clean" = true ]; then
echo "Removing archive..." >&2
rm $backup_path/backify-$timestamp.tar.gz
fi
fi
}
function dockerbackup {
if [ "$docker_enabled" = true]
then
if [ "$docker_images" = true]
then
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
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`
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
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
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 \
@ -216,13 +192,12 @@ function dockerbackup {
echo "Finished" >&2
done
fi
if [ "$docker_data" = true ]
then
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
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`
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
@ -232,21 +207,16 @@ function dockerbackup {
}
function backup_db {
if [ "$db_all" = true ]
then
if [ "$database_type" = "mysql" ]
then
if [ "$db_all" = true ]; then
if [ "$database_type" = "mysql" ]; then
mysqldump -u "$db_username" -p"$db_password" --all-databases >$tmpdir/db/db_all.sql
elif [ "$database_type" = "postgresql" ]
then
elif [ "$database_type" = "postgresql" ]; then
pg_dumpall -U "$db_username" -f $tmpdir/db/db_all.sql
fi
else
if [ "$database_type" = "mysql" ]
then
if [ "$database_type" = "mysql" ]; then
mysqldump -u "$db_username" -p"$db_password" "$db_name" >$tmpdir/db/$db_name.sql
elif [ "$database_type" = "postgresql" ]
then
elif [ "$database_type" = "postgresql" ]; then
pg_dump -U "$db_username" "$db_name" -f $tmpdir/db/$db_name.sql
fi
fi
@ -257,8 +227,7 @@ function runbackup {
init
# run system detection
system
if [ "$enabled" = true ]
then
if [ "$enabled" = true ]; then
# step 1 : create directory
makedir
# step 2 : www backup
@ -270,15 +239,16 @@ function runbackup {
# step 5: docker backup
dockerbackup
# step 6: db backup
if [ "$db_backup" = true ]
then
if [ "$db_backup" = true ]; then
backup_db
fi
# archive data
echo "Creating backup archive..." >&2
tar -czvf /opt/backify-$timestamp.tar.gz $tmpdir
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