forked from alaub81/backup_docker_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
root
committed
Feb 10, 2021
1 parent
8bee08a
commit 1f6ef6c
Showing
4 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env bash | ||
######################################################################### | ||
#Name: backup-docker-compose.sh | ||
#Subscription: This Script backups the docker compose project folder | ||
#to a backup directory | ||
##by A. Laub | ||
#andreas[-at-]laub-home.de | ||
# | ||
#License: | ||
#This program is free software: you can redistribute it and/or modify it | ||
#under the terms of the GNU General Public License as published by the | ||
#Free Software Foundation, either version 3 of the License, or (at your option) | ||
#any later version. | ||
#This program is distributed in the hope that it will be useful, | ||
#but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
#or FITNESS FOR A PARTICULAR PURPOSE. | ||
######################################################################### | ||
#Set the language | ||
export LANG="en_US.UTF-8" | ||
#Load the Pathes | ||
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | ||
# set the variables | ||
|
||
# Where to store the Backup files? | ||
BACKUPDIR=/backup/compose | ||
|
||
# How many Days should a backup be available? | ||
DAYS=2 | ||
|
||
# Timestamp definition for the backupfiles (example: $(date +"%Y%m%d%H%M") = 20200124-2034) | ||
TIMESTAMP=$(date +"%Y%m%d%H%M") | ||
|
||
# Which Docker Compose Project you want to backup? | ||
# Docker Compose Project pathes separated by space | ||
#COMPOSE="/opt/project1 /opt/project2" | ||
# you can use the following two big command to read out all compose projects | ||
# uncommend if you like to read automatic all projects: | ||
ALLCONTAINER=$(docker ps --format '{{.Names}}') | ||
ALLPROJECTS=$(for i in $ALLCONTAINER; do docker inspect --format '{{ index .Config.Labels "com.docker.compose.project.working_dir"}}' $i; done | sort -u) | ||
# then to use all projects without filtering it: | ||
COMPOSE=$ALLPROJECTS | ||
# you can filter all Compose Projects with grep (include only) or grep -v (exclude) or a combination | ||
# to do a filter for 2 or more arguments separate them with "\|" | ||
# example: $(echo $ALLPROJECTS |grep 'project1\|project2' | grep -v 'database') | ||
# to use volumes with name project1 and project2 but not database | ||
#COMPOSE=$(echo -e "$ALLPROJECTS" | grep 'project1\|project2' | grep -v 'database') | ||
#COMPOSE=$(echo -e "$ALLPROJECTS" | grep -v 'mailcow-dockerized') | ||
|
||
|
||
### Do the stuff | ||
echo -e "Start $TIMESTAMP Backup for Docker Compose Projects:\n" | ||
if [ ! -d $BACKUPDIR ]; then | ||
mkdir -p $BACKUPDIR | ||
fi | ||
|
||
for i in $COMPOSE; do | ||
PROJECTNAME=${i##*/} | ||
echo -e " Backup von Compose Project:\n * $PROJECTNAME"; | ||
cd $i | ||
tar -czf $BACKUPDIR/$PROJECTNAME-$TIMESTAMP.tar.gz . | ||
# dont delete last old backups! | ||
OLD_BACKUPS=$(ls -1 $BACKUPDIR/$PROJECTNAME*.tar.gz |wc -l) | ||
if [ $OLD_BACKUPS -gt $DAYS ]; then | ||
find $BACKUPDIR -name "$PROJECTNAME*.tar.gz" -daystart -mtime +$DAYS -delete | ||
fi | ||
done | ||
echo -e "\n$TIMESTAMP Backup for Compose Projects completed\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#!/usr/bin/env bash | ||
######################################################################### | ||
#Name: backup-docker-mysql.sh | ||
#Subscription: This Script backups docker mysql or mariadb containers, | ||
#or better dumps their database to a backup directory | ||
##by A. Laub | ||
#andreas[-at-]laub-home.de | ||
# | ||
#License: | ||
#This program is free software: you can redistribute it and/or modify it | ||
#under the terms of the GNU General Public License as published by the | ||
#Free Software Foundation, either version 3 of the License, or (at your option) | ||
#any later version. | ||
#This program is distributed in the hope that it will be useful, | ||
#but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
#or FITNESS FOR A PARTICULAR PURPOSE. | ||
######################################################################### | ||
#Set the language | ||
export LANG="en_US.UTF-8" | ||
#Load the Pathes | ||
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | ||
# set the variables | ||
|
||
# Where to store the Backup files? | ||
BACKUPDIR=/backup/mysql | ||
|
||
# How many Days should a backup be available? | ||
DAYS=2 | ||
|
||
# Timestamp definition for the backupfiles (example: $(date +"%Y%m%d%H%M") = 20200124-2034) | ||
TIMESTAMP=$(date +"%Y%m%d%H%M") | ||
|
||
# Which Containers do you want to backup? | ||
# Container names separated by space | ||
#CONTAINER="mysqlcontainer1 mysqlcontainer2 mysqlcontainer3" | ||
# you can use "$(docker ps --format '{{.Names}}:{{.Image}}' | grep 'mysql\|mariadb' | cut -d":" -f1)" | ||
# for all containers which are using mysql or mariadb images | ||
#CONTAINER=$(docker ps --format '{{.Names}}:{{.Image}}' | grep 'mysql\|mariadb' | cut -d":" -f1) | ||
# you can filter all containers with grep (include only) or grep -v (exclude) or a combination of both | ||
# to do a filter for 2 or more arguments separate them with "\|" | ||
# example: $(docker ps --format '{{.Names}}:{{.Image}}' | grep 'mysql\|mariadb' | cut -d":" -f1 | grep -v 'container1\|container2') | ||
#CONTAINER=$(docker ps --format '{{.Names}}:{{.Image}}' | grep 'mysql\|mariadb' | cut -d":" -f1 | grep -v 'container1\|container2') | ||
CONTAINER=$(docker ps --format '{{.Names}}:{{.Image}}' | grep 'mysql\|mariadb' | cut -d":" -f1) | ||
|
||
|
||
### Do the stuff | ||
echo -e "Start $TIMESTAMP Backup for Databases: \n" | ||
if [ ! -d $BACKUPDIR ]; then | ||
mkdir -p $BACKUPDIR | ||
fi | ||
|
||
for i in $CONTAINER; do | ||
MYSQL_DATABASE=$(docker exec $i env | grep MYSQL_DATABASE |cut -d"=" -f2) | ||
MYSQL_PWD=$(docker exec $i env | grep MYSQL_ROOT_PASSWORD |cut -d"=" -f2) | ||
echo -e " create Backup for Database on Container:\n * $MYSQL_DATABASE DB on $i"; | ||
docker exec -e MYSQL_DATABASE=$MYSQL_DATABASE -e MYSQL_PWD=$MYSQL_PWD \ | ||
$i /usr/bin/mysqldump -u root $MYSQL_DATABASE \ | ||
| gzip > $BACKUPDIR/$i-$MYSQL_DATABASE-$TIMESTAMP.sql.gz | ||
# dont delete last old backups! | ||
OLD_BACKUPS=$(ls -1 $BACKUPDIR/$i*.gz |wc -l) | ||
if [ $OLD_BACKUPS -gt $DAYS ]; then | ||
find $BACKUPDIR -name "$i*.gz" -daystart -mtime +$DAYS -delete | ||
fi | ||
done | ||
echo -e "\n$TIMESTAMP Backup for Databases completed\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env bash | ||
######################################################################### | ||
#Name: backup-docker-postgres.sh | ||
#Subscription: This Script backups docker postgres containers, | ||
#or better dumps their databases to a backup directory | ||
##by A. Laub | ||
#andreas[-at-]laub-home.de | ||
# | ||
#License: | ||
#This program is free software: you can redistribute it and/or modify it | ||
#under the terms of the GNU General Public License as published by the | ||
#Free Software Foundation, either version 3 of the License, or (at your option) | ||
#any later version. | ||
#This program is distributed in the hope that it will be useful, | ||
#but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
#or FITNESS FOR A PARTICULAR PURPOSE. | ||
######################################################################### | ||
#Set the language | ||
export LANG="en_US.UTF-8" | ||
#Load the Pathes | ||
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | ||
# set the variables | ||
|
||
# Where to store the Backup files? | ||
BACKUPDIR=/backup/postgres | ||
|
||
# How many Days should a backup be available? | ||
DAYS=2 | ||
|
||
# Timestamp definition for the backupfiles (example: $(date +"%Y%m%d%H%M") = 20200124-2034) | ||
TIMESTAMP=$(date +"%Y%m%d%H%M") | ||
|
||
# Which Containers do you want to backup? | ||
# Container names separated by space | ||
#CONTAINER="postgrescontainer1 postgrescontainer2 postgrescontainer3" | ||
# you can use "$(docker ps --format '{{.Names}}:{{.Image}}' | grep 'postgres' | cut -d":" -f1)" | ||
# for all containers which are using postgres images | ||
#CONTAINER=$(docker ps --format '{{.Names}}:{{.Image}}' | grep 'postgres' | cut -d":" -f1) | ||
# you can filter all containers with grep (include only) or grep -v (exclude) or a combination of both | ||
# to do a filter for 2 or more arguments separate them with "\|" | ||
# example: $(docker ps --format '{{.Names}}:{{.Image}}' | grep 'postgres' | cut -d":" -f1 | grep -v 'container1\|container2') | ||
#CONTAINER=$(docker ps --format '{{.Names}}:{{.Image}}' | grep 'postgres' | cut -d":" -f1 | grep -v 'container1\|container2') | ||
CONTAINER=$(docker ps --format '{{.Names}}:{{.Image}}' | grep 'postgres' | cut -d":" -f1) | ||
|
||
|
||
### Do the stuff | ||
echo -e "Start $TIMESTAMP Backup for Databases: \n" | ||
if [ ! -d $BACKUPDIR ]; then | ||
mkdir -p $BACKUPDIR | ||
fi | ||
|
||
for i in $CONTAINER; do | ||
POSTGRES_USER=$(docker exec $i env | grep POSTGRES_USER |cut -d"=" -f2) | ||
echo -e " create Backup for Database on Container:\n * $i"; | ||
docker exec $i pg_dumpall -c -U $POSTGRES_USER | gzip > $BACKUPDIR/$i-$TIMESTAMP.sql.gz | ||
# dont delete last old backups! | ||
OLD_BACKUPS=$(ls -1 $BACKUPDIR/$i*.gz |wc -l) | ||
if [ $OLD_BACKUPS -gt $DAYS ]; then | ||
find $BACKUPDIR -name "$i*.gz" -daystart -mtime +$DAYS -delete | ||
fi | ||
done | ||
echo -e "\n$TIMESTAMP Backup for Databases completed\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/bin/bash | ||
######################################################################### | ||
#Name: backup-docker-volumes.sh | ||
#Subscription: This Script backups docker volumes to a backup directory | ||
##by A. Laub | ||
#andreas[-at-]laub-home.de | ||
# | ||
#License: | ||
#This program is free software: you can redistribute it and/or modify it | ||
#under the terms of the GNU General Public License as published by the | ||
#Free Software Foundation, either version 3 of the License, or (at your option) | ||
#any later version. | ||
#This program is distributed in the hope that it will be useful, | ||
#but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
#or FITNESS FOR A PARTICULAR PURPOSE. | ||
######################################################################### | ||
#Set the language | ||
export LANG="en_US.UTF-8" | ||
#Load the Pathes | ||
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | ||
# set the variables | ||
|
||
# Where to store the Backup files? | ||
BACKUPDIR=/backup/volumes | ||
|
||
# How many Days should a backup be available? | ||
DAYS=2 | ||
|
||
# Timestamp definition for the backupfiles (example: $(date +"%Y%m%d%H%M") = 20200124-2034) | ||
TIMESTAMP=$(date +"%Y%m%d%H%M") | ||
|
||
# Which Volumes you want to backup? | ||
# Volumenames separated by space | ||
#VOLUME="project1_data_container1 project2_data_container1" | ||
# you can use "$(docker volume ls -q)" for all volumes | ||
VOLUME=$(docker volume ls -q) | ||
# you can filter all Volumes with grep (include only) or grep -v (exclude) or a combination | ||
# to do a filter for 2 or more arguments separate them with "\|" | ||
# example: $(docker volume ls -q |grep 'project1\|project2' | grep -v 'database') | ||
# to use volumes with name project1 and project2 but not database | ||
#VOLUME=$(docker volume ls -q |grep 'project1\|project2' | grep -v 'database') | ||
#VOLUME=$(docker volume ls -q | grep -v 'mailcowdockerized\|_db') | ||
|
||
# if you want to use memory limitation. Must be supported by the kernel. | ||
#MEMORYLIMIT="-m 35m" | ||
|
||
### Do the stuff | ||
echo -e "Start $TIMESTAMP Backup for Volumes:\n" | ||
if [ ! -d $BACKUPDIR ]; then | ||
mkdir -p $BACKUPDIR | ||
fi | ||
|
||
for i in $VOLUME; do | ||
echo -e " Backup von Volume:\n * $i"; | ||
docker run --rm \ | ||
-v $BACKUPDIR:/backup \ | ||
-v $i:/data:ro \ | ||
-e TIMESTAMP=$TIMESTAMP \ | ||
-e i=$i ${MEMORYLIMIT} \ | ||
--name volumebackup \ | ||
alpine sh -c "cd /data && /bin/tar -czf /backup/$i-$TIMESTAMP.tar.gz ." | ||
#debian:stretch-slim bash -c "cd /data && /bin/tar -czf /backup/$i-$TIMESTAMP.tar.gz ." | ||
# dont delete last old backups! | ||
OLD_BACKUPS=$(ls -1 $BACKUPDIR/$i*.tar.gz |wc -l) | ||
if [ $OLD_BACKUPS -gt $DAYS ]; then | ||
find $BACKUPDIR -name "$i*.tar.gz" -daystart -mtime +$DAYS -delete | ||
fi | ||
done | ||
echo -e "\n$TIMESTAMP Backup for Volumes completed\n" |