Wiki‎ > ‎

Setup Crontab/Rsync Automatic Backup on Windows

posted Dec 29, 2015, 1:54 PM by Dong Xu   [ updated Oct 22, 2016, 1:17 AM ]
Use windows copy the1st time backup and rsync for later incremental

Used cwRsync version 3.12 (less buggy), need to copy its cygwin1.dll to mobaxterm slash/bin for this new version to work.

set up mobaxterm cron server with this script for nightly backup.

#!/bin/bash

backup_log=backup_`date +%m-%d-%Y`.log.txt
echo "Backup Start: `date +%m-%d-%Y-%T`" > /cygdrive/e/Backup/$backup_log
~/bin/rsync.exe -avhW --no-compress --progress --exclude="*RECYCLE.BIN*" --exclude="System*Volume*Information"  --exclude="*Temporary*Files" /cygdrive/d /cygdrive/e/Backup/ >> /cygdrive/e/Backup/$backup_log 2>&1
echo "Backup End: `date +%m-%d-%Y-%T`" >> /cydrive/e/Backup/$backup_log


=================================
Reference http://www.davidjnice.com/cygwin_cron_service.html

1. Install cygwin to c:/cygwin64, make sure it has cron.exe in /usr/sbin   crontab.exe in /bin and rsync.exe in /bin, cygrunsrv.exe in /bin

2. Run cygwin as a windows administrator, type
cygrunsrv --install cron --path /usr/sbin/cron.exe --args -n

3. This should create a service (check it in Services)

4. Create a new user "cygrunsrv" and set it as a local admin.

5. launch local policies "secpol.msc", add that user to 'Create a token object', 'Logon as a service' & 'Replace a process level token'

6. type crontab -e  in cygwin

This will open the crontab in vi(m). Add the following line:

* * * * * echo "Cron test at $(date +\%k:\%M)" >> /cygdrive/c/crontest.txt 2>&1

7. You can start and stop the cron service by using:

  • Using the Windows Services
  • From a cmd window or run dialogue; 'net start cron' & 'net stop cron'
  • Or, since you are setting this up in Cygwin; 'cygrunsrv --start cron' & 'cygrunsrv --start cron'

8. If everything is running correctly this will generate a file called c:\crontest.log and add a new line every minute.

If step 8 works, proceed to step 9, otherwise debug step 8 by using Windows Event Viewer

The following scripts back up drive D to drive E at 11:55pm daily and remove deleted files from drive E at noon on the 1st day of each month.

9. vim backup.sh

#!/bin/bash

backup_log=backup_`date +%m-%d-%Y`.log.txt
echo "Backup Start: `date +%m-%d-%Y-%T`" > /cygdrive/c/$backup_log
/bin/rsync -avhW --no-compress --progress /cygdrive/d /cygdrive/e >> /cygdrive/c/$backup_log 2>&1
echo "Backup End: `date +%m-%d-%Y-%T`" >> /cydrive/c/$backup_log

10. vim remove_deleted.sh

#!/bin/bash

OIFS=$IFS
target_dir=/cygdrive/d
backup_dir=/cygdrive/e

IFS='
'
for file in $(find $backup_dir$target_dir)
do
        if ! [ -f "${file#$backup_dir}" -o -d "${file#$backup_dir}" ]
        then
                rm -rf "$file"
        fi
done

11. crontab -e

00 12 1 * * sh /home/dxu/remove_deleted.sh
55 23 * * * sh /home/dxu/backup.sh
#* * * * * echo "Cron test at $(date +\%k:\%M)" >> /cygdrive/c/crontest.txt 2>&1



Comments