0

I have some problem with bash script. I need to add some content to it. My script need to run at a certain time but I don't know how to do that. It should work like this: I have a variable then I assign a time like 3200s. When I run the program, then the script will create backups every 3200s but only if some files changed. What am I doing wrong?

!/bin/bash

SOURCE="/var/www/my_web/load/"
BACKUP="/home/your_user/load/"
LBACKUP="/home/your_user/load/latest-full/"

DATE=$(date +%Y-%m-%d-%T)

DESTINATION="$BACKUP"/"$DATE"-diff/

rsync -av --compare-dest="$LBACKUP" "$SOURCE" "$DESTINATION"

cd "$DESTINATION"
find . -depth -type d -empty -delete
Omar Ali
  • 8,467
  • 4
  • 33
  • 58

1 Answers1

0

here i have added that feature to your script:

usage:

./yourscript.sh 3200

script:

#!/bin/bash

# make sure you gave a number of seconds:
[ 0$1 -gt 0 ] || exit

while true; do
    SOURCE="/var/www/my_web/load/"
    BACKUP="/home/your_user/load/"
    LBACKUP="/home/your_user/load/latest-full/"

    DATE=$(date +%Y-%m-%d-%T)

    DESTINATION="$BACKUP"/"$DATE"-diff/

    rsync -av --compare-dest="$LBACKUP" "$SOURCE" "$DESTINATION"

    cd "$DESTINATION"
    find . -depth -type d -empty -delete

    sleep $1
done

if you get an error like bash: ./yourscript.sh: Permission denied, then you need to do this once: chmod +x yourscript.sh to make the script executable.

to continue running in background even after you leave the terminal window:

nohup ./yourscript.sh 3200 &

to run in background on schedule even after restart:

use cron, e.g., Using crontab to execute script every minute and another every 24 hours

webb
  • 4,180
  • 1
  • 17
  • 26