Thursday, 20 March 2025

how to export and import sap hana database users in system refresh

During a system refresh in SAP HANA, you may need to export and import database users to ensure that the target system has the same user configurations as the source system. Below is a step-by-step guide to export and import SAP HANA database users:

---

### **Step 1: Export Users from the Source System**

#### **Option 1: Export Users Using SQL Scripts**
1. **Generate the List of Users:**
   Run the following SQL query in the source system to generate a list of users and their roles:
   ```sql
   SELECT USER_NAME, CREATE_TIME, LAST_SUCCESSFUL_CONNECT, VALID_FROM, VALID_UNTIL
   FROM USERS
   WHERE USER_NAME NOT IN ('SYS', 'SYSTEM', '_SYS_REPO', '_SYS_STATISTICS');
   ```

2. **Export User Roles and Privileges:**
   Use the following SQL query to export user roles and privileges:
   ```sql
   SELECT GRANTEE, GRANTED_ROLE, IS_GRANTABLE
   FROM GRANTED_ROLES;
   ```

3. **Generate SQL Scripts for User Creation:**
   Create a script to generate `CREATE USER` and `GRANT` statements for all users:
   ```sql
   SELECT 'CREATE USER "' || USER_NAME || '" PASSWORD "InitialPassword" NO FORCE_FIRST_PASSWORD_CHANGE;'
   FROM USERS
   WHERE USER_NAME NOT IN ('SYS', 'SYSTEM', '_SYS_REPO', '_SYS_STATISTICS');
   ```

   Save the output as a SQL script (e.g., `create_users.sql`).

4. **Export the Script:**
   Save the generated SQL script to a file for later use.

---

#### **Option 2: Export Users Using SAP HANA Cockpit**
1. Log in to the SAP HANA Cockpit.
2. Navigate to **Security** > **Users**.
3. Export the list of users and their roles to a CSV or Excel file.

---

### **Step 2: Import Users into the Target System**

#### **Option 1: Import Users Using SQL Scripts**
1. **Execute the SQL Script:**
   Run the SQL script generated in Step 1 (e.g., `create_users.sql`) in the target system to create the users.

2. **Grant Roles and Privileges:**
   Execute the `GRANT` statements to assign roles and privileges to the users.

   Example:
   ```sql
   GRANT "ROLE_NAME" TO "USER_NAME";
   ```

3. **Verify User Creation:**
   Check that the users and roles have been correctly imported:
   ```sql
   SELECT * FROM USERS;
   SELECT * FROM GRANTED_ROLES;
   ```

---

#### **Option 2: Import Users Using SAP HANA Cockpit**
1. Log in to the SAP HANA Cockpit in the target system.
2. Navigate to **Security** > **Users**.
3. Use the **Import Users** option to upload the CSV or Excel file exported from the source system.

---

### **Step 3: Post-Import Steps**
1. **Reset Passwords:**
   After importing users, reset their passwords to ensure security:
   ```sql
   ALTER USER "USER_NAME" PASSWORD "NewPassword";
   ```

2. **Validate User Access:**
   Test user logins and verify that they have the correct roles and privileges.

3. **Check System-Specific Users:**
   Ensure that system-specific users (e.g., `SYS`, `SYSTEM`) are not overwritten during the import process.

---

### **Step 4: Automate the Process (Optional)**
If you frequently perform system refreshes, consider automating the export and import process using:
- **SAP HANA SQL Scripts**: Write a script to export and import users.
- **SAP HANA CLI (hdbsql)**: Use command-line tools for automation.
- **SAP HANA Transport Management**: Use transports to move users and roles between systems.

---

### **Important Notes:**
- **Passwords**: Passwords are not exported during the process. You will need to reset them in the target system.
- **System Differences**: Ensure that the source and target systems are compatible (e.g., same SAP HANA version).
- **Backup**: Always take a backup of the target system before performing a system refresh.

By following these steps, you can successfully export and import SAP HANA database users during a system refresh.

No comments:

Post a Comment