diff --git a/backup.sh b/backup.sh index d8a3ad96055dd9d887f32e29f8d398efbe59d554..28fd154124367278863f18622ffaaf59af55ffcc 100755 --- a/backup.sh +++ b/backup.sh @@ -135,6 +135,11 @@ if ! [ -d "$destinationDir/$lastBackup/patches" ]; then mkdir "$destinationDir/$lastBackup/patches" fi +#If there are no current file folders, create a folder for them +if ! [ -d "$destinationDir/$lastBackup/patches/files" ]; then + mkdir "$destinationDir/$lastBackup/patches/files" +fi + #Check and copy all files (images, .env) that are either new or changed. cd new files=$(find ".$snipeitDirectory" -type f) @@ -143,11 +148,26 @@ echo "$files" echo "Trying to copy:" while IFS= read -r file; do - if ! [ -f "$destinationDir/$lastBackup/$(echo $file | cut -c 3-)" ] || [ "$destinationDir/$lastBackup/$(echo $file | cut -c 3-)" -ot "$file" ]; then + if ! [ -f "$destinationDir/$lastBackup/$(echo $file | cut -c 3-)" ]; then cp -u --parents "$file" "$destinationDir/$lastBackup/" echo "Copied $file $destinationDir/$lastBackup/$(echo $file | cut -c 3-)" else - echo "File already exists: $destinationDir/$lastBackup/$(echo $file | cut -c 3-)" + #Om filen redan finns, kolla om den finns i en nyare version på disken. Om den är nyare, kolla om den redan patchats. + if [ "$destinationDir/$lastBackup/$(echo $file | cut -c 3-)" -ot "$file" ]; then + lastFilePatch=$(ls -lt "$destinationDir/$lastBackup/patches/files" | head -2 | tail -1 | awk '{print $9}') + if [ "$destinationDir/$lastBackup/patches/files/$lastFilePatch/$(echo $file | cut -c 3-)" -ot "$file" ]; then + #If there are no current file folders for this backup, create a folder for them + if ! [ -d "$destinationDir/$lastBackup/patches/files/$now" ]; then + mkdir "$destinationDir/$lastBackup/patches/files/$now" + fi + cp --parents "$file" "$destinationDir/$lastBackup/patches/files/$now/" + echo "Copied $file $destinationDir/$lastBackup/patches/files/$now/$(echo $file | cut -c 3-)" + else + echo "File is already patched: $destinationDir/$lastBackup/patches/files/$lastFilePatch/$(echo $file | cut -c 3-)" + fi + else + echo "File already exists $destinationDir/$lastBackup/$(echo $file | cut -c 3-)" + fi fi done <<< "$files"