create a backup of database every day using Cron. [putty] - backup

I have this code which created a backup of my database.
pg_dump -U dbadmin -h 127.0.0.1 123telcom -f dbbackup
Now i want to create a backup every night.
Is there a way u can execute this code with crontab?
0 3 * * * pg_dump -U dbadmin -h 127.0.0.1 123telcom -f dbbackup
I'm new to putty so if anyone could help me a little that would be great.

I suspect that you have fallen foul of cron's PATH set up.
If you look in /etc/crontab, it will define a PATH for itself and you will probably have a different PATH set up for your login.
Create your script with the first 2 lines:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
where the PATH includes whatever is set up in your environment and ensure that the script is executable.
To test what is going on try this script:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
echo $PATH >> /home/yourhome/cron.txt
create an entry in /etc/crontab:
* * * * * root /home/yourhome/yourshell.sh
tell cron about the changes by using sudo crontab -e and then just save it and exit (often Ctrl O and Ctrl X if using nano editor) or I think that you can just kill the cron process and it will re-spawn.
Then check the cron.txt file to see what it is using for PATH.
PS Don't forget to remove this script from the crontab afterwards

Related

Unable to run a postgresql script from bash

I am learning the shell language. I have creating a shell script whose function is to login into the DB and run a .sql file. Following are the contents of the script -
#!/bin/bash
set -x
echo "Login to postgres user for autoqa_rpt_production"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT
echo "Running SQL Dump - auto_qa_db_sync"
\\i auto_qa_db_sync.sql
After running the above script, I get the following error
./autoqa_script.sh: 39: ./autoqa_script.sh: /i: not found
Following one article, I tried reversing the slash but it didn't worked.
I don't understand why this is happening. Because when I try manually running the sql file, it works properly. Can anyone help?
#!/bin/bash
set -x
echo "Login to postgres user for autoqa_rpt_production and run script"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT -f auto_qa_db_sync.sql
The lines you put in a shell script are (moreless, let's say so for now) equivalent to what you would put right to the Bash prompt (the one ending with '$' or '#' if you're a root). When you execute a script (a list of commands), one command will be run after the previous terminates.
What you wanted to do is to run the client and issue a "\i ./autoqa_script.sh" comand in it.
What you did was to run the client, and after the client terminated, issue that command in Bash.
You should read about Bash pipelines - these are the way to run programs and input text inside them. Following your original idea to solving the problem, you'd write something like:
echo '\i auto_qa_db_sync.sql' | $DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT
Hope that helps to understand.

Open up a sql file and running it in PostgreSQL is having an issue with the path because of space in the folder name

I'm using command line to run the following script:
C:\Progra~1\pgAdmin III\1.16\psql -d [tablename] -h [servername] -p 5432 -U postgres -f C:\test\query.sql
But the issue comes with the folder pgAdmin III that I want to run the query in since it has a space in the name. When I changed the actual folder name to pgAdminIII and updated the script it will run the script just fine. I was wondering how I can run this script without physically modifying the folder name (i.e. keep it as pgAdmin III)?
Put double qoutes around a path with spaces in it:
"C:\Progra~1\pgAdmin III\1.16\psql" -d [tablename] ...

Execute multiple files at one go using a single bat file

I am using a batch file to execute multiple sql files.
So I have created a bat file like:
osql -S ServerName -U user -P password -d DBTest -i C:\SQLFILES\Test1.sql
pause
The above code executes a single file Test1.sql and if I need to execute the next file I have to again modify the bat file and change the file name.I am having 10 such sql files and I want to execute them one after another. Is there any way to do this at one go?
Well, what you could definitely do is give your BAT file a parameter (so you don't have to constantly change the BAT file contents...):
ExecSQL.bat:
osql -S ServerName -U user -P password -d DBTest -i %1
pause
and then you can call this batch file like this:
c:\> ExecSQL C:\SQLFILES\Test1.sql
and then
c:\> ExecSQL C:\SQLFILES\Test2.sql
and so forth

rsync: polling for new files

I've got:
$ rsync -azv zope#myserver:/smb/Data/*/*/* ~/rsynced_samples/
And I want it to run forever, syncing any new file as soon as it appears on myserver:
(specifying a poll interval, such as 4 seconds would be an ok comprise)
Instead of rsync you can use inotifywait which use kernel specific file changes triggers.
This script (inotify.sh) can you give an idea:
#!/bin/bash
directory=$1
inotifywait -q -m --format '%f' -e modify -e move -e create -e delete ${directory} | while read line
do
echo "doing something with: $line";
# for example:
# cp $line to <somewhere>
You can invoke this script specifying the "monitor" directory, in this way
./inotify.sh ~/Desktop/
The $line variable contains the full file path.
If you want to limit to only newly created files you can use on the flag "-e create"
Use cron to set up a check based on your time interval (say, every minute, perhaps?) . This link should help: http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
Note that a cron tab is set up on your machine side, not in your bash script
also useful: http://benr75.com/pages/using_crontab_mac_os_x_unix_linux
and here is a code example:
1) crontab -e // this opens up your current crontab or creates one if it does not exist
2) enter: * * * * * file.sh >> log.txt // this would pipe the output of your file to a log file and run it every minute.
hope that helps

sqlcmd runs script but script does not affect database

So we have a .bat file that runs SQL scripts, e.g.
#ECHO --- 03_Case6395_Publication.sql --- >> dbupt.log
sqlcmd -U %1 -P %2 -S %3 -d %4 -i 03_Case6395_Publication.sql -k -b >> dbupt.log
IF ERRORLEVEL 1 GOTO ErrorTag
The script runs and gives no errors, but the script doesn't actually affect the database. In the example above, here is what is being run:
IF NOT EXISTS (SELECT 1 FROM [dbo].[syscolumns] WHERE [NAME] = N'MandatoryInList' AND [ID] = object_id(N'Pub_Type'))
BEGIN
ALTER TABLE [dbo].[Pub_Type] ADD [MandatoryInList] bit NULL CONSTRAINT [DF_PubType_MandatoryInList] DEFAULT (0)
END
ELSE
BEGIN
ALTER TABLE [dbo].[Pub_Type] ALTER COLUMN [MandatoryInList] bit NULL
END
GO
The script is pretty simple and you would expect it to add a column called MandatoryInList, however it doesn't. The odd thing is there are no syntax errors in the script and when I run it from SQL Server Management Studio, it runs fine. There's no issues with permissions or which user is running the script because there are other scripts that run fine in the batch file.
I should also mention that this runs from a GUI by creating a new process and running it but not showing the command window. But regardless, I've been running the tool like this for months without any issues.
Any ideas?
So it works from Management Studio but not from your batch file that calls sqlcmd. Strange...
I don't know what could cause this, but here are some ideas:
If you run the same "sqlcmd" command manually from the command prompt, rather than from your batch file, does it work?
If you run the batch file from the command prompt, rather than invoking it from the GUI app, does it work?
What's it writing to your dbupt.log file? Does it have the header "--- 03_Case6395_Publication.sql --- ", and it's empty after that?
Can you add some PRINT statements to the script, so you can see whether it's executing the IF or the ELSE part?
Can you try redirecting the error stream to your log file as well, using the "2>&1" operator? E.g.:
sqlcmd -U %1 -P %2 -S %3 -d %4 -i 03_Case6395_Publication.sql -k -b >> dbupt.log 2>&1
Can you try changing "IF ERRORLEVEL 1" to "IF NOT ERRORLEVEL 0"?
Have you tried using SQL Profiler to see what SQL is actually executing? Does anything show up?