1
0
Fork 0

initial commit

This commit is contained in:
root 2022-06-10 10:48:10 +02:00
commit a5cb7d0700
3 changed files with 95 additions and 0 deletions

14
README.md Normal file
View File

@ -0,0 +1,14 @@
# Server tools
Install: clone the repository and add that directory to your $PATH
\
## Pushing changes
Changes can only be pushed by owner and colaborators of this project. To preserve privacy of systems, configure project as such:
```bash
git config user.email "root@localhost"
git config user.email "root"
```

70
docker-db-dump Executable file
View File

@ -0,0 +1,70 @@
#!/bin/bash
# @params:
# 1* => docker database running container name
# 2* => directory to place compressed backups when finished
# 3 => (optional) number of backups to keep
## NEEDS packages: cpulimit
set -o allexport
if [[ $1 == '' || $2 == '' ]]; then
echo 'E| Parameters are incorrect. Please specify "container_name /path/for/backups"'
exit 1
fi
[[ ! -d $2 ]] && mkdir -p $2
set -e
umask 077
CLIM1=${CPU_LIMIT_DUMP:-70}
CLIM2=${CPU_LIMIT_XZ:-20}
IMGTYPE=$(docker inspect --format='{{.Config.Image}}' $1 | cut -f1 -d:)
[[ $BACKTS == '' ]] && BACKTS=`date +"%Y%m%d"`.`echo $RANDOM|md5sum|head -c8`
TMPDIR=/var/tmp/dumps
mkdir -p $TMPDIR
echo 'docker-db-dump: '$1-$BACKTS
if [[ $IMGTYPE == 'postgres' ]]; then
docker exec $1 env | grep -i postgres_ > /tmp/.env-docker-db-dump
source /tmp/.env-docker-db-dump && rm /tmp/.env-docker-db-dump
DB=${POSTGRES_DB}
if [[ $DB == '' ]]; then
DB=${POSTGRES_USER}
fi
DBU=''
if [[ $POSTGRES_USER != '' ]]; then
DBU='-U'${POSTGRES_USER}
fi
cpulimit -l $CLIM1 -f -- nice -n 19 docker exec -upostgres $1 \
pg_dump $DBU $DB > $TMPDIR/$1.sql
elif [[ $IMGTYPE == 'mariadb' || $IMGTYPE == 'mysql' ]]; then
cat <<EOLFF > /tmp/.mysql-docker-db-dump
#!/bin/bash
if [[ \$MYSQL_ROOT_PASSWORD != '' ]]; then
mysqldump -uroot -p\$MYSQL_ROOT_PASSWORD \$MYSQL_DATABASE
else
mysqldump -u\$MYSQL_USER -p\$MYSQL_PASSWORD \$MYSQL_DATABASE
fi
rm /docker-db-dump.sh
EOLFF
chmod +x /tmp/.mysql-docker-db-dump
docker cp /tmp/.mysql-docker-db-dump $1:/docker-db-dump.sh
rm /tmp/.mysql-docker-db-dump
cpulimit -l $CLIM1 -f -- nice -n 19 docker exec $1 \
/docker-db-dump.sh > $TMPDIR/$1.sql
else
echo 'FATAL: image not supported "'$IMGTYPE'"'
exit 2
fi
set -x
cpulimit -l $CLIM2 -f -- nice -n 19 xz -9 $TMPDIR/$1.sql
mv $TMPDIR/$1.sql.xz $2/$BACKTS-$1.sql.xz
cd $2/
if [[ $? == 0 ]]; then
rmkeep $1.sql.xz ${3:-14} # days to keep by default
fi

11
rmkeep Executable file
View File

@ -0,0 +1,11 @@
#!/bin/bash
MAXN=${2:-30}
num=$(ls -1rt | grep -- $1 | wc -l)
if [[ $num -lt $(($MAXN-1)) ]]; then
exit 1
fi
num=$(($num-$MAXN))
for fil in $(ls -1rt | grep -- $1 | head -n $num); do
rm -v $fil
done