├── auto-disk-cache.service ├── install.sh ├── sync.sh └── README.md /auto-disk-cache.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Temporarily cache new files before they are properly backed up 3 | 4 | [Service] 5 | Type=simple 6 | Restart=always 7 | ExecStart=/bin/bash /opt/auto-disk-cache/sync.sh 8 | 9 | [Install] 10 | WantedBy=multi-user.target -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [[ $EUID -ne 0 ]]; then 4 | echo "This script must be run as root" 5 | exit 1 6 | fi 7 | 8 | cp auto-disk-cache.service /etc/systemd/system/auto-disk-cache.service 9 | chmod 644 /etc/systemd/system/auto-disk-cache.service 10 | 11 | systemctl enable auto-disk-cache 12 | systemctl start auto-disk-cache 13 | systemctl status auto-disk-cache -------------------------------------------------------------------------------- /sync.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | vers="1.1.0" 3 | now=$(date +"%d-%m-%y") 4 | 5 | echo "--- auto disk cache v$vers ---" 6 | echo "Running on $now $(date +"%T")" 7 | 8 | # --- START OF VARIABLES --- 9 | 10 | source="/mnt/uluru" 11 | destination="/media/wombat/auto-disk-cache" 12 | days=7 13 | 14 | # --- END OF VARIABLES --- 15 | 16 | sync_dest="${destination}/${now}" # Initial dest assignment 17 | initial_run=true 18 | 19 | # --- START OF FUNCTIONS --- 20 | 21 | function remove_old { 22 | while :; do 23 | echo "Checking for old directories..." 24 | 25 | if [ "$now" != "$(date +"%d-%m-%y")" ] || $initial_run 26 | then 27 | 28 | # Update variables 29 | dest="${destination}/${now}" 30 | initial_run=false 31 | now=$(date +"%d-%m-%y") 32 | 33 | # Delete old directories 34 | while [[ $(find "$destination" -maxdepth 1 -type d | wc -l) -gt $((days + 1)) ]] 35 | do 36 | IFS= read -r -d $'\0' line < <(find "$destination" -maxdepth 1 -printf '%T@ %p\0' 2>/dev/null | sort -z -n) 37 | file="${line#* }" 38 | 39 | echo $file 40 | rm -rf "$file" 41 | done 42 | fi 43 | 44 | # Sleep for 1h 45 | echo "Cleanup job sleeping..." 46 | sleep 3600 47 | 48 | done 49 | } 50 | 51 | function monitor { 52 | inotifywait -m -r -e create,modify --format '%w%f' "${source}" | while read newfile 53 | do 54 | rsync -RDa0 "${newfile}" ${sync_dest} 55 | done 56 | } 57 | 58 | # --- END OF FUNCTIONS --- 59 | 60 | monitor & remove_old -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # auto-disk-cache 2 | 3 | ## Usecase 4 | What is the point of this tool? I realised after spending a day video editing off my server, which uses mergerfs and SnapRAID for storage, that all my work for the day would not be backed up until SR ran the following morning, or CrashPlan got round to backing up new data (currently it was busy with other files). 5 | 6 | I searched for something that could act as an intermediary real-time backup until my conventional backup software got round to doing it's thing, but I couldn't find anything so I made this instead! 7 | 8 | ## Features 9 | - Automatically cache (by copying to a different destination) new files and folders within a directory. 10 | 11 | - Automatically remove cache files after specified number of days 12 | 13 | - Monitors source folder and makes copies in near-real-time 14 | 15 | - Cache is stored in unencrypted files / folders allowing easy recovery in the case of data loss 16 | 17 | - Keeps the **latest** version of a file daily; does not keep every version! 18 | 19 | ## Variables 20 | Set your variables in `sync.sh` before installing or running 21 | 22 | ``` 23 | source=[Folder to monitor for changes] 24 | destination=[Cache location] 25 | days=[Number of days of cache to retain] 26 | ``` 27 | 28 | ## Installation 29 | You can either run `sync.sh` manually, or use the included sample service file. 30 | 31 | ``` 32 | $ sudo cp auto-disk-cache.service /etc/systemd/system/auto-disk-cache.service 33 | $ sudo chmod 644 /etc/systemd/system/auto-disk-cache.service 34 | 35 | $ sudo systemctl enable auto-disk-cache 36 | $ sudo systemctl start auto-disk-cache 37 | ``` 38 | 39 | **OR** 40 | 41 | Just run `install.sh` --------------------------------------------------------------------------------