how to backup and restore data of an applications database? - android

I'm developing an application which will do the backup of Application's database to the Google servers and restore the data after reinstalling the application.
I didn't find any reference to do this task. I also tried the application given in Android SDK, which is working only with the Shell commands, but not by running the application.
Could anybody is having an idea how to do the backup and restore of the Application's database data?

Android has a "backup data" service that can help you in this particular case. You can find documentation here. I think it can solve your problem, but I didn't use it, so I can't describe the process.
You have also a good question in StackOverflow about backup/restore an SQLite internal DB. Just have a look at this link.

SQLite's backup API works only where the backup is another file that can be accessed directly (for example, if you want to create a backup on the SD card), so it cannot be used when the backup goes "into the cloud".
Ensure that there is no active transaction, then backup the database file just as you would backup any other file.

Related

Under what circumstance will the SQLite database file got lost or destroyed?

I know that one instance where an App's SQLite file may be removed from the System is when the user uninstall the Application. Apart from this situation, under what circumstance(s) will the SQLite file get lost or be destroyed in the Android System?
Again, under what circumstance will the SQLite database file loose all it's content even if the App is not uninstalled?
Will there be an instance where the Android System does this?
Settings -> App -> Data -> Clear Data would also delete the SQL. Also rooted phone can do whatever he likes with the SQLite files.
There is no way that the Android system will erase or remove your file. Database files are stored, along with other app data, under {package_name}/data/data folder so no one except your app can touch it (ignore rooted devices).
Only the following can happen:
1. your code cleared/corrupted the db file
2. user has cleared app data from device settings

How to securely delete DB on Android?

My app uses SQLite DB in Android to keep some logs. I understand this is saved in my app dir on Android as DBName.db. I need a way to truncate/delete/damage/empty the database so it is unrecoverable.
What is the best approach?
Does Android keep some shadow copies on the device?
Does Android keep synced backup in cloud by default (like settings are kept)?
If the app is running, is the whole db cached in RAM? If so, id like to clean that too.
Thanks :)
To overwrite all data, enable the secure_delete option, and DELETE FROM all your tables.
Android does not make copies of (database) files unless you explicitly configure it to do so.
There is a RAM cache.
The only safe way to clear it is to exit your own process.

Locking SQLite database file during backup

I am trying to implement a service to backup the SQLite database of my Android app. I am planning to both schedule this service for frequent backups (every day for example), and add an option to launch it immediately.
My problem is that the service might start while the application is running, or the user might start the application while the backup is in progress. And they may write to the database while I am copying it.
Is there any way to make sure that the copy and write will not run concurrently, without adding synchronization locks to all my queries ?
Thanks !
If you are not using explicit transactions, SQLite will automatically use a transaction around each SQL statement.
To ensure that the database files cannot be accessed by another database connection while you are doing the backup, open an exclusive transaction around the backup.
SQLite site has some notes on doing hot backup on a running database. See the Example 2 in that page.
In android, if you want to initiate a file copy of your sqlite db file, you will first need to get a shared lock as mentioned above, but this approach has shortcomings.
Ideally, you would want to use the sqlite3_backup_* apis.
These APIs are not available in standard android sqlite API, but it is easy to copy the sqlite jni code to your project, and expose these additional features. The advantage with this approach is that you dont have to change existing API calls in your code, as it mirrors existing android sqlite API definitions.
To expose backup APIs, take a look at android_database_SQLiteConnection.cpp to see how existing JNI functions call the native sqlite_* APIs.
Another option is to use something like sqlite4java, it has the sqlite backup APIs wrapped in as Java APIs, and seems the latest version supports Android.

Logger inside android based on database

I am working on logs inside android, I thought two ways for storing logs, and one is on external directory as a text file or a log file while other is to store in database. I found database method more useful in my case. My question is if I UN-install and reinstall the app will the database will be affected? In case of yes what should I do? I cannot place the logs online. How to take the backup or safe that database so it won’t be affected in case of UN- installation.
My question is if I uninstall and reinstall the app will the database will be affected?
Yes, your database will by default be stored in the application's data directory, which is deleted along with your application on uninstallation.
You can instead write a file of a filetype of your choosing (whether that's a simple text file, or a database file) to the external storage. You can obtain the directory path using:
Environment.getExternalStoragePublicDirectory(type) for obtaining a directory path of files of a specific type, such as images or videos;
Environment.getExternalStorageDirectory() for obtaining the primary external storage directory, under which you could create a new path.
I would nevertheless discourage you from doing this, because it would require your users to manually dispose of any files after uninstalling your application. Perhaps you should reconsider the justification of choosing for this solution.
If you uninstall the app, then the database (and all other data stored in the apps private storage for that matter) will be gone.
You could store your logs in the public external storage, but this will expose your logs to other applications as well as the user.
One possible approach could be to use application private storage for your 'live' logs, and make periodic backups to the public storage. In case of a new installation, you can check your designated backup location and attempt to restore previous logs from the backup.

Backup/Restore database file from device to website?

I've done a small school project to manage a library with several books, using SQLite and android 2.2. Later on, they asked me to create a back office system, which I wasn't ready for. So I'm trying to achieve a simple backup system that connects to the internet.
I would like directions or hints to:
Get the project database from data/data/package_name/databases/school
Send it to a online repository, could be just a ftp connection to upload school_%date% and latest.db (same contents)
and to restore:
Download latest file from server, something like http://www.site.com/school/latest.db
Replace data/data/package_name/databases/school with the downloaded latest.db
I been reading about SOA and REST approaches but I couldn't implement that on the trivial server I own... any other advices? /cheers
SOA or REST would be the most pretty way to do it. The easy way is just to get the database file and send it to you webserver. But I don't know if your app has enough privileges to restore the database while running.
You could use the sqlite3 commandline tool to dump an sql file, see here and send it to your webserver. Restoring would be something the same, but then you have to delete your tables first, then restore the database with the retrieved data.
Maybe it would be nice to have some version dependency management like sending your app version with the backup. This way you can administer whether a database file is compatible or not.

Categories

Resources