annotate tidy-fspot-database @ 0:e85e3470a41e

Initial commit of useful scripts
author IBBoard <dev@ibboard.co.uk>
date Sat, 02 Aug 2014 19:31:02 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 #!/bin/bash
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 # A script to find missing files in the f-spot database, and then delete them.
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 # At present these files crash f-spot. It's frustrating as all hell.
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 echo "Welcome to the f-spot database cleaner. All the usual disclaimers apply, as you might imagine."
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 echo "What would you like to do: "
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 echo " 1) Run in demo-mode "
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 echo " 2) Clean up your f-spot database"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 echo " 3) Quit"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 read -p "Your choice: " choice
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 case $choice in
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 1) demomode="true";;
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 2) demomode="false";;
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 3) exit 0;;
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 esac
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 # With that beginning stuff out of the way, let us do some functions
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 # First, a function to gather the database contents and to print out the ones that are orphans
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 function findAndFixOrphans {
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 # find our db, and set a var. Checking for XDG path first, since it's the more recent location of the db
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 if [ -f $XDG_CONFIG_DIR/f-spot/photos.db ] #checks if the $XDG_CONFIG_DIR variable is in use
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 then
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 DBPATH=$XDG_CONFIG_DIR/f-spot/photos.db
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 elif [ -f $HOME/.config/f-spot/photos.db ] #uses the default $XDG location, if that's being used.
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 then
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 DBPATH=$HOME/.config/f-spot/photos.db
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 elif [ -f $HOME/.gnome2/f-spot/photos.db ] #uses the old location of the DB, if the former aren't in use.
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 then
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 DBPATH=$HOME/.gnome2/f-spot/photos.db
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 else
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 echo "Error: Could not find database. Damn."
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 exit 1
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 fi
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 # Select the filenames, and put them in a variable.
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 filenames=$(sqlite3 $DBPATH "SELECT base_uri || '/' || filename FROM PHOTOS")
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 filenames_versions=$(sqlite3 $DBPATH "SELECT base_uri || '/' || filename FROM PHOTO_VERSIONS")
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 # Chomp off the first instance of file://, and replace the rest with newlines.
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 filenames=$(echo $filenames | sed 's/file:\/\///' | sed 's/file:\/\//\n/g' )
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 filenames_versions=$(echo $filenames_versions | sed 's/file:\/\///' | sed 's/file:\/\//\n/g' )
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 if [ $demomode == "true" ]
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 then
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 while read -r line
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 do
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 # Decode the filename
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 decodedLine=$(echo -e "${line//\%/\\x}")
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 if [ ! -f "$decodedLine" ]
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 then
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 # If the file doesn't exist, we output the filename, if in demomode, or we fix it if we are not in demomode.
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 echo "Errant record found in the photos table: $decodedLine"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 fi
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 done <<< "$filenames"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 # We do the same for the photo_versions table
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 while read -r line
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 do
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 # Decode filename
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 decodedLine=$(echo -e "${line//\%/\\x}")
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 if [ ! -f "$decodedLine" ]
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 then
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 # If the file doesn't exist, we output the filename
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 echo "Errant record found in the photo_versions table: $decodedLine"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 fi
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 done <<< "$filenames_versions"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 else
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 # We backup the database, and make the correction
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73 cp $DBPATH $DBPATH.`date -I`.bak
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 if [ $? -eq 0 ]
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 then
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 #The backup worked, tell the user.
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 echo "Your database has been backed up to $DBPATH.`date -I`.bak"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 else
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 echo "Error backing up your database."
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 exit 3
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81 fi
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 # First we do the photos table
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 while read -r line
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 do
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 # Decode the filename
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 decodedLine=$(echo -e "${line//\%/\\x}")
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89 if [ ! -f "$decodedLine" ]
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 then
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91 # Do some sql here.
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
92 foo="file://${line}"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 base_uri=${foo%/*}
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 filename=${foo##*/}
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 echo -n "Deleting URI $line from the database table photos..."
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 sqlite3 $DBPATH "DELETE FROM PHOTOS WHERE base_uri = '$base_uri' AND filename = '$filename'"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97 echo "done."
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98 fi
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
99 done <<< "$filenames"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
100
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
101 # Then we do the photo_versions table
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
102 while read -r line
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
103 do
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
104 # Decode the filename
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
105 decodedLine=$(echo -e "${line//\%/\\x}")
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
106
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
107 if [ ! -f "$decodedLine" ]
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
108 then
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
109 #Do some sql stuff
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
110 foo="file://${line}"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
111 base_uri=${foo%/*}
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
112 filename=${foo##*/}
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
113 echo -n "Deleting URI $line from the database table photo_versions..."
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
114 sqlite3 $DBPATH "DELETE FROM PHOTO_VERSIONS WHERE base_uri = '$base_uri' AND filename = '$filename'"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
115 echo "done."
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
116 fi
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
117 done <<< "$filenames_versions"
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
118
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
119 fi
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
120
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
121 }
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
122
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
123 if [ "$demomode" == "true" ]
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
124 then
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
125 echo "Great. Proceeding in demomode."
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
126
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
127 findAndFixOrphans
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
128
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
129 echo "Demomode successfully finished. Exiting."
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
130 exit 0;
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
131 elif [ "$demomode" == "false" ]
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
132 then
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
133 echo "Great. Cleaning up your database."
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
134
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
135 findAndFixOrphans
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
136
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
137 echo "Database cleaned successfully."
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
138 exit 0;
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
139 else
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
140 echo "Something strange happened. See the script for details. Exiting."
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
141 exit 2;
e85e3470a41e Initial commit of useful scripts
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
142 fi