Backing up and Restoring databases

Backing up and restoring databases is a crucial aspect of database management, ensuring data availability and integrity in case of failures, data corruption, or accidental deletions. The process and tools used for backup and restoration depend on the database management system (DBMS) in use.

MySQL
Backup


Using mysqldump:
mysqldump -u [username] -p [database_name] > backup.sql

  • -u [username]: Specifies the MySQL username.
  • -p: Prompts for the password.
  • [database_name]: The name of the database to back up.
  • > backup.sql: Redirects the output to a file named backup.sql.

  • Example:

    mysqldump -u root -p mydatabase > backup.sql


    Restore
    Using mysql:

    mysql -u [username] -p [database_name] < backup.sql

    Example:
    mysql -u root -p mydatabase < backup.sql


    PostgreSQL
    Backup

    Using pg_dump:
    pg_dump -U [username] -d [database_name] -F c -b -v -f backup.dump

  • -U [username]: Specifies the PostgreSQL username.
  • -d [database_name]: The name of the database to back up.
  • -F c: Specifies the format (custom).
  • -b: Includes large objects.
  • -v: Verbose mode.
  • -f backup.dump: The output file

  • Example:
    pg_dump -U postgres -d mydatabase -F c -b -v -f backup.dump

    Restore
    Using pg_restore:
    pg_restore -U [username] -d [database_name] -v backup.dump

    Example:
    pg_restore -U postgres -d mydatabase -v backup.dump


    SQL Server
    Backup

    Using T-SQL:
    BACKUP DATABASE [database_name] TO DISK = 'C:\path\to\backup.bak'
    Example:
    BACKUP DATABASE [mydatabase] TO DISK = 'C:\backups\mydatabase.bak'


    You can run this T-SQL command using SQL Server Management Studio (SSMS) or any SQL Server client tool.
    Restore
    Using T-SQL:

    RESTORE DATABASE [database_name] FROM DISK = 'C:\path\to\backup.bak'

    Example:
    RESTORE DATABASE [mydatabase] FROM DISK = 'C:\backups\mydatabase.bak'

    Oracle
    Backing up and restoring databases in Oracle can be done using various methods, including the Oracle Recovery Manager (RMAN), Data Pump, and SQL*Plus. Here, I'll focus on RMAN , which is the most commonly used tool.

    Using RMAN

    Backup

    Full Database Backup:


    1.Connect to RMAN:

    rman target /

    2.Run the backup command:
    BACKUP DATABASE;

    Restore

    1.Connect to RMAN:
    rman target /

    2.Restore and recover the database:
    SHUTDOWN IMMEDIATE;
    STARTUP MOUNT;
    RESTORE DATABASE;
    RECOVER DATABASE;
    ALTER DATABASE OPEN;

    Comments

    Popular posts from this blog

    KTU DBMS LAB CSL 333 BTech S5 - Dr Binu V P

    Procedures in PL/SQL

    Cursors in PL/SQL