2021-03-05 11:35:53 +00:00
|
|
|
#!/bin/bash
|
2021-03-19 06:06:07 +00:00
|
|
|
# postgres backup and log management
|
2021-03-05 11:35:53 +00:00
|
|
|
|
|
|
|
time_stamp=$(date '+%Y%m%d')
|
|
|
|
time_date=$(date '+%Y%m%d_%H%M%S')
|
|
|
|
backup_dir="$HOME/backup"
|
|
|
|
|
|
|
|
# backup
|
|
|
|
docker exec postgres pg_dump -U aqi | gzip > "$backup_dir/pg_$time_stamp.gz"
|
|
|
|
|
2021-05-09 05:26:38 +00:00
|
|
|
# rotate
|
|
|
|
find backup/ -type f -name "pg_*.gz" | sort -r | tail -n +15 | xargs --no-run-if-empty trash
|
|
|
|
|
2021-03-05 11:35:53 +00:00
|
|
|
# log
|
2021-03-19 06:06:07 +00:00
|
|
|
log_file="$backup_dir/pg_status.log"
|
2021-03-05 11:35:53 +00:00
|
|
|
|
2021-03-19 06:06:07 +00:00
|
|
|
query1="SELECT schemaname AS table_schema, \
|
2021-03-05 11:35:53 +00:00
|
|
|
relname AS table_name, \
|
|
|
|
pg_size_pretty(pg_relation_size(relid)) AS data_size \
|
|
|
|
FROM pg_catalog.pg_statio_user_tables \
|
|
|
|
ORDER BY pg_relation_size(relid) DESC;"
|
|
|
|
|
2021-03-19 06:06:07 +00:00
|
|
|
query2="SELECT schemaname,relname,n_live_tup \
|
|
|
|
FROM pg_stat_user_tables \
|
|
|
|
ORDER BY n_live_tup DESC;"
|
|
|
|
|
2021-03-05 11:35:53 +00:00
|
|
|
echo "postgres dump run at $time_date" > "$log_file"
|
2021-03-19 06:06:07 +00:00
|
|
|
echo "table size" >> "$log_file"
|
|
|
|
docker exec postgres psql -U aqi -c "$query1" >> "$log_file"
|
|
|
|
echo "row count" >> "$log_file"
|
|
|
|
docker exec postgres psql -U aqi -c "$query2" >> "$log_file"
|
2021-03-05 11:35:53 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
exit 0
|