Advertisement
QuangDepTrai

Move AuditLog to Another Database by Create Date

Aug 19th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.35 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ## GET time to query data
  4. move_time=`date --date="6 months ago" "+%Y-%m-%d %T"`
  5. echo ${move_time}
  6.  
  7. ## COPY data auditlog
  8. echo "Copy data auditlog"
  9. psql wms_local -c \
  10. "\copy (SELECT id,name,model_id,res_id,user_id,method,http_session_id,http_request_id,log_type,create_uid,create_date,write_uid,write_date FROM auditlog_log WHERE create_date <= '${move_time}') TO STDOUT" | \
  11. psql archived_wms -c "\copy auditlog_log(id,name,model_id,res_id,user_id,method,http_session_id,http_request_id,log_type,create_uid,create_date,write_uid,write_date) FROM STDIN"
  12.  
  13.  
  14. ## COPY data auditlog_line
  15. echo "\nCopy data auditlog_line"
  16. psql wms_local -c \
  17. "\copy (SELECT id,field_id,log_id,old_value,new_value,old_value_text,new_value_text,create_uid,create_date,write_uid,write_date FROM auditlog_log_line WHERE log_id in (SELECT id FROM auditlog_log WHERE create_date <= '${move_time}'$
  18. psql archived_wms -c "\copy auditlog_log_line(id,field_id,log_id,old_value,new_value,old_value_text,new_value_text,create_uid,create_date,write_uid,write_date) FROM STDIN"
  19.  
  20.  
  21. ### Remove OLD record
  22. echo "\nRemoving data auditlog_line"
  23. psql wms_local -c "DELETE FROM auditlog_log_line WHERE log_id in (SELECT id FROM auditlog_log WHERE create_date <= '${move_time}')"
  24.  
  25. echo "\nRemoving data auditlog"
  26. psql wms_local -c "DELETE FROM auditlog_log WHERE create_date <= '${move_time}'"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement