bash
rsync -avz --delete /source/directory /backup/location
bash
tar -cvpzf /backup/backup-$(date +%Y%m%d).tar.gz --exclude=/backup --exclude=/proc --exclude=/tmp --exclude=/mnt --exclude=/dev --exclude=/sys /
bash
dd if=/dev/sda of=/backup/sda.img bs=4M
mysqldump
bash
mysqldump -u root -p --all-databases > /backup/mysql-backup-$(date +%Y%m%d).sql
pg_dump
bash
pg_dump -U postgres -F c -b -f /backup/pg-backup-$(date +%Y%m%d).backup dbname
# 编辑crontab
crontab -e
# 每天凌晨2点执行完整备份
0 2 * * * /usr/bin/tar -cvpzf /backup/full-backup-$(date +\%Y\%m\%d).tar.gz --exclude=/backup --exclude=/proc --exclude=/tmp --exclude=/mnt --exclude=/dev --exclude=/sys /
# 每小时执行增量备份
0 * * * * /usr/bin/rsync -avz --delete /important/data /backup/hourly/
#!/bin/bash
# backup_script.sh
DATE=$(date +%Y%m%d)
BACKUP_DIR="/backup"
LOG_FILE="/var/log/backup.log"
# 创建备份目录
mkdir -p $BACKUP_DIR/$DATE
# 完整备份系统
echo "$(date) - 开始系统备份" >> $LOG_FILE
tar -cvpzf $BACKUP_DIR/$DATE/full-backup-$DATE.tar.gz \
--exclude=$BACKUP_DIR \
--exclude=/proc \
--exclude=/tmp \
--exclude=/mnt \
--exclude=/dev \
--exclude=/sys / >> $LOG_FILE 2>&1
# 备份MySQL数据库
echo "$(date) - 开始MySQL备份" >> $LOG_FILE
mysqldump -u root -p'password' --all-databases > $BACKUP_DIR/$DATE/mysql-$DATE.sql 2>> $LOG_FILE
# 备份完成
echo "$(date) - 备份完成" >> $LOG_FILE
# 从tar备份恢复
tar -xvpzf /backup/full-backup-20230101.tar.gz -C /
# 使用rsync恢复
rsync -avz /backup/hourly/ /important/data
bash
tar -xvpzf /mnt/backup/full-backup-20230101.tar.gz -C /mnt/root
bash
grub-install /dev/sda
update-grub
# MySQL恢复
mysql -u root -p < /backup/mysql-backup-20230101.sql
# PostgreSQL恢复
pg_restore -U postgres -d dbname /backup/pg-backup-20230101.backup
# 检查tar备份完整性
tar -tvf /backup/full-backup-20230101.tar.gz > /dev/null
# 检查MySQL备份
head -n 1 /backup/mysql-backup-20230101.sql | grep "MySQL dump"
# 检查最近备份文件
ls -lh /backup/
# 检查备份日志
tail -n 20 /var/log/backup.log
# 设置邮件通知(在备份脚本中添加)
if [ $? -eq 0 ]; then
echo "备份成功" | mail -s "备份状态" admin@example.com
else
echo "备份失败" | mail -s "备份状态警告" admin@example.com
fi
3-2-1备份规则:
加密敏感备份:
# 使用gpg加密备份
gpg -c --cipher-algo AES256 /backup/sensitive-data.tar.gz
定期演练恢复:每季度至少执行一次恢复测试
备份存储管理:
文档化流程:
通过实施上述方案,您可以确保Linux系统在面临数据丢失或系统故障时能够快速恢复,最大限度减少业务中断时间。