Search

My notebook

Category

DataBase

Backup script for SQL express server

Create sql_backup.bat file in folder c:\scripts\sql_backup

(Scripts in folder c:\script\sql_backup\ backups in folder d:\sqlbackup\)

File text:

sqlcmd -i c:\scripts\sql_backup\sql_backup.sql >  D:\SQLBACKUPS\reports\backuplog.txt

Create sql_backup.sql files in folder c:\scripts\sql_backup

File text:

DECLARE @basePath varchar(300); 
SET @basePath = N'D:\SQLBACKUPS\'; 

DECLARE @db sysname;
DECLARE user_db_cursor CURSOR FOR 
 SELECT name FROM sys.databases WHERE name NOT IN 
('master', 'model', 'msdb', 'tempdb') 
OPEN user_db_cursor 
FETCH NEXT FROM user_db_cursor 
INTO @db 

WHILE @@FETCH_STATUS = 0 
BEGIN 
 DECLARE @backupPath varchar(1000); 
 SET @backupPath = @basePath + @db + '\'; 

 DECLARE @backupName varchar(1000); 
 DECLARE @time datetime2; 
 SET @time = SYSDATETIME(); 
 SET @backupName = @db + '_backup_' + 
REPLACE(REPLACE(REPLACE(CONVERT(varchar, @time, 20),'-','_'),':',''),' ','_') 
 + '_' + CAST(DATEPART(NANOSECOND, @time)/100 as varchar) 

 --Create sub-directory 
 EXECUTE master.dbo.xp_create_subdir @backupPath 
 
 DECLARE @backupFileName varchar(1000); 
 SET @backupFileName = @backupPath + @backupName + '.bak'; 
 
 --Backup database 
 BACKUP DATABASE @db TO DISK=@backupFileName 
 WITH NOFORMAT, NOINIT, NAME=@backupName , 
 SKIP, REWIND, NOUNLOAD, STATS=10 
 FETCH NEXT FROM user_db_cursor 
INTO @db 
END
CLOSE user_db_cursor 
DEALLOCATE user_db_cursor

Delete old backups:

Create

sqlcmd -i D:\SQLBACKUPS\Script\deleteold.sql > D:\SQLBACKUPS\reports\delete.txt
DECLARE @basePath varchar(300); 
SET @basePath = N'D:\SQLBACKUPS\'; 

DECLARE @time datetime; 
SET @time = DATEADD(DAY,-3, GETDATE() ); 

EXECUTE master.dbo.xp_delete_file 0,@basePath,N'bak',@time,1 

Mysql backup

Create folder

mkdir /etc/scripts

edit backup files

vim backup_mysql.sh
#!/bin/bash
 
USER="dbuser"
PASSWORD="dbpassword"
OUTPUT="/opt/backup_mysql/"
 
