You can host your Django app effortlessly on PythonAnyWhere server. If you are using the database in your app then it is strongly recommended to take backup of database to avoid loss of data.
This PythonAnyWhere article explain the process to take sql dump. We will extend the same article to take database backup periodically and delete the old files.
As explained in the article we can use below command to take backup.
mysqldump -u yourusername -h yourusername.mysql.pythonanywhere-services.com 'yourusername$dbname' > db-backup.sql
We need to run this command in home directory.
To take the backup periodically, we will write a small script.
First lets create a directory in home where all the backup files will reside. Lets name is
. Now in script, define the variables like backup directory name, file prefix and file suffix etc.
To distinguish, we will append the time stamp in
yyyymmddHHMMSS ormat in file name.
Create the file name and run the backup command using os package of python.
from zipfile import ZipFile
BACKUP_DIR_NAME = "mysql_backups"
FILE_PREFIX = "my_db_backup_"
FILE_SUFFIX_DATE_FORMAT = "%Y%m%d%H%M%S"
USERNAME = "username"
DBNAME = USERNAME+"$dbname"
# get today's date and time
timestamp = datetime.datetime.now().strftime(FILE_SUFFIX_DATE_FORMAT)
backup_filename = BACKUP_DIR_NAME+"/"+FILE_PREFIX+timestamp+".sql"
os.system("mysqldump -u "+USERNAME+" -h "+USERNAME+".mysql.pythonanywhere-services.com '"+DBNAME+"' > "+backup_filename)
We can test this script by running using the python installed in virtual environment. I strongly recommend to use virtual environment for all your python and Django projects.
Now to save some space we will zip the sql file. For this we will use zipfile python package.
# creating zip file
zip_filename = BACKUP_DIR_NAME+"/"+FILE_PREFIX+timestamp+".zip"
with ZipFile(zip_filename, 'w') as zip:
Now when sql file has been added to zip file, we may delete it.
Now we can schedule this script from tasks tab.
Now lets extend the script to delete old files. Define a variable which signifies for how many days we need to keep the backup.
DAYS_TO_KEEP_BACKUP = 3
So for now we will keep last three backups.
Now there are two ways to delete the old files. Either use the
os.stat() function to check when was a file created and if it is older than
DAYS_TO_KEEP_BACKUP, delete it.
for f in os.listdir(path):
if os.stat(os.path.join(path,f)).st_mtime < now - 3 * 86400:
Or we use the below logic.
# deleting old files
list_files = os.listdir(BACKUP_DIR_NAME)
back_date = datetime.datetime.now() - datetime.timedelta(days=DAYS_TO_KEEP_BACKUP)
back_date = back_date.strftime(FILE_SUFFIX_DATE_FORMAT)
length = len(FILE_PREFIX)
# deleting files older than DAYS_TO_KEEP_BACKUP days
for f in list_files:
filename = f.split(".")
if "zip" == f.split("."):
suffix = filename[length:]
if suffix < back_date:
print("Deleting file : "+f)
os.remove(BACKUP_DIR_NAME + "/" + f)
First get the list of all files in backup directory. Then for each file in list, check if file extension is 'zip' and then compare the file time stamp suffix with the back date.
We can always use
os.path.splitext() function to get filename and extension. Its up to you to use whichever way you feel good. Feel free to tweak the script and experiment.
>>> filename, file_extension = os.path.splitext('/path/to/somefile.ext')
Restoring database backup:
You can restore the database backup using below command.
mysql -u yourusername -h yourusername.mysql.pythonanywhere-services.com 'yourusername$dbname' < db-backup.sql
Complete code of the above script is available on github.