Wiki‎ > ‎

Setup Crontab/Rsync Automatic Backup on Windows

posted Dec 29, 2015, 1:54 PM by Dong Xu   [ updated Oct 24, 2017, 2:39 PM ]
The latest technology is to use WSL, crontab, and rsync:

https://stackoverflow.com/questions/41281112/crontab-not-working-with-bash-on-ubuntu-on-windows

Need to add yourself to the crontab group.
usermod -a -G crontab (username)

Once you have done this, you also need to make sure that cron is running. Usually this is started with start cron however upstart does not work on WSL from what I can tell, but sudo cron does the job.

Restart your bash session for this to take effect.

One caveat to this is that once you close all bash windows, cron will stop running even though your computer runs. However, as long as you have a bash window open and cron running, it will perform as expected.

Also, if you want cron and other background jobs to keep running after you close all bash windows, you can use this vb script at start up, and it keeps a hiden instance of bash running. gist.github.com/leonelsr/cde77574519eb1fd672bc9690e01257e

To auto-start cron I added sudo cron to my ~/.bashrc file and myuser ALL=(ALL:ALL) NOPASSWD:/usr/sbin/cron to my /etc/sudoers file.


Use windows copy the1st time backup and rsync for later incremental

Used cwRsync version 3.12 (less buggy), install all files to ~/bin,
and copy its cygwin1.dll to /bin for this new version to work. Need to rename the old
cygwin1.dll.

set up mobaxterm cron server with this script for nightly backup. Run crontab -e

55 23 * * * sh /home/mobaxterm/backup.sh
00 12 1 * * sh /home/mobaxterm/remove_deleted.sh
#* * * * * date >>  /cygdrive/e/crontest.txt 2>&1

#!/bin/bash

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

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

=================

#!/bin/bash

OIFS=$IFS
target_dir=/cygdrive/d
backup_dir=/cygdrive/f/Backup

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

OIFS=$IFS
target_dir=/cygdrive/e
backup_dir=/cygdrive/f/Backup

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




=================================
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