Uglavnom, volim backupirati stvari i volim da je to automatizirano, pa rekoh, zašto ne napraviti automatizaciju koja će, kad se ušteka određeni USB, mountati ga i napraviti incrementalni backup na njega, te kad završi, unmountati ga?
Na kraju se ispostavilo da je to poprilično jednostavno:
Prvo valja izvući uuid vašeg diska za koji želite napraviti trigger:
Nađete koji je vaš disk i kopirate njegov UUID
1. Napraviti skriptu koja će raditi backup, ja osobno radim rsync:
Code:
nano /usr/local/bin/backup_data.sh
Code:
#!/bin/bash
# Variables
UUID="e6f8b3fb-9a44-4962-b33f-e1fd98df8230" # Replace with your disk's UUID
MOUNT_POINT="/mnt/mydisk" # Adjust the mount point as needed
SOURCE_FOLDER="/path/to/folder_X" # Replace with the path to the folder "X"
BACKUP_FOLDER="$MOUNT_POINT/backup" # Backup location on the mounted disk
LOG_DIR="/var/log/mount_backup" # Directory to store log files
LOG_FILE="$LOG_DIR/backup_$(date +'%Y%m%d_%H%M%S').log" # Log file with timestamp
# Ensure the log directory exists
[ ! -d "$LOG_DIR" ] && mkdir -p "$LOG_DIR"
# Function to perform the backup
perform_backup() {
echo "[$(date)] Starting backup from $SOURCE_FOLDER to $BACKUP_FOLDER" | tee -a "$LOG_FILE"
# Use rsync to perform the backup, excluding the specified folder
rsync -av "$SOURCE_FOLDER/" "$BACKUP_FOLDER/" | tee -a "$LOG_FILE"
if [ $? -eq 0 ]; then
echo "[$(date)] Backup completed successfully." | tee -a "$LOG_FILE"
else
echo "[$(date)] Backup failed." | tee -a "$LOG_FILE"
fi
}
# Function to clean up old log files (older than 7 days)
cleanup_logs() {
echo "[$(date)] Cleaning up log files older than 7 days." | tee -a "$LOG_FILE"
find "$LOG_DIR" -type f -name "*.log" -mtime +7 -exec rm {} \;
echo "[$(date)] Log cleanup completed." | tee -a "$LOG_FILE"
}
# Check if the disk is already mounted using findmnt and UUID
if ! findmnt -rn -S "UUID=$UUID" > /dev/null; then
# Create the mount point directory if it doesn't exist
[ ! -d "$MOUNT_POINT" ] && mkdir -p "$MOUNT_POINT"
# Mount the disk
mount -U "$UUID" "$MOUNT_POINT"
# Check if the mount was successful
if findmnt -rn -S "UUID=$UUID" > /dev/null; then
echo "[$(date)] Disk mounted successfully at $MOUNT_POINT" | tee -a "$LOG_FILE"
# Ensure the backup directory exists
[ ! -d "$BACKUP_FOLDER" ] && mkdir -p "$BACKUP_FOLDER"
# Perform the backup
perform_backup
else
echo "[$(date)] Failed to mount the disk" | tee -a "$LOG_FILE"
fi
else
echo "[$(date)] Disk is already mounted." | tee -a "$LOG_FILE"
# Perform the backup even if the disk was already mounted
perform_backup
fi
# Clean up old log files
cleanup_logs
# Unmount disk
umount "$MOUNT_POINT"
2. napraviti da je skripta executable:
Code:
sudo chmod +x /usr/local/bin/backup_data.sh
3. Napraviti servis koji će pokretati skriptu koristeći systemd:
Code:
sudo nano /etc/systemd/system/backup_data.service
Code:
[Unit]
Description=Backup Data on Insertion
[Service]
ExecStart=/usr/local/bin/backup_data.sh
User=root
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
4. Napraviti servis koji će raditi monitoring /dev/disk/by-uuid/ foldera:
Code:
sudo nano /etc/systemd/system/monitor_disk.path
Code:
[Unit]
Description=Monitor USB Disk Path
[Path]
PathExists=/dev/disk/by-uuid/e6f8b3fb-9a44-4962-b33f-e1fd98df8230
DirectoryNotEmpty=/dev/disk/by-uuid/
Unit=backup_data.service
[Install]
WantedBy=multi-user.target
5. reloadati systemd:
Code:
sudo systemctl daemon-reload
6. aktivirati servis za monitoring:
Code:
sudo systemctl enable monitor_disk.path
sudo systemctl start monitor_disk.path
Ukratko, monitor_disk.path će čekati da se ušteka USB disk sa određenim UUID-jem i onda triggerati servis koji će pokrenuti vašu skriptu.
NAPOMENA: ne startati servis backup_data.service, njega će triggerati .path servis.
Eto, nadam se da sam nekom pomoga.