diff --git a/README.md b/README.md new file mode 100644 index 0000000..6cca14f --- /dev/null +++ b/README.md @@ -0,0 +1,95 @@ +# Automatic backup files script + +## Prerquisites + +* Rsync (included in Debian, Ubuntu, Linux Mint, etc) +* Sudo or Root access +* Terminal learn + +## Quick start + + +1- If disk on save backup is diferent a instalation disk: + +Copy UUID your disk + + sudo blkid + +Editing disks mount points file + + sudo nano /etc/fstab + +Replace backup external disk UUID and mount point for you configuration +``` +# /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=645648-kadfvadfb86teyn1sd3 /backup-disk ext4 defaults 0 0 +``` + +1- Modify custom service: + +* Default backup run on 1 hour (RestartSec=3600) +* Default bash script path is "/home/backup-files.sh" (ExecStart=/to/bash/file) + +``` +[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 +``` + +2- Copy service into path /etc/systemd/system/ + + sudo cp backup-files.service /etc/systemd/system/ + + sudo systemctl dameon-reload + + sudo systemctl enable backup-files.service + + +3- Modify script + +``` +# Paths +external_disk=true // Default backup saved on external disk +backup_path="/backup-disk" //Disk backup mount point +origins=("/home/user1/files" "/home/user2/files/documents") // Origins files array +destinations=("$backup_path/backup-user1" "$backup_path/backup-user2") // Destination backups +``` + +4- Copy script into ExecStart= path defined in .service + + sudo cp backup-files.sh /home/ + + +5- Test script commands + + sudo bash /home/backup-files.sh + +6- If before Test not return never error, then start sevice + + sudo systemctl start backup-files.service + + sudo systemctl status backup-files.service diff --git a/backup-files.sh b/backup-files.sh index 72ea752..6f406a7 100644 --- a/backup-files.sh +++ b/backup-files.sh @@ -1,6 +1,7 @@ #!/bin/bash # Paths +external_disk=true backup_path="/backup-disk" origins=("/home/user1/files" "/home/user2/files/documents") destinations=("$backup_path/backup-user1" "$backup_path/backup-user2") @@ -16,17 +17,21 @@ perform_backup() { return 1 fi - # The mount point disk not exists - if [ ! -d "$backup_path" ]; then - mkdir -p "$backup_path" - echo "Mount point disk created" - fi + if [ external_disk ]; then - # The disk not mount - if ! mountpoint -q "$backup_path"; then - echo "Mounting disk..." - mount "$backup_path" + # 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 fi + # The destination folder not exists if [ ! -d "$destination" ]; then @@ -53,8 +58,11 @@ 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" + +if [ external_disk ]; then + # Umount disk + if mountpoint -q "$backup_path"; then + echo "Umounting disk..." + umount "$backup_path" + fi fi