diff options
Diffstat (limited to '')
-rwxr-xr-x | scripts/gitAR.sh | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/scripts/gitAR.sh b/scripts/gitAR.sh new file mode 100755 index 0000000..2a067be --- /dev/null +++ b/scripts/gitAR.sh | |||
@@ -0,0 +1,118 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | # Work around OpenSims slow database corruption bug by using git to store all old backups. | ||
4 | # Try to squeeze every last byte out of the tarballs. Seems to cut the total storage size down to one third the size of just the raw I/OAR files. | ||
5 | # Saves even more if there's been no changes. | ||
6 | # On the other hand, these backup files will grow indefinately, the more changes, the faster it grows. I can live with that for more reliable backups that go back further. | ||
7 | # Tries to avoid loosing data if things go wrong. I think the main remaining problem would be running out of space, in which case you have bigger problems to deal with. | ||
8 | |||
9 | # Strategy - unpack the last one, unpack and commit any old I/OARs, pack up the result, delete it's working directory, THEN run the save i/oar. | ||
10 | # Avoids having to sync with OpenSim finishing the current I/OAR, and as a bonus, an easy to deliver latest I/OAR for people that want it. | ||
11 | |||
12 | # Not really meant to be called by users, so don't bother validating the input and such. | ||
13 | |||
14 | type=$1 | ||
15 | title=$2 | ||
16 | |||
17 | # Figure out where we are, most of this mess is to troll through soft links. | ||
18 | PRG="$0" | ||
19 | while [ -h "${PRG}" ] ; do | ||
20 | ls=$(ls -ld "${PRG}") | ||
21 | link=`expr "${ls}" : '.*-> \(.*\)$'` | ||
22 | if expr "${link}" : '.*/.*' > /dev/null; then | ||
23 | PRG="${link}" | ||
24 | else | ||
25 | PRG=$(dirname "${PRG}")/"${link}" | ||
26 | fi | ||
27 | done | ||
28 | PRGDIR=$(dirname "${PRG}") | ||
29 | pushd ${PRGDIR} >/dev/null | ||
30 | PRGDIR=$(pwd) | ||
31 | popd >/dev/null | ||
32 | |||
33 | date=$(date '+%F_%T') | ||
34 | |||
35 | # Sanitize the name. Not removing [ or ], couldn't get that to work, only important for Windows. | ||
36 | name=$(echo "${title}" | sed -e 's/[\\/:\*\?"<>\|@#$%&\0\x01-\x1F\x27\x40\x60\x7F. ]/_/g' -e 's/^$/NONAME/') | ||
37 | # Convert the type to uppercase. | ||
38 | gar="_git$(echo -n ${type} | tr '[:lower:]' '[:upper:]')AR" | ||
39 | |||
40 | if [ -d ${PRGDIR}/../../backups/temp_backup${type}_${name} ]; then | ||
41 | echo "WARNING - Mess left over from last backup, not gonna run!" | ||
42 | mv ${PRGDIR}/../../backups/temp_backup${type}_${name}/*.oar ${PRGDIR}/../backups | ||
43 | exit 1 | ||
44 | fi | ||
45 | |||
46 | mkdir -p ${PRGDIR}/../../backups/temp_backup${type}_${name} | ||
47 | pushd ${PRGDIR}/../../backups/temp_backup${type}_${name} >/dev/null | ||
48 | if [ -f ../${name}${gar}.tar.xz ]; then | ||
49 | nice -n 19 tar -xf ../${name}${gar}.tar.xz | ||
50 | else | ||
51 | mkdir -p ${name}${gar} | ||
52 | git init ${name}${gar} >log | ||
53 | fi | ||
54 | |||
55 | pushd ${name}${gar} >/dev/null | ||
56 | |||
57 | # Make sure stuff that's already compressed doesn't get compressed by git. | ||
58 | # Also tries to protect binaries from mangling. | ||
59 | cat >.gitattributes <<- zzzzEOFzzzz | ||
60 | *.bvh -delta -diff -text | ||
61 | *.jp2 -delta -diff -text | ||
62 | *.jpg -delta -diff -text | ||
63 | *.llmesh -delta -diff -text | ||
64 | *.ogg -delta -diff -text | ||
65 | *.png -delta -diff -text | ||
66 | *.r32 -delta -diff -text | ||
67 | *.tga -delta -diff -text | ||
68 | zzzzEOFzzzz | ||
69 | git add .gitattributes &>>../log | ||
70 | # Coz git insists. | ||
71 | git config user.email "opensim@$(hostname -A | cut -d ' ' -f 1)" | ||
72 | git config user.name "opensim" | ||
73 | |||
74 | # Looping through them in case there's a bunch of I/OARs from previous versions of this script. | ||
75 | find ../.. -maxdepth 1 -type f -name ${name}-*.${type}ar | sort | while read file; do | ||
76 | # Deal with deletions in the inventory / sim, easy method, which becomes a nop for files that stay in the git add below. | ||
77 | git rm -fr * &>>../log && \ | ||
78 | nice -n 19 tar -xzf "${file}" || echo "ERROR - Could not unpack ${file} !" >>../errors | ||
79 | if [ ! -f ../errors ]; then | ||
80 | git add * &>>../log && git add */\* &>>../log | ||
81 | # Magic needed to figure out if there's anything to commit. | ||
82 | # After all the pain to get this to work, there's an ever changing timestamp in archive.xml that screws it up. | ||
83 | # Like this system didn't have enough timestamps in it already. lol | ||
84 | # TODO - I could sed out that timestamp, and put it back again based on the OAR file name when extracting. | ||
85 | # IARs don't seem to have the timestamp. | ||
86 | if t=$(git status --porcelain) && [ -z "${t}" ]; then | ||
87 | true | ||
88 | else | ||
89 | # Note this commit message has to be just the file name, as the ungitAR script uses it. | ||
90 | git commit -qm "$(basename ${file})" &>>../log || echo "ERROR - Could not commit ${file} !" >>../errors | ||
91 | fi | ||
92 | if [ ! -f ../errors ]; then | ||
93 | mv ${file} .. | ||
94 | fi | ||
95 | fi | ||
96 | if [ -f ../errors ]; then | ||
97 | exit 1 # Seems to only exit from this loop, not the script. Makes me want to rewrite this in a real language. lol | ||
98 | fi | ||
99 | done | ||
100 | |||
101 | #git gc --aggressive --prune=now # Takes a long time, doesn't gain much. Even worse, it increases the size of the resulting tarball. lol | ||
102 | |||
103 | popd >/dev/null | ||
104 | |||
105 | if [ ! -f errors ]; then | ||
106 | XZ_OPT="-9e" nice -n 19 tar -c --xz ${name}${gar} -f ../${name}${gar}.tar.xz || echo "ERROR - Could not pack gitAR!" >>errors | ||
107 | fi | ||
108 | |||
109 | popd >/dev/null | ||
110 | |||
111 | if [ -f ${PRGDIR}/../../backups/temp_backup${type}_${name}/errors ]; then | ||
112 | echo "NOT cleaning up coz - " | ||
113 | cat ${PRGDIR}/../../backups/temp_backup${type}_${name}/errors | ||
114 | else | ||
115 | rm -fr ${PRGDIR}/../../backups/temp_backup${type}_${name} | ||
116 | fi | ||
117 | |||
118 | echo -n ${name} | ||