Hardening Backup Security: Securing Dump Credentials and File Permissions on VPS
A backup is supposed to protect important data. But if the backup script contains exposed passwords or creates files that anyone can read, it can become a security risk.
Many Linux VPS administrators automate database backups using mysqldump, shell scripts, and cron. These tools work well, but their security depends on how credentials, permissions, and backup files are handled.
A few changes can make backup routines safer without making them difficult to manage.
Why Hardcoding Passwords in Backup Scripts Is Risky
A common mistake is placing the database password directly inside a shell script.
For example:
mysqldump -u backupuser -pMyPassword database_name > backup.sql
This approach has several problems.
Anyone who can read the script may discover the password. The credential can also appear in shell history, source control, or process information, depending on how the command is executed and how the system is configured.
The safer approach is to keep database credentials in a separate, protected configuration file.
Store MySQL Credentials in a Protected .my.cnf File
MySQL supports option files that allow client programs to read connection settings without requiring a password directly in the command.
For a root-run backup process, a configuration file can be stored at:
/root/.my.cnf
Step 1: Create the Configuration File
Open the file using a text editor:
sudo nano /root/.my.cnf
Add the following example configuration:
[client]
user=backupuser
password="YOUR_DATABASE_PASSWORD"
host=localhost
Replace the placeholder with the actual credentials.
The backup account should have only the database permissions necessary to perform its job. Avoid using the MySQL administrative account when a restricted backup account is sufficient.
This file contains a plaintext password, so protecting access to it is essential.
Step 2: Restrict File Permissions
Set ownership and permissions:
sudo chown root:root /root/.my.cnf
sudo chmod 600 /root/.my.cnf
The permission 600 allows the owner to read and write the file, while other users receive no access through standard Linux file permissions.
Step 3: Run mysqldump Without a Password Argument
When the backup command runs as root, it can read /root/.my.cnf.
For example:
sudo mysqldump --defaults-file=/root/.my.cnf --single-transaction --quick database_name > backup.sql
The password is no longer included directly in the command.
The --defaults-file option tells MySQL which configuration file to read. It must appear before other ordinary options.
The --single-transaction option is useful for obtaining a consistent snapshot of transactional tables such as InnoDB. It does not provide the same consistency guarantees for nontransactional tables, and concurrent schema changes can cause problems.
For a dedicated backup service account, use a protected option file owned by that account instead of relying on root.
Secure Backup Script Permissions
Protecting database credentials is only one part of backup security.
The backup script itself should also have restricted permissions.
For example:
sudo chmod 700 /root/scripts/backup.sh
The 700 permission allows the file owner to read, write, and execute the script while denying those permissions to other users.
Also verify ownership:
sudo chown root:root /root/scripts/backup.sh
These commands assume the script exists and the backup process is intended to run as root.
For production systems, a separate restricted backup account is often preferable when the job does not require root privileges.
Protect the Backup Directory
Database dumps can contain customer information, application records, configuration details, and other sensitive data.
Even if the original database is protected, an unencrypted SQL dump may expose its contents when copied or accessed.
Create a restricted backup directory:
sudo install -d -m 700 -o root -g root /var/backups/secure
This creates a directory with access restricted to root through normal filesystem permissions.
Backup scripts should also use a restrictive file-creation mask:
umask 077
This helps prevent newly created backup files from receiving permissions for group members or other users.
Remember that restrictive permissions do not protect against a compromised root account or an attacker with equivalent privileges.
Encrypt Backup Archives with GPG
File permissions protect access on the Linux system, but encryption adds protection when archives are copied to another location.
GPG can encrypt backup files so their contents cannot be read without the required decryption credentials.
Step 1: Encrypt an Existing Archive
For an interactive example:
gpg --symmetric --output backup.tar.gz.gpg backup.tar.gz
GPG asks for a passphrase to protect the file.
Step 2: Decrypt the Archive
To recover the original file:
gpg --output restored-backup.tar.gz --decrypt backup.tar.gz.gpg
Enter the correct passphrase when prompted.
Keep the recovery passphrase securely stored and accessible to authorized administrators.
Step 3: Encrypt Automated Backups
For unattended cron jobs, GPG public-key encryption is often more practical than interactive passphrase entry.
A basic example is:
gpg --encrypt --recipient BACKUP_KEY_ID --output backup.sql.gpg backup.sql
Replace BACKUP_KEY_ID with the actual recipient key identifier.
The correct public key must already be available in the GPG keyring. The corresponding private key is required to decrypt the backup.
For better protection, avoid creating an unnecessary plaintext intermediate file when your backup workflow supports streaming data directly into encryption.
Encryption should also be tested through a recovery procedure before relying on it.
Keep Backup Credentials Out of Logs
Backup logs are useful for troubleshooting, but they should not reveal confidential information.
Avoid enabling shell debugging with set -x in scripts that handle secrets.
Do not print passwords, authentication tokens, or the contents of credential files.
Limit access to backup logs and verify that error messages do not accidentally expose credentials.
Use Least-Privilege Database Accounts
A backup user should receive only the privileges necessary for the backup method and database objects involved.
For example, a logical backup may need access to tables, views, triggers, or other database objects depending on what is being exported.
Avoid granting unrestricted administrative privileges simply because a backup command fails.
Instead, identify the missing permissions and grant only those required for the job.
Review the backup account's permissions whenever the database structure or backup process changes.
Test Your Backup Security Before Scheduling Cron
Before enabling a new automated backup, check the configuration manually.
Confirm File Permissions
sudo ls -l /root/.my.cnf
sudo ls -l /root/scripts/backup.sh
Verify that ownership and permissions match the intended backup account.
Check Database Access
Run a test backup and confirm that the expected data has been exported.
Check Encryption and Recovery
Decrypt a test archive in a secure location and verify that the restored data is usable.
Review Backup Logs
Confirm that credentials and other sensitive values are not included in output files.
For more VPS security practices, see the VPS server hardening and security tips guide.
Common Backup Security Mistakes
Saving Passwords Directly in Shell Scripts
Use protected MySQL option files or a suitable secrets-management system instead.
Giving Backup Files Open Permissions
Restrict archive access and use appropriate directory permissions.
Leaving Plaintext Archives on the Server
Encrypt sensitive backups and securely manage temporary plaintext files.
Using Root for Every Backup Task
Use an account with limited permissions whenever the backup process allows it.
Never Testing Encrypted Backups
An encrypted archive is not useful if the recovery key is missing or the file cannot be restored.
Final Thoughts
Securing VPS backups starts with protecting database credentials, restricting file access, and encrypting sensitive archives.
Keep passwords out of commands, configure permissions carefully, and verify that scheduled backup jobs work with the intended user account.
Most importantly, test the recovery process. A secure backup should remain accessible to authorized administrators when the original data needs to be restored.
- Art
- Causes
- Crafts
- Dance
- Drinks
- Film
- Fitness
- Food
- الألعاب
- Gardening
- Health
- الرئيسية
- Literature
- Music
- Networking
- أخرى
- Party
- Religion
- Shopping
- Sports
- Theater
- Wellness