crontab weekly
#!/bin/bash
echo "New log file..." > /home/facundo/log.txt
echo "#############################" >> /home/facundo/log.txt
date >> /home/facundo/log.txt
echo "#############################" >> /home/facundo/log.txt
echo "Processing........................................" >> /home/facundo/log.txt
echo "Deleting files older than a month in dst folder..." >> /home/facundo/log.txt
find /mnt/hdd/backups/rberrypart/ -mtime +30 -delete
echo "done" >> /home/facundo/log.txt
echo "Moving the last backups to retention folder......." >> /home/facundo/log.txt
mv /mnt/hdd/backups/rberrypart/latest/PiOS.img.gz /mnt/hdd/backups/rberrypart/PiOS.img.gz_$(date +"%Y%m%d-%H%M%S")
mv /mnt/hdd/backups/rberrypart/latest/bootpart.img.gz /mnt/hdd/backups/rberrypart/bootpart.img.gz_$(date +"%Y%m%d-%H%M%S")
echo "done" >> /home/facundo/log.txt
echo "#############################" >> /home/facundo/log.txt
date >> /home/facundo/log.txt
echo "#############################" >> /home/facundo/log.txt
echo "dding and gzipping /dev/sda1 using the default options" >> /home/facundo/log.txt
echo "-----------------------------" >> /home/facundo/log.txt
dd bs=4M if=/dev/sda1 2>>/home/facundo/log.txt | gzip --fast > /mnt/hdd/backups/rberrypart/latest/PiOS.img.gz
echo "done" >> /home/facundo/log.txt
echo "-----------------------------" >> /home/facundo/log.txt
echo "" >> /home/facundo/log.txt
echo "now dding and --fast gzipping the boot part......." >> /home/facundo/log.txt
echo "#############################" >> /home/facundo/log.txt
date >> /home/facundo/log.txt
echo "#############################" >> /home/facundo/log.txt
echo "Processing /dev/mmcblk0p1 ................" >> /home/facundo/log.txt
echo "-----------------------------" >> /home/facundo/log.txt
dd bs=4M if=/dev/mmcblk0p1 2>>/home/facundo/log.txt | gzip --fast > /mnt/hdd/backups/rberrypart/latest/bootpart.img.gz
echo "Finished, all done." >> /home/facundo/log.txt
echo "#############################" >> /home/facundo/log.txt
date >> /home/facundo/log.txt
echo "#############################" >> /home/facundo/log.txt
echo "Now sending the status log................" >> /home/facundo/log.txt
mutt -s "Weekly Backup Status" facundo.ipharraguerre@pm.me < /home/facundo/log.txt
tal vez se pueda formatear en html
Como poder, se puede pasar a HTML, pero preferible enviarlo en texto plano...
#!/bin/bash
LOG_FILE="/home/facundo/log.txt"
BACKUP_DIR="/mnt/hdd/backups/rberrypart"
DATE="$(date +"%Y%m%d-%H%M%S")"
echo "New log file..." > "$LOG_FILE"
echo "$(date)" >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
echo "Files right now in the destination directory:" >> "$LOG_FILE"
ls -ltah "$BACKUP_DIR" | awk '{print $5, $9}' >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
echo "Deleting files older than 2 weeks in destination folder..." >> "$LOG_FILE"
find "$BACKUP_DIR" -mtime +13 -delete
echo "Done" >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
echo "Moving the last backups to retention folder:" >> "$LOG_FILE"
mv "$BACKUP_DIR/latest/PiOS.img.gz" "$BACKUP_DIR/PiOS.img.gz_$DATE"
mv "$BACKUP_DIR/latest/bootpart.img.gz" "$BACKUP_DIR/bootpart.img.gz_$DATE"
echo "Done" >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
echo "Backing up /dev/sda1:" >> "$LOG_FILE"
dd bs=4M if=/dev/sda1 2>> "$LOG_FILE" | gzip --fast > "$BACKUP_DIR/latest/PiOS.img.gz"
echo "Done" >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
echo "Backing up /dev/mmcblk0p1 (the boot partition):" >> "$LOG_FILE"
dd bs=4M if=/dev/mmcblk0p1 2>> "$LOG_FILE" | gzip --fast > "$BACKUP_DIR/latest/bootpart.img.gz"
echo "Finished, all done." >> "$LOG_FILE"
echo "Files in the destination directory:" >> "$LOG_FILE"
ls -ltah "$BACKUP_DIR" | awk '{print $5, $9}' >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
echo "Now sending the status log." >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
mutt -s "Weekly Backup Status" facundo@ad.facundoitest.space < "$LOG_FILE"
===== Results =====
El correo recibido tiene el siguiente aspecto:
New log file...
Sat 27 Apr 04:00:01 -03 2024
Files right now in the destination directory:
0 latest
4.0K .
156M bootpart.img.gz_20240420-040001
6.6G PiOS.img.gz_20240420-040001
156M bootpart.img.gz_20240413-040002
6.6G PiOS.img.gz_20240413-040002
8.0K ..
Deleting files older than 2 weeks in destination folder...
Done
Moving the last backups to retention folder:
Done
Backing up /dev/sda1:
3974+1 records in
3974+1 records out
16669671936 bytes (17 GB, 16 GiB) copied, 4169.89 s, 4.0 MB/s
Done
Backing up /dev/mmcblk0p1 (the boot partition):
64+0 records in
64+0 records out
268435456 bytes (268 MB, 256 MiB) copied, 81.3556 s, 3.3 MB/s
Finished, all done.
Files in the destination directory:
264 latest
4.0K .
156M bootpart.img.gz_20240427-040001
6.7G PiOS.img.gz_20240427-040001
156M bootpart.img.gz_20240420-040001
6.6G PiOS.img.gz_20240420-040001
8.0K ..
Now sending the status log.