123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #!/bin/bash
- CONTAINER="database"
- DB_USER="odoo"
- ODOO_IMAGE="odoo/eiru:1.0"
- DATE=$(date +%Y%m%d)
- DAYS_TO_KEEP=15
- BKP_PATH="/mnt/backup/primary_local_bkp/pg"
- SYNC_PATH="/mnt/backup/secondary_local_bkp/pg"
- TMP_PATH="."
- # perform database backup if docker exist
- perform_backups()
- {
- local BKP_TODAY_PATH=$(printf "%s/backup_%s" "$BKP_PATH" "$DATE")
- if [ ! -d "$BKP_TODAY_PATH" ]; then
- mkdir "$BKP_TODAY_PATH"
- fi
- for DB_NAME in $(docker ps -a --format "{{.Names}}" --filter "ancestor=$ODOO_IMAGE"); do
- local TMP_FILE=$(printf "%s/%s.tar" "$TMP_PATH" "$DB_NAME")
- docker exec $CONTAINER pg_dump -U $DB_USER -d $DB_NAME -F tar -C -b -c -f $TMP_FILE
- docker cp $CONTAINER:$TMP_FILE $BKP_TODAY_PATH
- docker exec $CONTAINER rm $TMP_FILE
- done
- }
- # delete old backups
- delete_old_backups()
- {
- find $1 -maxdepth 1 -type d -name "backup_*" -mtime +$2 -exec rm -rf '{}' ';'
- }
- # sync two backup paths
- sync_backups_locally()
- {
- rsync -arz $1 $2
- }
- # sync two backup paths remotely
- sync_backups_remotely()
- {
- rsync -arze "ssh -p $5" $1 "$2@$3:$4"
- }
- if [ ! -x "$(command -v docker)" ]; then
- echo "cannot perform backup"
- exit 1
- fi
- perform_backups
- delete_old_backups $BKP_PATH $DAYS_TO_KEEP
- # if [ ! -x "$(command -v rsync)" ]; then
- # echo "cannot sync backups path"
- # exit 1
- # fi
- # sync_backups_locally "$BKP_PATH/" $SYNC_PATH
- exit 0
|