i want to perform the backup of several ubuntu machines towards a qnap nas. i want to…
- User
i want to perform the backup of several ubuntu machines towards a qnap nas. i want to backup the home directories of all users. e.g. one of the machines is 10.74.0.35 (vision), I want to backup/home/user1, /home/user2, /home/user3 and so on
the backup on the nas should have a path like /vision/home/user1,/vision/home/user2, /vision/home/user3
the problem is that each user directory is only reachable by sudo user, otherwise would have used a cron job on each machine. can you suggest the best optimal way to perform it?
- Assistant
To efficiently back up the home directories of multiple Ubuntu machines to your QNAP NAS, especially when each user directory requires
sudoaccess, a centralized backup approach usingrsyncover SSH with elevated permissions is recommended. Here's a step-by-step guide to implement this solution:1. Prepare the NAS for Receiving Backups
a. Create Backup Directories on the NAS:
Organize your backup directories on the NAS to mirror the structure you desire. For example:
/backup/vision/home/user1 /backup/vision/home/user2 /backup/another_machine/home/user1b. Ensure SSH Access:
Ensure that your NAS can receive SSH connections. If necessary, install and configure an SSH server on the NAS.
2. Configure SSH Key-Based Authentication
a. Generate SSH Keys on the NAS:
On your NAS, generate an SSH key pair (if not already existing):
ssh-keygen -t rsa -b 4096 -C "backup@nas" -f /path/to/backup_keyb. Copy the Public Key to Each Ubuntu Machine:
For each Ubuntu machine (e.g.,
10.74.0.35), add the NAS's public key to theauthorized_keysof the backup user:ssh-copy-id -i /path/to/backup_key.pub backup_user@10.74.0.353. Configure Passwordless
sudofor the Backup UserOn each Ubuntu machine:
a. Edit the
sudoersFile:Use
visudoto safely edit thesudoersfile:sudo visudob. Add the Following Line:
backup_user ALL=(ALL) NOPASSWD: /usr/bin/rsyncThis allows
backup_userto runrsyncwithsudowithout being prompted for a password.4. Create the Backup Script on the NAS
Create a script on your NAS to perform the backups. For example,
/path/to/backup_script.sh:#!/bin/bash # Define variables BACKUP_USER="backup_user" SSH_KEY="/path/to/backup_key" NAS_PATH="/backup" MACHINES=( "vision:10.74.0.35" "machine2:10.74.0.36" # Add more machines as needed ) # Loop through each machine and perform backup for machine in "${MACHINES[@]}"; do IFS=":" read -r NAME IP <<< "$machine" echo "Backing up $NAME ($IP)..." rsync -avz -e "ssh -i $SSH_KEY" \ --rsync-path="sudo rsync" \ $BACKUP_USER@$IP:/home/ $NAS_PATH/$NAME/home/ if [ $? -eq 0 ]; then echo "Backup of $NAME successful." else echo "Backup of $NAME failed." fi doneMake the Script Executable:
chmod +x /path/to/backup_script.sh5. Schedule Backups Using Cron
To automate the backup process, schedule the script to run at desired intervals using
cron.Edit the Crontab:
crontab -eAdd a Cron Job (e.g., Daily at 2 AM):
0 2 * * * /path/to/backup_script.sh >> /var/log/backup.log 2>&1This schedules the backup script to run daily at 2 AM and logs output to
/var/log/backup.log.6. Verify and Test the Setup
-
Initial Run: Manually execute the backup script to ensure it works as expected.
/path/to/backup_script.sh -
Check Logs: Review the backup logs to confirm successful backups.
-
Verify Permissions: Ensure that the backup directories on the NAS have the correct permissions and ownership.
Additional Recommendations
-
Security:
-
Limit
rsyncAccess: In thesudoersfile, you can further restrict the commands thatbackup_usercan execute withsudoto enhance security. -
Firewall Rules: Ensure that only the NAS can communicate with the backup ports on the Ubuntu machines.
-
-
Monitoring:
- Implement monitoring to alert you in case of backup failures.
-
Retention Policy:
- Implement a retention policy to manage disk space on the NAS, such as deleting backups older than a certain date.
-
Encryption:
- Consider encrypting backups, especially if sensitive data is involved.
Alternative Solutions
If you require more advanced features like incremental backups, deduplication, or easier management across many machines, consider using dedicated backup solutions such as:
- Bacula: An enterprise-level network backup solution.
- BorgBackup (Borg): Offers compression and encryption.
- Duplicity: Supports encrypted, bandwidth-efficient backups.
- Restic: A modern backup program with strong encryption.
These tools often provide more flexibility and features but may require a more complex setup.
By following the above steps, you can set up an efficient and secure backup process that centralizes backups on your QNAP NAS while handling the necessary
sudopermissions to access user home directories. -