Today, I encountered two simple errors when I was trying to to run a simple sql file on my Sybase database. Though the errors are not difficult ones. But it costed some time to find the causes of errors during my little maintenance window...
The task I wanted to do is to run the a sql file t1.sql and it just create a table on the database db1...simple enough.
test_host$ cat t1.sql
IF OBJECT_ID('test1') IS NOT NULL
PRINT '<<< Table test1 already has existed>>>'
ELSE
PRINT '<<< Creating table test1 >>>'
go
create table test1(
col1 int,
col2 varchar(200))
LOCK DATAROWS
go
I tried run it with below command:
isql -Uuser1 -SSYB_SERVER1 -Ppassword1 -i t1.sql -o t1.out
test_host$ ls -ltr t*
-rwxr-x--- 1 sybase sybase 214 Apr 14 11:54 t1.sql
-rw-r----- 1 sybase sybase 0 Apr 14 11:56 t1.out
Here I wonder why the t1.out would be zero bytes. isql didnt tell whether the command is successful, nor failed....
Then I had spent some time to figure out the reasons. And everything comes clear until I run the vi command with the t1.sql file.
test_host$view t1.sql
IF OBJECT_ID('test1') IS NOT NULL^M
PRINT '<<< Table test1 already has existed>>>'^M
ELSE^M
PRINT '<<< Creating table test1 >>>'^M
go^M
^M
create table test1(^M
col1 int,^M
col2 varchar(200))^M
LOCK DATAROWS^M
go^M
^M
~
The extra ^M character at the end of line means the t1.sql in dos-style, and isql in unix cannot run with this style.
So, the next thing is simple, convert the file to unix format and run it again.
test_host$ dos2unix t1.sql >t2.sql
could not open /dev/kbd to get keyboard type US keyboard assumed
could not get keyboard type US keyboard assumed
test_host$ ls -ltr t*
total 208
-rwxr-x--- 1 sybase sybase 214 Apr 14 11:54 t1.sql
-rw-r----- 1 sybase sybase 0 Apr 14 11:56 t1.out
-rw-r----- 1 sybase sybase 202 Apr 14 11:58 t2.sql
test_host$ isql -Uuser1 -SSYB_SERVER1 -Ppassword1 -i t2.sql -o t2.out
test_host$ ls -la t*
-rw-r----- 1 sybase sybase 0 Apr 14 11:56 t.out
-rwxr-x--- 1 sybase sybase 214 Apr 14 11:54 t1.sql
-rw-r----- 1 sybase sybase 29 Apr 14 12:31 t2.out
-rw-r----- 1 sybase sybase 202 Apr 14 11:58 t2.sql
test_host$ cat t2.out
<<< Creating table test1 >>>
The t2.sql can be run successfully. Here I have learnt 2 lessons: 1) Don't trust the auto file mode in ftp client tool, it sometimes cannot choose conversion correctly.
2) Some tools, e.g. cat and more, cannot show the special characters in files. To view special characters in a file, user vi or view command instead.
Blog > Showing posts with label Sybase. Show all posts
Showing posts with label Sybase. Show all posts
No Comments
Filed under:
Sybase
It is not easy to find document to provide simple and complete steps on the internet about Sybase backup and restore(because Sybase is not actively developed now? ). Therefore I wrote my own below as a quick reference for backing up and restoring Sybase database. This reference should be working for ASE 12.5 to 15.x.
Backup database
1. Run isql to connect to the database instance wtih sa privilege
isql -Usa -P -S
2. Run the command
sp_helpdb
go
to check for the database list.
3. Force a checkpoint on the database to be backed up
use
go
checkpoint
go
4. Run the backup command
dump database to '/_.dmp'
go
Restore Master database
1. Shutdown the database and start the database again in single user mode
cd $SYBASE/ASE-12.5/install
startserver RUN_DBINSTANCE -m
2. Run isql to connect to the database instance wtih sa privilege
isql -Usa -SSYSBASE_INSTANCE
3. In the single User Mode, Load the database by
load database master from ''
4. After restoring the master database, shutdown the database by
shutdown SYB_BACKUP
go
shutdown with nowait
go
6. Start the sybase database in Multi-user mode by
cd /opt/sybase/ASE-12.5/install
startserver -f
startserver
Restore User databases
1. Run isql to connect to the database instance wtih sa privilege
isql -Usa -P -S
3. Restore the database
load database from ''
go
4. Put the database back online
online database
go
Note that if you do not start the server in single user mode during restoration, the database may be locked by other users and you may encounter the following error:
Msg 3101, Level 16, State 1:
Server 'local', Line 1:
Database in use. A user with System Administrator (SA) role must haveexclusive use of database to run load. To fix this problem, you need to find out who is locking the database using the following commands:
use master
go
sp_who
After find out who is locking the database, you can use kill command to terminate user sessions:
kill
go
Backup database
1. Run isql to connect to the database instance wtih sa privilege
isql -Usa -P
2. Run the command
sp_helpdb
go
to check for the database list.
3. Force a checkpoint on the database to be backed up
use
go
checkpoint
go
4. Run the backup command
dump database to '
go
Restore Master database
1. Shutdown the database and start the database again in single user mode
cd $SYBASE/ASE-12.5/install
startserver RUN_DBINSTANCE -m
2. Run isql to connect to the database instance wtih sa privilege
isql -Usa -SSYSBASE_INSTANCE
3. In the single User Mode, Load the database by
load database master from '
4. After restoring the master database, shutdown the database by
shutdown SYB_BACKUP
go
shutdown with nowait
go
6. Start the sybase database in Multi-user mode by
cd /opt/sybase/ASE-12.5/install
startserver -f
startserver
Restore User databases
1. Run isql to connect to the database instance wtih sa privilege
isql -Usa -P
3. Restore the database
load database from '
go
4. Put the database back online
online database
go
Note that if you do not start the server in single user mode during restoration, the database may be locked by other users and you may encounter the following error:
Msg 3101, Level 16, State 1:
Server 'local', Line 1:
Database in use. A user with System Administrator (SA) role must haveexclusive use of database to run load. To fix this problem, you need to find out who is locking the database using the following commands:
use master
go
sp_who
After find out who is locking the database, you can use kill command to terminate user sessions:
kill
go
Filed under:
Sybase