crontab

last edited Sat, 20 Jul 2024 11:31:53 GMT
backlinks: null


setting up a crontab direct link to this section

crontab -e
	
# .---------------- minute (0 - 59) 
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ... 
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat 
# |  |  |  |  |
# *  *  *  *  *  command to be executed
#
# m h  dom mon dow   command
0 3 * * * /bin/bash /root/backup.sh

The crontab will run a backup script. The example above is set to 3am daily, see the man pages for additional information on using crontab.

backup.sh direct link to this section

#!/bin/bash
#this must run as root !
if [ "$EUID" -ne 0 ]
then
        echo 'MUST RUN AS ROOT!'
        exit
fi

cd /tmp/

cooldate=$(date --iso-8601)
echo $cooldate
rm backup*.zip
rm backup-$cooldate.zip
zip -r backup-$cooldate.zip /root/myveryimportantdirectory/
rsync backup-$cooldate.zip root@192.168.0.150:/root/backups/
rm backup*.zip

cd -

Remember to change permissions chmod +x backup.sh, test with ./backup.sh