4 # Written by Stein Magnus Jodal <stein.magnus@jodal.no>
5 # Distributed under the GNU General Public License version 2.
8 # 2003-10-20 Stein Magnus Jodal
9 # - Started, but I was too tired and went home.
11 # 2003-10-22 Stein Magnus Jodal
12 # - Continued, but I started to chat instead.
14 # 2003-10-27 Stein Magnus Jodal
15 # - The script started looking like something usefull.
17 # 2003-11-10 Stein Magnus Jodal
18 # - The script can now delete old archives!
20 # 2004-10-17 Stein Magnus Jodal
21 # - The script now creates the storagedirs if they don't exists.
22 # - The delete old archives part doesn't fail if we don't have any old archives
23 # anymore. (shopt -s nullglob, so the script now requires bash)
24 # - Fixed s/:/;/ at one of the lines. This made the first backup run use
25 # "full:" instead of "full" as target dir.
27 # 2004-10-18 Stein Magnus Jodal
28 # - Fixed the calculation of $LIMIT and added a new check under the deletion
32 # skruebackup.sh -full
35 # skruebackup.sh -incr
36 # Run incremental backup (default if the lastrun file exists)
38 # Example for crontab usage:
39 # 0 4 1 * * /path/to/skruebackup.sh -full
40 # 0 6 * * * /path/to/skruebackup.sh -incr
43 # Number of archives to keep
44 NUMFULL=3 # months (31 days)
46 # Dir to store the backups to
47 STORAGEDIR="/backup/skrue";
48 # File listing what dirs to backup
49 BACKUPLIST="/home/backup/skruebackup_this.txt";
50 # File touched for every backup
51 LASTRUNFILE="${STORAGEDIR}/skruebackup_lastrun";
53 SMBTAR="/usr/bin/smbtar -s skrue -x tilkolje -u skruebackup -p 'gl25bE!v' -t -";
56 TODAY=$(date +%Y-%m-%d);
57 TODAY_EPOCH=$(date +%s);
59 ### DON'T EDIT ANYTHING BELOW THIS LINE ###
61 # If we got empty dirs, we don't want dir/* to be passed on
65 if [ ! -d ${STORAGEDIR} ]; then
66 echo "--- Creating backup dir";
67 mkdir -vp ${STORAGEDIR}/full ${STORAGEDIR}/incr;
70 ### Delete old archives
71 echo "--- Deleting old archives";
73 # Deleting old full archives
74 LIMIT=$(( ${TODAY_EPOCH} - ${NUMFULL} * 31 * 24 * 60 * 60 ));
76 for FILE in ${STORAGEDIR}/full/*; do
77 if [ -e ${FILE} ]; then
78 THIS_DATE=$(echo ${FILE} | sed 's/^.*_//; s/\.tar\.gz$//');
79 THIS_EPOCH=$(date --date="${THIS_DATE}" +%s);
80 if [ "${THIS_EPOCH}" -lt "${LIMIT}" ]; then
86 # Delete old incremental archives
87 LIMIT=$(( ${TODAY_EPOCH} - ${NUMINCR} * 24 * 60 * 60 ));
89 for FILE in ${STORAGEDIR}/incr/*; do
90 if [ -e ${FILE} ]; then
91 THIS_DATE=$(echo ${FILE} | sed 's/^.*_//; s/\.tar\.gz$//');
92 THIS_EPOCH=$(date --date="${THIS_DATE}" +%s);
93 if [ "${THIS_EPOCH}" -lt "${LIMIT}" ]; then
99 ### Full or incremental backup
100 if [ "$1" = "-full" ]; then
101 echo "--- Running full backup";
102 STORAGEDIR="${STORAGEDIR}/full";
104 if [ -w ${STORAGEDIR}/skruebackup_lastrun ]; then
105 echo "--- Running incremental backup";
106 STORAGEDIR="${STORAGEDIR}/incr";
107 SMBTAR="${SMBTAR} -N ${LASTRUNFILE} -i";
109 echo "--- Running full backup";
110 STORAGEDIR="${STORAGEDIR}/full";
115 for DIR in $(cat ${BACKUPLIST}); do
116 echo ">>> Currently processing ${DIR}";
117 ${SMBTAR} -d ${DIR} | ${GZIP} > ${STORAGEDIR}/skruebackup_${DIR}_${TODAY}.tar.gz;
120 ### Updating last runned record
121 echo "--- Updating last runned record";
122 touch ${LASTRUNFILE};