backup-prox-cfg-files

Below is a shell script that will back up all the important Proxmox configuration files and directories to a specified backup location. This script can be run on each of your Proxmox servers to ensure that all configurations are safely backed up.

### Backup Script: `backup_proxmox_config.sh`

————————————————————————————————————–

#!/bin/bash

# Set backup destination directory
BACKUP_DIR=”/backup/proxmox-config-backup”
TIMESTAMP=$(date +”%Y%m%d_%H%M%S”)
BACKUP_PATH=”$BACKUP_DIR/proxmox_config_$TIMESTAMP.tar.gz”

# Create backup directory if it does not exist
mkdir -p “$BACKUP_DIR”

# Directories and files to back up
CONFIG_DIRS=(
“/etc/pve/”
“/etc/network/interfaces”
“/etc/hosts”
“/etc/fstab”
“/etc/ceph/”
“/etc/lvm/”
“/var/lib/pve-cluster/”
“/etc/postfix/”
)

# Check if each directory exists before backup
BACKUP_ITEMS=()
for DIR in “${CONFIG_DIRS[@]}”; do
if [ -e “$DIR” ]; then
BACKUP_ITEMS+=(“$DIR”)
else
echo “Warning: $DIR does not exist and will not be included in the backup.”
fi
done

# Create a tar.gz archive of the configuration files
if [ ${#BACKUP_ITEMS[@]} -ne 0 ]; then
tar -czf “$BACKUP_PATH” “${BACKUP_ITEMS[@]}”
echo “Backup completed successfully. Backup file: $BACKUP_PATH”
else
echo “No valid directories or files to back up. Backup aborted.”
fi

# Optionally, you can set up a cron job to automate this script
# Example: Run backup daily at 2:00 AM
# 0 2 * * * /path/to/backup_proxmox_config.sh
“`

————————————————————————————————————–

### Explanation:

1. **Backup Directory:**
– The script sets the backup destination directory (`/backup/proxmox-config-backup`). You can change this path to any desired location where you want to store the backups.

2. **Timestamping:**
– Each backup is timestamped with the current date and time, ensuring that older backups are not overwritten.

3. **Configuration Directories:**
– The `CONFIG_DIRS` array contains the paths of all important Proxmox configuration directories and files that you want to back up. The script checks whether each path exists before adding it to the backup.

4. **Creating Backup:**
– If there are valid directories/files to back up, the script creates a compressed tarball (`.tar.gz`) in the specified backup directory.

5. **Logging:**
– The script outputs the status of the backup process, including any warnings about missing directories or files.

6. **Automation (Optional):**
– The script can be added to a cron job to automate the backup process. The commented example in the script sets it to run daily at 2:00 AM.

### How to Use:

1. **Save the Script:**
– Save the script as `backup_proxmox_config.sh`.

2. **Make It Executable:**
“`bash
chmod +x backup_proxmox_config.sh
“`

3. **Run the Script:**
– You can run the script manually:
“`bash
sudo ./backup_proxmox_config.sh
“`
– Or schedule it to run automatically using cron.

### Scheduling with Cron:

To automate the backup process, you can add a cron job. For example, to run the script every day at 2:00 AM:

1. Open the crontab file for editing:
“`bash
sudo crontab -e
“`

2. Add the following line to schedule the script:
“`bash
0 2 * * * /path/to/backup_proxmox_config.sh
“`

This script provides a simple and effective way to back up all your Proxmox configuration files regularly, ensuring you have a recovery plan in case of system failures or data loss.

————————————————————————————————————–

————————————————————————————————————–