How do I run a cronjob from cron.d? - postgresql

I created a file called 'test_purge' in /etc/cron.d that contains:
00 04 * * * postgres /var/lib/pgsql/test.sh
This is supposed to run test.sh at 4 AM everyday, right?
test.sh has:
#!/bin/bash
psql -d kettle -c "TRUNCATE TABLE test;"
Am I supposed to do anything else to make this run? Do I add 'test_purge' which is in cron.d to my crontab or anything? I tried to run it last night but it did not work.

Your cron file and scripts seems correct. There some things to test though.
First, make sure you script has execution permission (specially for postgres user):
$ chmod +x /var/lib/pgsql/test.sh
I'd also set it to postgres user as owner:
$ chown postgres:postgres /var/lib/pgsql/test.sh
Now. Make sure the psql binary is mapped in your PATH environment, for a cron file you have to set the PATH into the file:
PATH=/usr/lib/pgsql/bin:/usr/local/bin/:/usr/bin/:...
00 04 * * * postgres /var/lib/pgsql/test.sh
At last, make sure you can see the script output somehow, either by checking the user mail or by redirecting the outputs to a file, e.g.:
00 04 * * * postgres /var/lib/pgsql/test.sh > /tmp/test.log 2>&1

Related

Crontab doesn't backup postgre DB in docker, but if i run script by hand it work prorely

I'm trying to set up automatic backup for postgre database. Postgre running in docker, so my script for backup is:
docker-compose exec postgres -U user database_name | gzip > "/var/server/my_service/data/backup-db/db_backup.sql.gz"
And its working fine, if I run it manually. I wrote the following job for the crontab (every 5 minutes just for testing):
*/5 * * * * cd /var/server/my_service && sh /var/server/my_service/data/backup/backup_script
This command also working great, if i run it manually it create valid DB backups that i can use.
But crontab just create empty archive, without any data. I just cant understand why.
My guess is that the output stream that catches the gzip is normally generated in manual mode, but completely empty when the crontab trying to run command
I thought there were problems with access rights and put the in the root crontab but it didn't help
UPD:
so... problem in backup_script, error in logs says the input device is not a TTY
I tried google it and add -T, but it didn't help as well
Update your /var/server/my_service/data/backup/backup_script with the following:
Prefix the first 3 line in your script:
#!/bin/bash
source ~/.bash_profile
cd /var/server/my_service
#
# rest of your script
#
Your crontab line should be (At 04:44 on every day-of-month):
44 4 */1 * * /var/server/my_service/data/backup/backup_script

automatic backup in postgresql creates the empty backup

I want to automate backup of PostgreSQL database using crontab in UNIX. I have tried but it will create 0 bytes backup.
My crontab entry is:
24 * * * * /home/desktop/myscript.sh
and my sh file contains the following code:
pg_dump -U PostgreSQL -d test > b.backup
It will create the file but the file is empty. Is there any solution? Is there any way to solve this question?
Don't assume that any environment variables are set in a cron job; be explicit:
/full/path/to/pg_dump -U postgres -d test > /full/path/to/b.backup
Look for mail in your inbox for failure reports from cron.
You must specify full path to pg_dump
#!/bin/bash
BKPDATE=$(date +%d.%m.%Y-%H:%M:%S)
cd /var/lib/pgsql/12/backups
/usr/pgsql-12/bin/pg_dump -Fc dl_db > DBNAME_$BKPDATE.dmp --verbose 2> LOG_$BKPDATE.log
or you must add PostgreSQL's bin directory to the path like below:
vi /var/lib/pgsql/.pgsql_profile
export PATH=$PATH:/usr/pgsql-12/bin

Create a file as one user and edit it as another user

Im writing a shell to automate a process this shell will be run as root or as another user but not as the postgres user (meaning the user will just run the script)
What i did as a postgres user while testing was
touch /var/lib/postgresql/10/main/recovery.conf
sudo nano recovery.conf (wrote some content )
ctrl +O
However whenever I try to do this by using the following lines in my shell as as another user (including root)
sudo -H -u postgres bash -c "touch /var/lib/postgresql/10/main/recovery.conf"
The file is created as postgres user which is what i intended, and then i run
sudo -H -u postgres bash -c echo "content" > /var/lib/postgresql/10/main/recovery.conf
and get a
-bash: /var/lib/postgresql/10/main/recovery.conf: Permission denied
How come I can create the file but not put content on it?
I already tried giving 775 and 777 permisions using chmod
chmod 775 recovery.conf
as the psotgres user and i get
-rwxrwxr-x 1 postgres postgres 133 May 11 22:11 recovery.conf
to this file as a postgres user and still the error persists so im confused about whats going on
At the second line where you try to put the "echo" command, please use:
/var/lib/postgresql/10/main/recovery.conf
With a / at the beginning

Crontab: Running a Single Command Inline

I want to run a single command from a crontab. Rather than create a bash file for just one command, is it possible to run a crontab somehow like this:
# Everyday at 3 am, restart the server.
0 3 * * * 'sudo shutdown -r now'
Instead of:
0 3 * * * /usr/local/bin/restart.bash
Can something like that be done?
Yes, you can type the command in-line, but it must be typed the same as if you were on the command line. You do not type single quotes ' on the command line, so you should not type them in the crontab either.
Finally, a command which requires root access must be run in the root crontab. (i.e. edit using sudo crontab -e instead of plain crontab -e, or log in as root using su - before typing crontab -e)
So in your example, you should type this in the crontab:
# Everyday at 3 am, restart the server.
0 3 * * * shutdown -r now
(sudo has no effect, removed single quotes)

issues backing up postgres databases in cron

I am trying to backup postgres databases. I am running a cron job to do so. Issue is that postgres runs under user postgres and I dont think I can run a cron job under ubuntu user. I tried to create a cron job under postgres user and that also did not work. My script, if login as postgres user works just fine.
Here is my script
#!/bin/bash
# Location to place backups.
backup_dir="/home/postgres-backup/"
#String to append to the name of the backup files
backup_date=`date +%d-%m-%Y`
#Numbers of days you want to keep copie of your databases
number_of_days=30
databases=`psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'`
for i in $databases; do
if [ "$i" != "template0" ] && [ "$i" != "template1" ]; then
echo Dumping $i to $backup_dir$i\_$backup_date
pg_dump -Fc $i > $backup_dir$i\_$backup_date
fi
done
find $backup_dir -type f -prune -mtime +$number_of_days -exec rm -f {} \;
if I do
sudo su - postgres
I see
-rwx--x--x 1 postgres postgres 570 Jan 12 20:48 backup_all_db.sh
and when I do
./backup_all_db.sh
it gets backed up in /home/postgres-backup/
however with cronjob its not working, regardless if I add the cron job under postgres or under ubuntu.
here is my cronjob
0,30 * * * * /var/lib/pgsql/backup_all_db.sh 1> /dev/null 2> /home/cron.err
Will appreciate any help
Enable user to run cron jobs
If the /etc/cron.allow file exists, then users must be listed in it in order to be allowed to run the crontab command. If the /etc/cron.allow file does not exist but the /etc/cron.deny file does, then users must not be listed in the /etc/cron.deny file in order to run crontab.
In the case where neither file exists, the default on current Ubuntu (and Debian, but not some other Linux and UNIX systems) is to allow all users to run jobs with crontab.
Add cron jobs
Use this command to add a cron job for the current user:
crontab -e
Use this command to add a cron job for a specified user (permissions are required):
crontab -u <user> -e
Additional reading
man 5 crontab
Crontab in Ubuntu: https://help.ubuntu.com/community/CronHowto