Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

How to move older than one month (pg_logs) logs to another mount point in PostgreSQL ?

Ans:
As Postgres User
This is my log files:
 [username@hostname pg_log]# ll -h
total 245G
-rw-------. 1 postgres postgres 2.8G Jul  5 00:00 postgresql-2017-07-04_000000.log
-rw-------. 1 postgres postgres 2.4G Jul  6 00:00 postgresql-2017-07-05_000000.log
-rw-------. 1 postgres postgres 3.0G Jul  7 00:00 postgresql-2017-07-06_000000.log
-rw-------. 1 postgres postgres 3.4G Jul  8 00:00 postgresql-2017-07-07_000000.log
-rw-------. 1 postgres postgres 2.5G Jul  9 00:00 postgresql-2017-07-08_000000.log
-rw-------. 1 postgres postgres 2.1G Jul 10 00:00 postgresql-2017-07-09_000000.log
-rw-------. 1 postgres postgres 3.4G Jul 11 00:00 postgresql-2017-07-10_000000.log
-rw-------. 1 postgres postgres 3.4G Jul 12 00:00 postgresql-2017-07-11_000000.log
-rw-------. 1 postgres postgres 3.8G Jul 13 00:00 postgresql-2017-07-12_000000.log
-rw-------. 1 postgres postgres 3.9G Jul 14 00:00 postgresql-2017-07-13_000000.log
-rw-------. 1 postgres postgres 3.4G Jul 15 00:00 postgresql-2017-07-14_000000.log
-rw-------. 1 postgres postgres 3.7G Jul 16 00:00 postgresql-2017-07-15_000000.log
-rw-------. 1 postgres postgres 2.4G Jul 17 00:00 postgresql-2017-07-16_000000.log
-rw-------. 1 postgres postgres 3.9G Jul 18 00:00 postgresql-2017-07-17_000000.log
-rw-------. 1 postgres postgres 3.5G Jul 19 00:00 postgresql-2017-07-18_000000.log
-rw-------. 1 postgres postgres 3.6G Jul 19 23:59 postgresql-2017-07-19_000000.log
-rw-------. 1 postgres postgres 3.5G Jul 21 00:00 postgresql-2017-07-20_000000.log
-rw-------. 1 postgres postgres 3.2G Jul 22 00:00 postgresql-2017-07-21_000000.log
-rw-------. 1 postgres postgres 2.7G Jul 23 00:00 postgresql-2017-07-22_000000.log
-rw-------. 1 postgres postgres 2.5G Jul 24 00:00 postgresql-2017-07-23_000000.log
-rw-------. 1 postgres postgres 3.2G Jul 25 00:00 postgresql-2017-07-24_000000.log
-rw-------. 1 postgres postgres 3.6G Jul 25 23:59 postgresql-2017-07-25_000000.log
-rw-------. 1 postgres postgres 3.5G Jul 27 00:00 postgresql-2017-07-26_000000.log
-rw-------. 1 postgres postgres 3.6G Jul 28 00:00 postgresql-2017-07-27_000000.log
-rw-------. 1 postgres postgres 3.8G Jul 28 23:59 postgresql-2017-07-28_000000.log
-rw-------. 1 postgres postgres 3.4G Jul 30 00:00 postgresql-2017-07-29_000000.log
-rw-------. 1 postgres postgres 2.7G Jul 31 00:00 postgresql-2017-07-30_000000.log
-rw-------. 1 postgres postgres 3.4G Aug  1 00:00 postgresql-2017-07-31_000000.log
-rw-------. 1 postgres postgres 2.5G Aug  2 00:00 postgresql-2017-08-01_000000.log
-rw-------. 1 postgres postgres 2.7G Aug  3 00:00 postgresql-2017-08-02_000000.log
-rw-------. 1 postgres postgres 2.6G Aug  4 00:00 postgresql-2017-08-03_000000.log
-rw-------. 1 postgres postgres 2.8G Aug  4 23:59 postgresql-2017-08-04_000000.log
-rw-------. 1 postgres postgres 2.6G Aug  6 00:00 postgresql-2017-08-05_000000.log
-rw-------. 1 postgres postgres 2.1G Aug  7 00:00 postgresql-2017-08-06_000000.log
-rw-------. 1 postgres postgres 2.0G Aug  8 00:00 postgresql-2017-08-07_000000.log
-rw-------. 1 postgres postgres 1.9G Aug  9 00:00 postgresql-2017-08-08_000000.log
-rw-------. 1 postgres postgres 1.7G Aug 10 00:00 postgresql-2017-08-09_000000.log
-rw-------. 1 postgres postgres 2.1G Aug 11 00:00 postgresql-2017-08-10_000000.log
-rw-------. 1 postgres postgres 2.3G Aug 12 00:00 postgresql-2017-08-11_000000.log
-rw-------. 1 postgres postgres 2.6G Aug 12 23:59 postgresql-2017-08-12_000000.log
-rw-------. 1 postgres postgres 2.4G Aug 14 00:00 postgresql-2017-08-13_000000.log
-rw-------. 1 postgres postgres 2.6G Aug 15 00:00 postgresql-2017-08-14_000000.log
-rw-------. 1 postgres postgres 1.6G Aug 16 00:00 postgresql-2017-08-15_000000.log
-rw-------. 1 postgres postgres 1.8G Aug 17 00:00 postgresql-2017-08-16_000000.log
-rw-------. 1 postgres postgres 2.1G Aug 18 00:00 postgresql-2017-08-17_000000.log
-rw-------. 1 postgres postgres 2.0G Aug 19 00:00 postgresql-2017-08-18_000000.log
-rw-------. 1 postgres postgres 2.5G Aug 20 00:00 postgresql-2017-08-19_000000.log
-rw-------. 1 postgres postgres 2.3G Aug 21 00:00 postgresql-2017-08-20_000000.log
-rw-------. 1 postgres postgres 2.2G Aug 22 00:00 postgresql-2017-08-21_000000.log
-rw-------. 1 postgres postgres 2.1G Aug 23 00:00 postgresql-2017-08-22_000000.log
-rw-------. 1 postgres postgres 2.4G Aug 23 23:59 postgresql-2017-08-23_000000.log
-rw-------. 1 postgres postgres 2.3G Aug 25 00:00 postgresql-2017-08-24_000000.log
-rw-------. 1 postgres postgres 2.4G Aug 26 00:00 postgresql-2017-08-25_000000.log
-rw-------. 1 postgres postgres 3.0G Aug 27 00:00 postgresql-2017-08-26_000000.log
-rw-------. 1 postgres postgres 2.8G Aug 28 00:00 postgresql-2017-08-27_000000.log
-rw-------. 1 postgres postgres 2.8G Aug 29 00:00 postgresql-2017-08-28_000000.log
-rw-------. 1 postgres postgres 2.2G Aug 30 00:00 postgresql-2017-08-29_000000.log
-rw-------. 1 postgres postgres 2.1G Aug 31 00:00 postgresql-2017-08-30_000000.log
-rw-------. 1 postgres postgres 2.4G Aug 31 23:59 postgresql-2017-08-31_000000.log
-rw-------. 1 postgres postgres 2.3G Sep  2 00:00 postgresql-2017-09-01_000000.log
-rw-------. 1 postgres postgres 2.5G Sep  3 00:00 postgresql-2017-09-02_000000.log
-rw-------. 1 postgres postgres 2.8G Sep  4 00:00 postgresql-2017-09-03_000000.log
-rw-------. 1 postgres postgres 2.5G Sep  5 00:00 postgresql-2017-09-04_000000.log
-rw-------. 1 postgres postgres 1.8G Sep  6 00:00 postgresql-2017-09-05_000000.log
-rw-------. 1 postgres postgres 2.1G Sep  7 00:00 postgresql-2017-09-06_000000.log
-rw-------. 1 postgres postgres 2.6G Sep  8 00:00 postgresql-2017-09-07_000000.log
-rw-------. 1 postgres postgres 2.5G Sep  9 00:00 postgresql-2017-09-08_000000.log
-rw-------. 1 postgres postgres 2.6G Sep 10 00:00 postgresql-2017-09-09_000000.log
-rw-------. 1 postgres postgres 2.8G Sep 11 00:00 postgresql-2017-09-10_000000.log
-rw-------. 1 postgres postgres 2.8G Sep 12 00:00 postgresql-2017-09-11_000000.log
-rw-------. 1 postgres postgres 2.1G Sep 12 22:03 postgresql-2017-09-12_000000.log
-rw-------. 1 postgres postgres 265K Sep 12 23:55 postgresql-2017-09-12_225530.log
-rw-------. 1 postgres postgres 1.1G Sep 14 00:00 postgresql-2017-09-13_000000.log
-rw-------. 1 postgres postgres 1.2G Sep 14 23:59 postgresql-2017-09-14_000000.log
-rw-------. 1 postgres postgres 763M Sep 16 00:00 postgresql-2017-09-15_000000.log
-rw-------. 1 postgres postgres 769M Sep 17 00:00 postgresql-2017-09-16_000000.log
-rw-------. 1 postgres postgres 1.8G Sep 18 00:00 postgresql-2017-09-17_000000.log
-rw-------. 1 postgres postgres 2.5G Sep 19 00:00 postgresql-2017-09-18_000000.log
-rw-------. 1 postgres postgres 2.3G Sep 19 23:59 postgresql-2017-09-19_000000.log
-rw-------. 1 postgres postgres 2.2G Sep 20 23:59 postgresql-2017-09-20_000000.log
-rw-------. 1 postgres postgres 2.1G Sep 22 00:00 postgresql-2017-09-21_000000.log
-rw-------. 1 postgres postgres 2.0G Sep 22 23:59 postgresql-2017-09-22_000000.log
-rw-------. 1 postgres postgres 2.4G Sep 23 23:59 postgresql-2017-09-23_000000.log
-rw-------. 1 postgres postgres 2.6G Sep 25 00:00 postgresql-2017-09-24_000000.log
-rw-------. 1 postgres postgres 2.2G Sep 26 00:00 postgresql-2017-09-25_000000.log
-rw-------. 1 postgres postgres 1.4G Sep 26 21:28 postgresql-2017-09-26_000000.log
-rw-------. 1 postgres postgres 6.6K Sep 26 21:29 postgresql-2017-09-26_212911.log
-rw-------. 1 postgres postgres  899 Sep 26 21:30 postgresql-2017-09-26_213001.log
-rw-------. 1 postgres postgres  66M Sep 27 00:00 postgresql-2017-09-26_213302.log
-rw-------. 1 postgres postgres 1.8G Sep 27 23:59 postgresql-2017-09-27_000000.log
-rw-------. 1 postgres postgres 1.9G Sep 29 00:00 postgresql-2017-09-28_000000.log
-rw-------. 1 postgres postgres 1.8G Sep 29 23:59 postgresql-2017-09-29_000000.log
-rw-------. 1 postgres postgres 1.3G Oct  1 00:00 postgresql-2017-09-30_000000.log
-rw-------. 1 postgres postgres 1.4G Oct  2 00:00 postgresql-2017-10-01_000000.log
-rw-------. 1 postgres postgres 1.7G Oct  2 23:59 postgresql-2017-10-02_000000.log
-rw-------. 1 postgres postgres 1.7G Oct  3 23:59 postgresql-2017-10-03_000000.log
-rw-------. 1 postgres postgres 1.6G Oct  4 23:59 postgresql-2017-10-04_000000.log
-rw-------. 1 postgres postgres 1.8G Oct  5 23:59 postgresql-2017-10-05_000000.log
-rw-------. 1 postgres postgres 1.6G Oct  6 23:59 postgresql-2017-10-06_000000.log
-rw-------. 1 postgres postgres 1.3G Oct  8 00:00 postgresql-2017-10-07_000000.log
-rw-------. 1 postgres postgres 1.9G Oct  8 23:59 postgresql-2017-10-08_000000.log
-rw-------. 1 postgres postgres 1.7G Oct 10 00:00 postgresql-2017-10-09_000000.log
-rw-------. 1 postgres postgres 1.5G Oct 11 00:00 postgresql-2017-10-10_000000.log
-rw-------. 1 postgres postgres 1.4G Oct 11 23:59 postgresql-2017-10-11_000000.log
-rw-------. 1 postgres postgres 1.8G Oct 12 23:59 postgresql-2017-10-12_000000.log
-rw-------. 1 postgres postgres 1.9G Oct 13 23:59 postgresql-2017-10-13_000000.log
-rw-------. 1 postgres postgres 1.8G Oct 15 00:00 postgresql-2017-10-14_000000.log
-rw-------. 1 postgres postgres 1.8G Oct 16 00:00 postgresql-2017-10-15_000000.log
-rw-------. 1 postgres postgres 843M Oct 16 14:20 postgresql-2017-10-16_000000.log
-rwx------. 1 postgres postgres 3.3K Sep 12 22:55 startup.log

Step to move logs:

checking backup logs path:
bash-4.1$ cd  /pg_xlog/backup_logs
bash-4.1$ ls -l
total 0
bash-4.1$ cd ..
bash-4.1$ ls -l
total 20
drwxr-xr-x. 2 postgres postgres  4096 Oct 16 15:42 backup_logs
bash-4.1$
Script For Moving pg_log file
find /data/pg93/pg_log/ -type f -mtime +30 -print | xargs -I {} mv {} /tmp/old_log/
Move log file older than 10 days:
find /data/pg93/pg_log/ -type f -mtime +10 -print | xargs -I {} mv {} /tmp/old_log/
For Remove PostgreSQL pg_log
find /data/pg93/pg_log/ -type f -mtime +30 -print | xargs -I {} rm {}

Note that there are spaces between rm, {}, and \;

Compressing older than one month logfile:
check the total size of the log file
bash-4.1$ du -ch /data/emut16_data/pg_log/*.log | grep total
270G    total
check the backup mount poit size :
$ df -h
Moving one month older file /data:
find /data/pg_log/ -type f -mtime +30 -print | xargs -I {} mv {} /tmp/old_log/
zip and move older than one month log file:
find /data/pg_log/ -mtime +30 -exec zip older_log_$(date +%F).zip '{}' + && mv older_log_$(date +%F).zip  /backup/
Remove pg_xlog after moving zip file:
find /data/pg_log -type f -mtime +30 -print | xargs -I {} rm {}


Explanation
  • The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
  • The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +30, it will find files older than 30 days.
  • The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.

Comments

Popular posts from this blog

rman interview and scenario based questions and answer and also rman(backup and recovery) discussions panel

How to Get Table Size, Database Size, Indexes Size, schema Size, Tablespace Size, column Size in PostgreSQL Database

How to find the server is whether standby (slave) or primary(master) in Postgresql replication ?

7 Steps to configure BDR replication in postgresql

PostgreSQL Introduction