Rsync implementation, formatting
This commit is contained in:
parent
6c40c9898d
commit
8e2e28602f
13
README.MD
13
README.MD
@ -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
|
|
@ -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
|
||||||
|
140
main.sh
140
main.sh
@ -6,8 +6,7 @@ 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
|
||||||
@ -23,20 +22,18 @@ 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
|
||||||
@ -49,13 +46,12 @@ 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/
|
||||||
@ -64,8 +60,7 @@ 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/
|
||||||
@ -74,8 +69,7 @@ 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
|
||||||
|
|
||||||
@ -84,42 +78,34 @@ function logbackup {
|
|||||||
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
|
fi
|
||||||
|
|
||||||
if [ "$log_backup_web" = true ]
|
if [ "$log_backup_web" = true ]; then
|
||||||
then
|
if [ "$apache" = true ]; then
|
||||||
if [ "$apache" = true ]
|
|
||||||
then
|
|
||||||
mkdir -p $tmpdir/apachelogs
|
mkdir -p $tmpdir/apachelogs
|
||||||
cp -r /var/log/httpd $tmpdir/apachelogs
|
cp -r /var/log/httpd $tmpdir/apachelogs
|
||||||
fi
|
fi
|
||||||
if [ "$nginx" = true ]
|
if [ "$nginx" = true ]; then
|
||||||
then
|
|
||||||
mkdir -p $tmpdir/nginxlogs
|
mkdir -p $tmpdir/nginxlogs
|
||||||
cp -r /var/log/nginx $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 ]
|
if [ "$apache" = true ]; then
|
||||||
then
|
|
||||||
truncate -s 0 /var/log/httpd/*
|
truncate -s 0 /var/log/httpd/*
|
||||||
rm /var/log/httpd/*.gz
|
rm /var/log/httpd/*.gz
|
||||||
fi
|
fi
|
||||||
if [ "$nginx" = true ]
|
if [ "$nginx" = true ]; then
|
||||||
then
|
|
||||||
truncate -s 0 /var/log/nginx/*
|
truncate -s 0 /var/log/nginx/*
|
||||||
rm /var/log/nginx/*.gz
|
rm /var/log/nginx/*.gz
|
||||||
fi
|
fi
|
||||||
if [ "$fail2ban_log" = true ]
|
if [ "$fail2ban_log" = true ]; then
|
||||||
then
|
|
||||||
truncate -s 0 /var/log/fail2ban.log
|
truncate -s 0 /var/log/fail2ban.log
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@ -129,82 +115,72 @@ function logbackup {
|
|||||||
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
|
fi
|
||||||
|
|
||||||
if [ "$log_backup_web" = true ]
|
if [ "$log_backup_web" = true ]; then
|
||||||
then
|
if [ "$apache" = true ]; then
|
||||||
if [ "$apache" = true ]
|
|
||||||
then
|
|
||||||
mkdir -p $tmpdir/apachelogs
|
mkdir -p $tmpdir/apachelogs
|
||||||
cp -r /var/log/apache2 $tmpdir/apachelogs
|
cp -r /var/log/apache2 $tmpdir/apachelogs
|
||||||
fi
|
fi
|
||||||
if [ "$nginx" = true ]
|
if [ "$nginx" = true ]; then
|
||||||
then
|
|
||||||
mkdir -p $tmpdir/nginxlogs
|
mkdir -p $tmpdir/nginxlogs
|
||||||
cp -r /var/log/nginx $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 ]
|
if [ "$apache" = true ]; then
|
||||||
then
|
|
||||||
truncate -s 0 /var/log/apache2/*
|
truncate -s 0 /var/log/apache2/*
|
||||||
rm /var/log/apache2/*.gz
|
rm /var/log/apache2/*.gz
|
||||||
fi
|
fi
|
||||||
if [ "$nginx" = true ]
|
if [ "$nginx" = true ]; then
|
||||||
then
|
|
||||||
truncate -s 0 /var/log/nginx/*
|
truncate -s 0 /var/log/nginx/*
|
||||||
rm /var/log/nginx/*.gz
|
rm /var/log/nginx/*.gz
|
||||||
fi
|
fi
|
||||||
if [ "$fail2ban_log" = true ]
|
if [ "$fail2ban_log" = true ]; then
|
||||||
then
|
|
||||||
truncate -s 0 /var/log/fail2ban.log
|
truncate -s 0 /var/log/fail2ban.log
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
fi
|
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]
|
|
||||||
then
|
|
||||||
echo "Backing up Docker images..." >&2
|
echo "Backing up Docker images..." >&2
|
||||||
for i in `docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/`
|
for i in $(docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/); do
|
||||||
do container_name=$i
|
container_name=$i
|
||||||
echo -n "$container_name - "
|
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
|
mkdir -p $tmpdir/containers/$container_name
|
||||||
save_dir="$tmpdir/containers/$container_name/$container_name-image.tar"
|
save_dir="$tmpdir/containers/$container_name/$container_name-image.tar"
|
||||||
docker save -o $save_dir $container_image
|
docker save -o $save_dir $container_image
|
||||||
echo "Finished" >&2
|
echo "Finished" >&2
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
if [ "$docker_volumes" = true ]
|
if [ "$docker_volumes" = true ]; then
|
||||||
then
|
|
||||||
echo "Backing up Docker volumes..." >&2
|
echo "Backing up Docker volumes..." >&2
|
||||||
#Thanks piscue :)
|
#Thanks piscue :)
|
||||||
for i in `docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/`
|
for i in $(docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/); do
|
||||||
do container_name=$i
|
container_name=$i
|
||||||
mkdir -p $tmpdir/containers/$container_name
|
mkdir -p $tmpdir/containers/$container_name
|
||||||
echo -n "$container_name - "
|
echo -n "$container_name - "
|
||||||
docker run --rm --userns=host \
|
docker run --rm --userns=host \
|
||||||
@ -216,13 +192,12 @@ function dockerbackup {
|
|||||||
echo "Finished" >&2
|
echo "Finished" >&2
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
if [ "$docker_data" = true ]
|
if [ "$docker_data" = true ]; then
|
||||||
then
|
|
||||||
echo "Backing up container information..." >&2
|
echo "Backing up container information..." >&2
|
||||||
for i in `docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/`
|
for i in $(docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/); do
|
||||||
do container_name=$i
|
container_name=$i
|
||||||
echo -n "$container_name - "
|
echo -n "$container_name - "
|
||||||
container_data=`docker inspect $container_name`
|
container_data=$(docker inspect $container_name)
|
||||||
mkdir -p $tmpdir/containers/$container_name
|
mkdir -p $tmpdir/containers/$container_name
|
||||||
echo $container_data >$tmpdir/containers/$container_name/$container_name-data.txt
|
echo $container_data >$tmpdir/containers/$container_name/$container_name-data.txt
|
||||||
echo "Finished" >&2
|
echo "Finished" >&2
|
||||||
@ -232,21 +207,16 @@ function dockerbackup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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" ]
|
|
||||||
then
|
|
||||||
mysqldump -u "$db_username" -p"$db_password" --all-databases >$tmpdir/db/db_all.sql
|
mysqldump -u "$db_username" -p"$db_password" --all-databases >$tmpdir/db/db_all.sql
|
||||||
elif [ "$database_type" = "postgresql" ]
|
elif [ "$database_type" = "postgresql" ]; then
|
||||||
then
|
|
||||||
pg_dumpall -U "$db_username" -f $tmpdir/db/db_all.sql
|
pg_dumpall -U "$db_username" -f $tmpdir/db/db_all.sql
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
if [ "$database_type" = "mysql" ]
|
if [ "$database_type" = "mysql" ]; then
|
||||||
then
|
|
||||||
mysqldump -u "$db_username" -p"$db_password" "$db_name" >$tmpdir/db/$db_name.sql
|
mysqldump -u "$db_username" -p"$db_password" "$db_name" >$tmpdir/db/$db_name.sql
|
||||||
elif [ "$database_type" = "postgresql" ]
|
elif [ "$database_type" = "postgresql" ]; then
|
||||||
then
|
|
||||||
pg_dump -U "$db_username" "$db_name" -f $tmpdir/db/$db_name.sql
|
pg_dump -U "$db_username" "$db_name" -f $tmpdir/db/$db_name.sql
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@ -257,8 +227,7 @@ function runbackup {
|
|||||||
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
|
||||||
@ -270,15 +239,16 @@ function runbackup {
|
|||||||
# step 5: docker backup
|
# step 5: docker backup
|
||||||
dockerbackup
|
dockerbackup
|
||||||
# step 6: db backup
|
# step 6: db backup
|
||||||
if [ "$db_backup" = true ]
|
if [ "$db_backup" = true ]; then
|
||||||
then
|
|
||||||
backup_db
|
backup_db
|
||||||
fi
|
fi
|
||||||
# archive data
|
# archive data
|
||||||
echo "Creating backup archive..." >&2
|
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 data to server
|
||||||
push
|
push
|
||||||
|
# remove temp files
|
||||||
|
rm -r $tmpdir
|
||||||
echo "Voila, enjoy the rest of the day" >&2
|
echo "Voila, enjoy the rest of the day" >&2
|
||||||
else
|
else
|
||||||
echo "Backup is disabled in the configuration" >&2
|
echo "Backup is disabled in the configuration" >&2
|
||||||
|
Loading…
Reference in New Issue
Block a user