This script archives an important folder with 7z while saving the 7z across to another location. A windows task scheduler is setup to run this weekly for me. (I dont cover how to schedule a task but this does: http://windows.microsoft.com/en-us/windows7/schedule-a-task this shows the Basic Task, I setup an Advanced Task, to each his own way)
The Main script actually has all of the instructions such that you dont actually need to read this article – however it doesnt hurt to read – one thing this article talks about that the instructions in the script dont talk about is the importance of correct line endings, which I will cover later.
Let begin:
Basically you need to make 3 scripts, 2 .sh scripts and 1 bat windows script… The bat script (Script #3) is what we will run from windows to start the backup job, and if you in Cygwin you just start the main script (Script #1) for the job to start, but if you want everything to be logged you start the starter linux script (Script #2)
(Note the 3 files below exist once each, there are just 2 ways to get to them from Windows, or from the Cygwin app, the cygwin app has a linux style POSIX way to get to them)
1. MAIN SCRIPT: linux shell script that starts the 7zip program that backs up everything: /cygdrive/g/WeeklyBackups/7zipbackup.sh
In Windows: G:\WeeklyBackups\start-7z-backup.bat
2. linux shell script that starts number 1: /cygdrive/g/WeeklyBackups/7zipstarter.sh
In Windows: G:\WeeklyBackups\start-7z-backup.bat
3. windows bat script that starts number 3: /cygdrive/g/WeeklyBackups/start-7z-backup.bat
In Windows: G:\WeeklyBackups\start-7z-backup.bat
For Windows you must have CYGWIN to be able to run this, also install 7zip on CYGWIN and on your WINDOWS machine
I got the instruction on how to run this using this link:
Except notice in his example he uses WINDOWS locaitons, I had to use cygwin/linux locations So a folder like D:\Dropbox in windows becomes /cygdrive/d/Dropbox through cygwin…
Note this script will also work in linux, just you dont need the #3 script (the windows bat script) you just need #1 and #2, (you can even make do without #2 if you wanted to) you would just need to make a cronjob. Google cronjobs. and I would recommend to make a cronjob as a root user that starts either #1 or #2. Remember we need to start #1, #2 starts #1, so its okay to start #2… Likewise in the windows scenario, #3 starts #2 starts #1, so in the end #1 always runs.
When testing these start from #1 and go to #2 then to #3
One more important note, and this one is about line endings, Cygwins scripts since it emulates linux need to have linux line endings, and the windows scripts (.bat file) needs to have Windows line endings:
7zipbackup.sh – Make sure this has line endings in LF
7zipstarter.sh – Make sure this has line endings in LF
start-7z-backup.bat – Make sure this has line endings in CR+LF
How to? Line endings can be converted to the right format with Notepad++ just go Edit->EOL Conversion->Unix for LF and for CR+LF click on Dos or Windows), to see if you have the right line endings go to the View menu->Show Symbol->Show all Characters
Any how here is the script:
#!/bin/bash
# 7zip BACKUP
# SAVING THIS BACKUP TO:
# /cygdrive/g/WeeklyBackups/7zipbackup.sh
# * NOTE MAKE THIS FILE IN VIM ON CYGWIN SO THAT EVERY LINE ENDS IN “LF” AND NOT “CR-LF”
# * YOU CAN SEE THE ENDING WITH NOTEPAD++ -> VIEW-> SHOW SYMBOL -> END OF LINE
# * TO GET LF YOU NEED TO MAKE FILE IN LINUX OR VIM/VI/NANO IN VYGWIN
# * IF YOU MADE IT IN NOTEPAD++ ENDING WILL BE IN CRLF BUT YOU CAN CONVERT IT JUST GOOGLE HOW
# * GOOGLE HOW TO CONVERT CRLF TO LF WITH NOTEPAD++ IF YOUR SCRIPT IS ENDING IN CRLF
######################################################
# ITS LIKE THIS: 7za a -t7z [7zip file to make] [what to make it with folders files]
######################################################
# SRCDIR ABOVE IS THE SRC DIRECTORY
# FINAL FILE IS DESTDIR/DFILEPREFIX-DATE.7z
# NOTE NEED 2 STARTERS 1 4 LINUX 1 4 WINDOWS
# WINDOWS STARTS LINUX STARTER (WHICH LOGS)
#####################################################
# SO 3 FILES ALL IN /cygdrive/g/WeeklyBackups
# THIS FILE – WHICH RUNS THE 7z BACKUP: 7zipbackup.sh
# LINUX STARTER WHICH LOGS: 7zipstarter.sh
# WINDOWS STARTER WHICH STA.RTS LINUX STARTER: start-7z-backup.bat
#####################################################
# TO START FROM LINUX CAN JUST RUN ./7zipbackup.sh
# BUT INSTEAD MAKE THIS SCRIPT 7zipstarter.sh:
# SCRIPT7ZIP=”/cygdrive/g/WeeklyBackups/7zipbackup.sh”
# LOGFILE=”/cygdrive/g/WeeklyBackups/_Manual/log/log-`date +D%D-T%T | tr :/ –`.txt”
# echo “RUNNING: ${SCRIPT7ZIP} |& tee -a ${LOGFILE}”
# ${SCRIPT7ZIP} |& tee -a ${LOGFILE}
######################################################
# TO START FROM WINDOWS – MAKE THIS BAT SCRIPT: start-7z-backup.bat
# D:\cygwin\bin\bash.exe -l -c “/cygdrive/g/WeeklyBackups/7zipstarter.sh”
######################################################
# TO EDIT THIS FOR YOURSELF JUST EDIT THE start-7z-backup.bat to point at your script and log directory that already exists
# ALSO EDIT THE 3 VARIABLES BELOW SRCDIR DESTDIR and DFILEPREFIX
######################################################
# WHEN DONE MAKE A TASK SCHEDULER TO RUN THIS AS OFTEN AS YOU WANT
# I WOULD TIME THIS FIRST TO SEE HOW LONG THIS PROCESS TAKES
######################################################
SRCDIR=”/cygdrive/d/skydrive/school/”
DESTDIR=”/cygdrive/g/WeeklyBackups/_Manual”
DFILEPREFIX=”school-Backup-From-skydrive”
#######################################################
D8=`date +D%D-T%T | tr :/ –`
DESTFILE=${DFILEPREFIX}-${D8}
DFULL=${DESTDIR}/${DESTFILE}.7z
echo “Backing up with 7zip (7za – linux): ${SRCDIR}”
echo “Going to make archive: ${DFULL}”
echo “ACTIVIATING DISK 15 SECONDS”
touch ${DESTDIR}/test1
sleep 1
touch ${DESTDIR}/test2
sleep 2
touch ${DESTDIR}/test3
sleep 3
touch ${DESTDIR}/test4
sleep 4
touch ${DESTDIR}/test5
sleep 5
rm ${DESTDIR}/{test1,test2,test3,test4,test5}
echo “Initiating command: 7za a -t7z ${DFULL} ${SRCDIR}”
7za a -t7z ${DFULL} ${SRCDIR}