如何创建自定义备份脚本?修改权限后tar命令无法运行怎么办?
Hey there! Let's tackle your two questions one by one, starting with building a custom backup script, then troubleshooting your tar permission issue.
Let's build a practical, reusable script that compresses your home directory, adds timestamped filenames, and even cleans up old backups. Here's a step-by-step breakdown:
Step 1: Write the Script Skeleton
Create a new file (e.g., home_backup.sh) with this structure—customize the variables to match your setup:
#!/bin/bash # Define core variables (adjust these to your needs) SOURCE_DIR="$HOME" # The directory you want to back up (your home folder) BACKUP_DIR="/mnt/external-drive/backups" # Where to store finished backups DATE_STAMP=$(date +%Y-%m-%d_%H-%M-%S) # Unique timestamp for backup filenames BACKUP_FILE="home_backup_$DATE_STAMP.tar.gz" # Create backup directory if it doesn't exist if [ ! -d "$BACKUP_DIR" ]; then mkdir -p "$BACKUP_DIR" echo "Created backup directory: $BACKUP_DIR" fi # Run the compression command echo "Starting backup of $SOURCE_DIR..." tar -czf "$BACKUP_DIR/$BACKUP_FILE" "$SOURCE_DIR" # Verify backup success if [ $? -eq 0 ]; then echo "Backup completed! File saved to: $BACKUP_DIR/$BACKUP_FILE" else echo "Error: Backup failed. Double-check paths and permissions." fi # Optional: Clean up backups older than 7 days echo "Removing old backups (older than 7 days)..." find "$BACKUP_DIR" -name "home_backup_*.tar.gz" -type f -mtime +7 -delete echo "Cleanup finished."
Step 2: Finalize and Automate
- Make the script executable:
chmod +x home_backup.sh - Test it manually first:
./home_backup.sh - For automatic backups, add it to cron (run
crontab -eand add a line like0 2 * * * /path/to/home_backup.shto run daily at 2 AM).
Let's troubleshoot the most common causes for this issue:
Check the Script's Execution Permissions
If you set restrictive permissions (like chmod 600) on your script, it won't run. Fix this with:
chmod +x your_backup_script.sh
Verify tar's Ownership and Permissions
Accidentally modifying system file permissions can break tar. Check if tar has correct access:
# Find tar's location which tar # Example output: /usr/bin/tar # Check tar's permissions ls -l /usr/bin/tar # It should show: -rwxr-xr-x 1 root root ... tar
If permissions are wrong, reset them with:
sudo chmod 755 /usr/bin/tar
Ensure You Have Access to Home Directory Files
If you're running the script as a different user (e.g., with sudo), that user might lack read access to your home folder. Test the tar command manually first to see errors:
tar -czf test_backup.tar.gz "$HOME"
If you get "Permission denied" errors, adjust the home directory's permissions (e.g., chmod o+rX "$HOME"—but be cautious with broad permissions).
Use Absolute Path for tar in the Script
Sometimes shell environment issues prevent the system from finding tar. Replace tar with its full path (from which tar) in your script:
/usr/bin/tar -czf "$BACKUP_DIR/$BACKUP_FILE" "$SOURCE_DIR"
Check for SELinux Restrictions
On SELinux-enabled systems (like RHEL/CentOS), incorrect file contexts can block tar. Temporarily disable SELinux to test:
sudo setenforce 0
If this fixes the issue, adjust the SELinux context for your backup directory:
sudo chcon -R system_u:object_r:backup_t:s0 "$BACKUP_DIR"
内容的提问来源于stack exchange,提问作者user794322

