]> git.sesse.net Git - backup.sh/blob - skruebackup.sh
Englishify a bit.
[backup.sh] / skruebackup.sh
1 #! /bin/bash
2
3 ### About
4 # Written by Stein Magnus Jodal <stein.magnus@jodal.no>
5 # Distributed under the GNU General Public License version 2.
6
7 ### ChangeLog
8 # 2003-10-20 Stein Magnus Jodal
9 # - Started, but I was too tired and went home.
10 #
11 # 2003-10-22 Stein Magnus Jodal
12 # - Continued, but I started to chat instead.
13
14 # 2003-10-27 Stein Magnus Jodal
15 # - The script started looking like something usefull.
16 #
17 # 2003-11-10 Stein Magnus Jodal
18 # - The script can now delete old archives!
19 #
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.
26 #
27 # 2004-10-18 Stein Magnus Jodal
28 # - Fixed the calculation of $LIMIT and added a new check under the deletion
29 #   part
30
31 ### Usage
32 # skruebackup.sh -full
33 #       Run full backup
34 #
35 # skruebackup.sh -incr
36 #       Run incremental backup (default if the lastrun file exists)
37 #
38 # Example for crontab usage:
39 #       0 4 1 * * /path/to/skruebackup.sh -full
40 #       0 6 * * * /path/to/skruebackup.sh -incr
41
42 ### Vars
43 # Number of archives to keep
44 NUMFULL=3 # months (31 days)
45 NUMINCR=40 # 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";
52 # Programs
53 SMBTAR="/usr/bin/smbtar -s skrue -x tilkolje -u skruebackup -p 'gl25bE!v' -t -";
54 GZIP="/bin/gzip -6";
55 # Dates
56 TODAY=$(date +%Y-%m-%d);
57 TODAY_EPOCH=$(date +%s);
58
59 ### DON'T EDIT ANYTHING BELOW THIS LINE ###
60
61 # If we got empty dirs, we don't want dir/* to be passed on
62 shopt -s nullglob
63
64 ### Create dirs
65 if [ ! -d ${STORAGEDIR} ]; then
66         echo "--- Creating backup dir";
67         mkdir -vp ${STORAGEDIR}/full ${STORAGEDIR}/incr;
68 fi
69
70 ### Delete old archives
71 echo "--- Deleting old archives";
72
73 # Deleting old full archives
74 LIMIT=$(( ${TODAY_EPOCH} - ${NUMFULL} * 31 * 24 * 60 * 60 ));
75
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
81                         rm -v ${FILE};
82                 fi
83         fi
84 done
85
86 # Delete old incremental archives
87 LIMIT=$(( ${TODAY_EPOCH} - ${NUMINCR} * 24 * 60 * 60 ));
88
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
94                         rm -v ${FILE};
95                 fi
96         fi
97 done
98
99 ### Full or incremental backup
100 if [ "$1" = "-full" ]; then
101         echo "--- Running full backup";
102         STORAGEDIR="${STORAGEDIR}/full";
103 else
104         if [ -w ${STORAGEDIR}/skruebackup_lastrun ]; then
105                 echo "--- Running incremental backup";
106                 STORAGEDIR="${STORAGEDIR}/incr";
107                 SMBTAR="${SMBTAR} -N ${LASTRUNFILE} -i";
108         else
109                 echo "--- Running full backup";
110                 STORAGEDIR="${STORAGEDIR}/full";
111         fi
112 fi
113
114 # The actual backup
115 for DIR in $(cat ${BACKUPLIST}); do
116         echo ">>> Currently processing ${DIR}";
117         ${SMBTAR} -d ${DIR} | ${GZIP} > ${STORAGEDIR}/skruebackup_${DIR}_${TODAY}.tar.gz;
118 done
119
120 ### Updating last runned record
121 echo "--- Updating last runned record";
122 touch ${LASTRUNFILE};
123