diff --git a/backup-files.service b/backup-files.service new file mode 100644 index 0000000..6af6957 --- /dev/null +++ b/backup-files.service @@ -0,0 +1,13 @@ +[Unit] +Description=Automatic backup files +After=network.target + +[Service] +Type=simple +User=root +ExecStart=/home/backup-files.sh +Restart=always +RestartSec=3600 + +[Install] +WantedBy=multi-user.target diff --git a/backup-files.sh b/backup-files.sh new file mode 100644 index 0000000..72ea752 --- /dev/null +++ b/backup-files.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# Paths +backup_path="/backup-disk" +origins=("/home/user1/files" "/home/user2/files/documents") +destinations=("$backup_path/backup-user1" "$backup_path/backup-user2") + +# Function to perform backup for each origin-destination pair +perform_backup() { + local origin="$1" + local destination="$2" + + # The origin folder not exists + if [ ! -d "$origin" ]; then + echo "Origin folder not exists: $origin" + return 1 + fi + + # The mount point disk not exists + if [ ! -d "$backup_path" ]; then + mkdir -p "$backup_path" + echo "Mount point disk created" + fi + + # The disk not mount + if ! mountpoint -q "$backup_path"; then + echo "Mounting disk..." + mount "$backup_path" + fi + + # The destination folder not exists + if [ ! -d "$destination" ]; then + echo "Folder $destination created" + mkdir "$destination" + fi + + # Execute rsync + rsync -av --delete "$origin/" "$destination/" + + # Verify folder backup + if [ $? -eq 0 ]; then + echo "Backup successfully saved $origin on $destination" + else + echo "Error on copy files" + return 1 + fi + + return 0 +} + +# Iterate over origin-destination pairs and perform backup +for (( i=0; i<${#origins[@]}; i++ )); do + perform_backup "${origins[i]}" "${destinations[i]}" +done + +# Umount disk +if mountpoint -q "$backup_path"; then + echo "Umounting disk..." + umount "$backup_path" +fi diff --git a/fstab b/fstab new file mode 100644 index 0000000..5d29774 --- /dev/null +++ b/fstab @@ -0,0 +1,16 @@ +# /etc/fstab: static file system information. +# +# Use 'blkid' to print the universally unique identifier for a +# device; this may be used with UUID= as a more robust way to name devices +# that works even if disks are added and removed. See fstab(5). +# +# systemd generates mount units based on this file, see systemd.mount(5). +# Please run 'systemctl daemon-reload' after making changes here. +# +# + +# Others discks +UUID= /boot/efi vfat umask=0077 0 1 + +# backup external disk +UUID= /backup-disk ext4 defaults 0 0