databases=`mysql --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
 
for db in $databases; do
 if [[ "$db" != "information_schema" ]] && [[ "$db" != _* ]] ; then
 echo "Dumping database: $db"
 mysqldump --force --opt --user=$USER --password=$PASSWORD --databases $db > $OUTPUT/`date +%Y%m%d`.$db.sql
 gzip $OUTPUT/`date +%Y%m%d`.$db.sql
 fi
done

Delete old backups:

#!/bin/bash
find /opt/backup_mysql/* -mtime +6 -exec rm {} \;

Change permission:

chmod 700 backup_mysql.sh
Add task
crontab -e
#delete old backups
0 2 * * * SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin /etc/scripts/delete_old_backups.sh > /dev/null 2>&1
#new backup
0 3 * * * SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin /etc/scripts/backup_mysql.sh > /dev/null 2>&1

Install LAMP Server (Apache, MySQL, PHP) On RHEL, CentOS, Scientific Linux 6.5/6.4

LAMP is a combination of operating system and open-source software stack. The acronym LAMP is derived from first letters of Linux, Apache HTTP Server, MySQL database, and PHP/Perl/Python.

In this tutorial let us see how to setup LAMP server on RHEL/CentOS/Scientific Linux 6.x. Here x stands for version such as 6.1, 6.2, 6.3, 6.4, 6.5 etc.

My testbox hostname and IP address are server.unixmen.local and 192.168.1.101/24, respectively.

Install Apache

Apache is an open-source multi-platform web server. It provides a full range of web server features including CGI, SSL and virtual domains.

To install Apache, enter the following command from your terminal:

# yum install httpd -y

Start the Apache service and let it to start automatically on every reboot:

# service httpd start
# chkconfig httpd on

Allow Apache server default port 80 through your firewall/router if you want to connect from remote systems. To do that, edit file /etc/sysconfig/iptables,

# vi /etc/sysconfig/iptables

Add the following lines.

[...]
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEP
[...]

Restart iptables:

# service iptables restart

Test Apache:

Open your web browser and navigate to http://localhost/ or http://server-ip-address/.

Apache HTTP Server Test Page powered by CentOS - Mozilla Firefox_001

Install MySQL

MySQL is an enterprise class, open source, world’s second most used database. MySQL is a popular choice of database for use in web applications, and is a central component of the widely used LAMP open source web application software stack.

To install MySQL, enter the following command:

# yum install mysql mysql-server -y

Start the MySQL service and make to start automatically on every reboot.

# service mysqld start
# chkconfig mysqld on

Setup MySQL root password

By default, mysql root user doesn’t has password. To secure mysql, we have to setup mysql root user password.

# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):     ## Press Enter ## 
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n]     ## Press Enter ##
New password:                ## Enter new password ##
Re-enter new password:       ## Re-enter new password ##
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]     ## Press Enter ##
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]     ## Press Enter ## 
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]     ## Press Enter ##
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]     ## Press Enter ##
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Install PHP

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely used open-source general purpose scripting language that is especially suited for web development and can be embedded into HTML.

Install PHP with following command:

# yum install php -y

Test PHP

Create a sample “testphp.php” file in Apache document root folder and append the lines as shown below:

# vi /var/www/html/testphp.php

Add the following lines.


Restart httpd service:

# service httpd restart

Navigate to http://server-ip-address/testphp.php. It will display all the details about php such as version, build date and commands etc.

If you wanna to get MySQL support in your PHP, you should install “php-mysql” package. If you want to install all php modules just you use the command “yum install php*”

[root@server ~]# yum install php-mysql -y

Now open the phptest.php file in your browser using http://ip-address/testphp.php or http://domain-name/testphp.php. Scroll down and you will see the mysql module will be presented there.

phpinfo() - Mozilla Firefox_002Install phpMyAdmin

phpMyAdmin is a free open source web interface tool, used to manage your MySQL databases. By default phpMyAdmin is not found in CentOS official repositories. So let us install it using EPEL repository.

To install EPEL repository, follow the below link:

Install EPEL Repository On RHEL/CentOS/Scientific Linux 6

Now install phpMyAdmin

# yum install phpmyadmin -y

Configure phpMyAdmin

Edit the phpmyadmin.conf file.

# vi /etc/httpd/conf.d/phpMyAdmin.conf

Find and comment the whole /<Directory> section as shown below:

[...]
Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin

#<Directory /usr/share/phpMyAdmin/>
#   <IfModule mod_authz_core.c>
#     # Apache 2.4
#     Require local
#   </IfModule>
#   <IfModule !mod_authz_core.c>
#     # Apache 2.2
#     Order Deny,Allow
#     Deny from All
#     Allow from 127.0.0.1
#     Allow from ::1
#   </IfModule>
#</Directory>
[...]

Open “config.inc.php” file and change from “cookie” to “http” to change the authentication in phpMyAdmin:

# cp /usr/share/phpMyAdmin/config.sample.inc.php /usr/share/phpMyAdmin/config.inc.php 
# vi /usr/share/phpMyAdmin/config.inc.php

Change cookie to http.

[...] 
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'http';
[...]

Restart the Apache service:

# service httpd restart

Now you can access the phpmyadmin console by navigating to http://server-ip-address/phpmyadmin/ from your browser.

Enter your MySQL username and password which you have given in previous steps. In my case its “root” and “centos”.

phpMyAdmin - Mozilla Firefox_003Now you will be redirected to the phpmyadmin dashboard.page as shown below.

192.168.1.101 - localhost | phpMyAdmin 4.1.2 - Mozilla Firefox_004

Now you will able to manage your MariaDB databases from phpMyAdmin web interface.

That’s it. Your LAMP server is up and ready to use.

Original post – link

Take database offline and online

ALTER DATABASE database-name SET OFFLINE
ALTER DATABASE database-name SET ONLINE
ALTER DATABASE database-name SET OFFLINE WITH NO_WAIT

Restore MSSQL from backup (CLI)

USE [master]
RESTORE DATABASE [DB4] 
 FROM DISK = N'D:\Backup\Backup_file.bak' WITH FILE = 1, 
MOVE N'VeeamBackup1' TO N'c:\Program Files\Microsoft SQL Server\MSSQL11.VEEAMSQL2012\MSSQL\DATA\DB4.mdf', 
MOVE N'VeeamBackup1_log' TO N'c:\Program Files\Microsoft SQL Server\MSSQL11.VEEAMSQL2012\MSSQL\DATA\DB4_log.ldf', NOUNLOAD, STATS = 5

GO

Forgot SYS password in oracle 11g

Log on to your Windows server as a member of the Administrators group or a member of the ORA_DBA group.
Try the below steps

sqlplus /nolog
connect / as sysdba
Once connected, you can change the SYS password to something you know:
ALTER USER sys IDENTIFIED BY new_password;

MySQL create user

Localhost:

grant all privileges on mydb.* to myuser@localhost identified by 'mypasswd';

From anywhere:

grant all privileges on mydb.* to myuser@'%' identified by 'mypasswd';

 

Как создать нового пользователя и настроить права доступа в MySQL:

Начнем с создания нового пользователя из консоли MySQL:

CREATE USER 'My_User'@'localhost' IDENTIFIED BY 'password';

Предоставить пользователю доступ к информации, которая ему потребуется.

GRANT ALL PRIVILEGES ON * . *  TO 'My_User'@'localhost';

Обновить все права доступа:

FLUSH PRIVILEGES;

PostgreSQL – List All Tables and Describe Tables

Connect to the database:
psql [-U username] [-h hostname] database-name

To list all databases, type either one of the following:
\l
\list

To list tables in a current database, type:
\dt

To describe a table, type:
\d <table-name>

 

From http://onewebsql.com/

DB2 – List All Tables and Describe Tables

Connect to the database:
db2 connect to <database-name>

List all tables:
db2 list tables for all

To list all tables in selected schema, use:
db2 list tables for schema <schema-name>

To describe a table, type:
db2 describe table <table-schema.table-name>

 

From http://onewebsql.com/

Blog at WordPress.com.

Up ↑