David Cummings wrote: ↑Thu Nov 08, 2018 9:43 am
What I want to do is to be able to, at midnight every night for the RPI to perform a cron job that will -
1. Backup the MYSQL database for the forum.
1.1 Upload this backup to a file on the NAS - \backups\Elite\RPI-MYSQL\<foldername as date and time> (e.g. 2018-11-08)
2. Backup any files uploaded, to the NAS - \backups\Elite\RPI-STORE\<foldername as date and time> (e.g. 2018-11-08)
3. Email me when it's done.
The RPI will be called "Elite" on ip address ending 0.2
The external storage space is a NAS box, on ip ending 0.3
Can anyone point me in the right direction?
A couple of notes:
1) I assume that you know that you should use mysqldump to make a valid backup of MySQL database -
not just copy the database files. The latter
may work if you can guarantee that no-one is doing anything that affects the database while you are copying it, but is not recommended.
2) The following crontab lines implement a backup system (using some short Python scripts) which create zipped backups of SQLite files containing my weather data every day, with the daily data kept for a week, the weekly data kept for a month, and the monthly data kept indefinitely. I do just zip the 'raw' files because the software which writes to them only does so (for a few seconds) every 5 minutes, so I know that they are not otherwise being accessed on odd minutes.
Code: Select all
2 23 * * * sh /home/pi/weather/DBackup.sh
1 23 8,15,22 * * sh /home/pi/weather/WBackup.sh
1 23 1 * * sh /home/pi/weather/MBackup.sh
The following is the script that does the daily backup. The other scripts are very similar, except that they don't need or have any tests to avoid the days when the other levels of backup run (cron handles this) and they create appropriate names:
Code: Select all
import datetime
import zipfile
import os
import time
# save temp/humidity and weather data as archive to NAS backup area
# version to run daily (except on days 1,8,15,22 when weekly and monthly
# backups run)
thisDate = datetime.date.today()
thisDay = thisDate.day
if ((thisDay % 7) != 1) or (thisDay == 29):
outFile = "/mnt/mycloud/TBackup/daily/" + thisDate.strftime("%A") + ".zip"
testFile = "/mnt/mycloud/TBackup/TestFile.txt"
try:
truth = os.path.getsize(testFile) > 0
except:
time.sleep(30)
# if the file existence test times out because the disk needs too much time to spin up, the sleep will allow it to do so before
# the next line tries to read or create the real output file. If it fails for a 'hard' reason, the next line will fail, too,
# and the script will be aborted
myFile = zipfile.ZipFile(outFile, 'w')
myFile.write("data.db", compress_type=zipfile.ZIP_DEFLATED)
myFile.write("/var/lib/weewx/weewx.sdb", arcname="weewx.sdb",compress_type=zipfile.ZIP_DEFLATED)
myFile.close()