s
This commit is contained in:
32
backup/auto_copy_backup.sh
Normal file
32
backup/auto_copy_backup.sh
Normal file
@@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Define backup directory on your local machine
|
||||
BACKUP_DIR="/path/to/your/backups"
|
||||
WEEKLY_DIR="$BACKUP_DIR/weekly"
|
||||
|
||||
# Run the backup script
|
||||
ssh server /root/backup/script.sh
|
||||
scp server:/root/backup/backup_$(date +"%Y%m%d").tar "$BACKUP_DIR"
|
||||
ssh server rm /root/backup/backup_$(date +"%Y%m%d").tar
|
||||
|
||||
# Keep only the last 7 daily backups
|
||||
find "$BACKUP_DIR" -maxdepth 1 -name "backup_*.tar" -type f -mtime +6 -exec rm {} \;
|
||||
|
||||
# Weekly backup: Every Sunday, move the daily backup to a weekly folder
|
||||
if [ "$(date +%u)" -eq 7 ]; then
|
||||
mkdir -p "$WEEKLY_DIR"
|
||||
cp "$BACKUP_DIR/backup_$(date +"%Y%m%d").tar" "$WEEKLY_DIR/weekly_$(date +"%Y%m%d").tar"
|
||||
|
||||
# Enforce 100 GB limit for weekly backups
|
||||
while true; do
|
||||
TOTAL_SIZE=$(du -s "$WEEKLY_DIR" | awk '{print $1}')
|
||||
if [ "$TOTAL_SIZE" -le 102400 ]; then # 100 GB in KB
|
||||
break
|
||||
fi
|
||||
# Find and remove the oldest weekly backup
|
||||
OLDEST_FILE=$(ls -t "$WEEKLY_DIR" | tail -1)
|
||||
if [ -n "$OLDEST_FILE" ]; then
|
||||
rm "$WEEKLY_DIR/$OLDEST_FILE"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
Reference in New Issue
Block a user