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.
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
.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 fileExample:
pg_dump -U postgres -d mydatabase -F c -b -v -f backup.dump
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:
2.Run the backup command:
Full Database Backup:
1.Connect to RMAN:
rman target /
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
Post a Comment