Last active
September 1, 2024 04:25
-
-
Save composite/af3b85f8a9604c56377fb80fb001dba8 to your computer and use it in GitHub Desktop.
My docker compose and data backup and refresh container up to date solution.
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
#!/bin/bash | |
SHDIR=$(dirname $0) | |
NOWDA=$(date '+%Y%m%d') | |
NOWDT=$(date '+%Y%m%d%H%M%S') | |
BUDIR="/data/backup/docker/compose/$NOWDA" | |
mkdir -p "$BUDIR" | |
find "$SHDIR" -maxdepth 3 -type d 2>/dev/null | while read COMPOSE_DIR | |
do | |
COMPOSE_NM=$(basename "$COMPOSE_DIR") | |
if [ ! -f "$COMPOSE_DIR/docker-compose.yml" ]; then continue; fi | |
echo "$COMPOSE_NM : $COMPOSE_DIR" | |
TARFILE="$COMPOSE_NM-$NOWDT.tar.gz" | |
find "$COMPOSE_DIR" -maxdepth 1 -type f -exec basename {} \; 2>/dev/null | xargs tar -czf "$BUDIR/$TARFILE" -C "$COMPOSE_DIR" | |
logger "Docker Compose Backup: Successfully backup! $BUDIR/$TARFILE" | |
done |
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
#!/bin/bash | |
#set -ex | |
day=$(date '+%Y%m%d') | |
now=$(date '+%Y-%m-%d %H:%M:%S') | |
dir=$(dirname "$0") | |
out=$(sudo docker compose --project-directory "$dir" -f "$dir/docker-compose.yml" pull) | |
log="$dir/log/pull-$day.log" | |
mkdir -p $(dirname "$log") | |
if [[ $out == *"Pull complete"* ]]; then | |
echo "application-dev - $now : Updated image detected. restarting with new image..." | tee -a "$log" | |
pushd . | |
cd "$dir" | |
sudo docker compose down | |
sudo docker compose up -d | |
sudo docker image prune -f | |
popd | |
echo "application-dev - $now : Done." | tee -a "$log" | |
else | |
echo "application-dev - $now : Up to date." | tee -a "$log" | |
fi |
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
#!/bin/bash | |
SHDIR=$(dirname $0) | |
NOWDA=$(date '+%Y%m%d') | |
NOWDT=$(date '+%Y%m%d%H%M%S') | |
BUDIR="/data/backup/volumes/$NOWDA" | |
mkdir -p "$BUDIR" | |
TARFILE="$BUDIR/volumes-$NOWDT.tar.bz2" | |
sudo find "$SHDIR" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | sudo xargs tar -cjf "$TARFILE" -C "$SHDIR" | |
# sudo chown 1000:1000 "$TARFILE" | |
logger "Docker Volumes Backup: $TARFILE" |
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
#!/bin/bash | |
SHDIR=$(dirname $0) | |
NOWDA=$(date '+%Y%m%d') | |
NOWDT=$(date '+%Y%m%d%H%M%S') | |
BUDIR="/data/backup/postgres/dump/$NOWDA" | |
declare -A DB_MAP | |
# FORMAT: DB_MAP["DB_NAME"]="DB_USER DB_PASSWORD" | |
# DB USER AND PASSWORD SPLITTED BY SPACES. | |
DB_MAP["mydb"]="mydbuser mydbpassword" | |
DB_MAP["yourdb"]="yourdbuser yourdbpassword" | |
mkdir -p "$BUDIR" | |
for key in "${!DB_MAP[@]}" | |
do | |
read -a val <<< "${DB_MAP[$key]}" | |
#echo "PGPASSWORD=${val[1]} pg_dump -U ${val[0]} -F t ${key}" | |
sudo docker compose --project-directory "$SHDIR" -f "$SHDIR/docker-compose.yml"\ | |
exec db sh -c "PGPASSWORD=${val[1]} pg_dump -h localhost -U ${val[0]} -F t ${key}" |\ | |
gzip > "$BUDIR/$key-$NOWDT.tar.gz" | |
sudo chown ${USER}:${USER} "$BUDIR/$key-$NOWDT.tar.gz" | |
logger "Successfully backup to '$BUDIR/$key-$NOWDT.tgz'." | |
# if rclone copy "$BUDIR/$key-$NOWDT.tgz" googledrive:postgres-dev/$NOWDA; then | |
# logger "Successfully backup to remote in 'postgres-dev/$NOWDA/$key-$NOWDT.tgz'." | |
# fi | |
done | |
#BEMON=$(date -d "-1 month" '+%Y%m%d') | |
#logger "Checking previous month backup on 'postgres-dev/$BEMON'..." | |
#if rclone lsd googledrive:postgres-dev/$BEMON >/dev/null 2>&1; then | |
# rclone purge --retries 0 googledrive:postgres-dev/$BEMON >/dev/null 2>&1 | |
# logger "Successfully purged 'postgres-dev/$BEMON'." | |
#fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment