This commit is contained in:
facundo.prog 2024-04-03 14:55:11 -03:00
parent 3beb764ad2
commit 4f4092e92d
2 changed files with 116 additions and 13 deletions

95
README.md Normal file
View File

@ -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.
#
# <file system> <mount point> <type> <options> <dump> <pass>
# 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

View File

@ -1,6 +1,7 @@
#!/bin/bash #!/bin/bash
# Paths # Paths
external_disk=true
backup_path="/backup-disk" backup_path="/backup-disk"
origins=("/home/user1/files" "/home/user2/files/documents") origins=("/home/user1/files" "/home/user2/files/documents")
destinations=("$backup_path/backup-user1" "$backup_path/backup-user2") destinations=("$backup_path/backup-user1" "$backup_path/backup-user2")
@ -16,17 +17,21 @@ perform_backup() {
return 1 return 1
fi fi
# The mount point disk not exists if [ external_disk ]; then
if [ ! -d "$backup_path" ]; then
mkdir -p "$backup_path"
echo "Mount point disk created"
fi
# The disk not mount # The mount point disk not exists
if ! mountpoint -q "$backup_path"; then if [ ! -d "$backup_path" ]; then
echo "Mounting disk..." mkdir -p "$backup_path"
mount "$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 fi
# The destination folder not exists # The destination folder not exists
if [ ! -d "$destination" ]; then if [ ! -d "$destination" ]; then
@ -53,8 +58,11 @@ for (( i=0; i<${#origins[@]}; i++ )); do
perform_backup "${origins[i]}" "${destinations[i]}" perform_backup "${origins[i]}" "${destinations[i]}"
done done
# Umount disk
if mountpoint -q "$backup_path"; then if [ external_disk ]; then
echo "Umounting disk..." # Umount disk
umount "$backup_path" if mountpoint -q "$backup_path"; then
echo "Umounting disk..."
umount "$backup_path"
fi
fi fi