eiru-backup.sh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/bash
  2. CONTAINER="database"
  3. DB_USER="odoo"
  4. ODOO_IMAGE="odoo/eiru:1.0"
  5. DATE=$(date +%Y%m%d)
  6. DAYS_TO_KEEP=15
  7. BKP_PATH="/mnt/backup/primary_local_bkp/pg"
  8. SYNC_PATH="/mnt/backup/secondary_local_bkp/pg"
  9. TMP_PATH="."
  10. # perform database backup if docker exist
  11. perform_backups()
  12. {
  13. local BKP_TODAY_PATH=$(printf "%s/backup_%s" "$BKP_PATH" "$DATE")
  14. if [ ! -d "$BKP_TODAY_PATH" ]; then
  15. mkdir "$BKP_TODAY_PATH"
  16. fi
  17. for DB_NAME in $(docker ps -a --format "{{.Names}}" --filter "ancestor=$ODOO_IMAGE"); do
  18. local TMP_FILE=$(printf "%s/%s.tar" "$TMP_PATH" "$DB_NAME")
  19. docker exec $CONTAINER pg_dump -U $DB_USER -d $DB_NAME -F tar -C -b -c -f $TMP_FILE
  20. docker cp $CONTAINER:$TMP_FILE $BKP_TODAY_PATH
  21. docker exec $CONTAINER rm $TMP_FILE
  22. done
  23. }
  24. # delete old backups
  25. delete_old_backups()
  26. {
  27. find $1 -maxdepth 1 -type d -name "backup_*" -mtime +$2 -exec rm -rf '{}' ';'
  28. }
  29. # sync two backup paths
  30. sync_backups_locally()
  31. {
  32. rsync -arz $1 $2
  33. }
  34. # sync two backup paths remotely
  35. sync_backups_remotely()
  36. {
  37. rsync -arze "ssh -p $5" $1 "$2@$3:$4"
  38. }
  39. if [ ! -x "$(command -v docker)" ]; then
  40. echo "cannot perform backup"
  41. exit 1
  42. fi
  43. perform_backups
  44. delete_old_backups $BKP_PATH $DAYS_TO_KEEP
  45. # if [ ! -x "$(command -v rsync)" ]; then
  46. # echo "cannot sync backups path"
  47. # exit 1
  48. # fi
  49. # sync_backups_locally "$BKP_PATH/" $SYNC_PATH
  50. exit 0