Bladeren bron

Improve the Friendica backup script to handle database errors

Bob Mottram 10 jaren geleden
bovenliggende
commit
b1bf0b8a7e
1 gewijzigde bestanden met toevoegingen van 39 en 5 verwijderingen
  1. 39
    5
      beaglebone.txt

+ 39
- 5
beaglebone.txt Bestand weergeven

@@ -4226,31 +4226,65 @@ Make sure that the database gets backed up.  By using cron if anything goes wron
4226 4226
 editor /etc/cron.daily/backup
4227 4227
 #+END_SRC
4228 4228
 
4229
-Enter the following
4229
+Enter the following, replacing /myusername@mydomainname.com/ with your email address.
4230 4230
 
4231 4231
 #+BEGIN_SRC: bash
4232 4232
 #!/bin/sh
4233 4233
 
4234
-# stop the web server to avoid any changes to the databases during backup
4235
-service apache2 stop
4234
+EMAIL=myusername@mydomainname.com
4236 4235
 
4237 4236
 MYSQL_PASSWORD=<mysql root password>
4238 4237
 umask 0077
4239 4238
 
4239
+# stop the web server to avoid any changes to the databases during backup
4240
+service apache2 stop
4241
+
4242
+# Save to a temporary file first so that it can be checked for non-zero size
4243
+TEMPFILE=/tmp/friendicared.sql
4244
+
4240 4245
 # Backup the database
4241
-mysqldump --password=$MYSQL_PASSWORD friendica > /var/backups/friendica_daily.sql
4246
+mysqldump --password=$MYSQL_PASSWORD friendica > $TEMPFILE
4247
+FILESIZE=$(stat -c%s $TEMPFILE)
4248
+if [ "$FILESIZE" -eq "0" ]; then
4249
+    # restart the web server
4250
+    service apache2 start
4251
+
4252
+	# Send a warning email
4253
+	echo "Unable to create a backup of the Friendica database" | mail -s "Friendica backup" $EMAIL
4254
+
4255
+    exit 1
4256
+fi
4257
+
4258
+chmod 600 $TEMPFILE
4259
+mv $TEMPFILE /var/backups/friendica_daily.sql
4242 4260
 
4243 4261
 # Make the backup readable only by root
4244 4262
 chmod 600 /var/backups/friendica_daily.sql
4245 4263
 
4246 4264
 # Backup the database
4247
-#mysqldump --password=$MYSQL_PASSWORD redmatrix > /var/backups/redmatrix_daily.sql
4265
+#mysqldump --password=$MYSQL_PASSWORD redmatrix > $TEMPFILE
4266
+
4267
+FILESIZE=$(stat -c%s $TEMPFILE)
4268
+if [ "$FILESIZE" -eq "0" ]; then
4269
+    # restart the web server
4270
+    service apache2 start
4271
+
4272
+	# Send a warning email
4273
+	echo "Unable to create a backup of the Red Matrix database" | mail -s "Red Matrix backup" $EMAIL
4274
+
4275
+    exit 2
4276
+fi
4277
+
4278
+chmod 600 $TEMPFILE
4279
+mv $TEMPFILE /var/backups/redmatrix_daily.sql
4248 4280
 
4249 4281
 # Make the backup readable only by root
4250 4282
 #chmod 600 /var/backups/redmatrix_daily.sql
4251 4283
 
4252 4284
 # restart the web server
4253 4285
 service apache2 start
4286
+
4287
+exit 0
4254 4288
 #+END_SRC
4255 4289
 
4256 4290
 Save and exit.