<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Horatio Caine&#039;s Blog &#187; backup</title>
	<atom:link href="http://hxcaine.com/blog/tag/backup/feed/" rel="self" type="application/rss+xml" />
	<link>http://hxcaine.com/blog</link>
	<description>A blog of life, ideas and code</description>
	<lastBuildDate>Thu, 19 Jun 2014 00:46:16 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Backing up, importing and restoring databases on Android</title>
		<link>http://hxcaine.com/blog/2010/09/14/backing-up-importing-and-restoring-databases-on-android/</link>
		<comments>http://hxcaine.com/blog/2010/09/14/backing-up-importing-and-restoring-databases-on-android/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 12:43:42 +0000</pubDate>
		<dc:creator><![CDATA[Horatio Caine]]></dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[load]]></category>
		<category><![CDATA[restore]]></category>
		<category><![CDATA[save]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[store]]></category>
		<category><![CDATA[valid]]></category>

		<guid isPermaLink="false">http://hxcaine.wordpress.com/?p=79</guid>
		<description><![CDATA[If your Android application has a database and you want your users to be able to backup the database and restore it as they see fit, you&#8217;ll need to mess about with database files. Below is a class called DbExportImport. Once you&#8217;ve set it up with the correct values, all you need to do is [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>If your Android application has a database and you want your users to be able to backup the database and restore it as they see fit, you&#8217;ll need to mess about with database files.</p>
<p>Below is a class called DbExportImport. Once you&#8217;ve set it up with the correct values, all you need to do is call <strong>exportDb()</strong>, <strong>importDb()</strong> or <strong>restoreDb()</strong> from your application to perform the necessary operations.</p>
<p>This is also useful as a temporary measure when changing your package name or key for application signing, as your application will be newly installed and you will lose your database.</p>
<p>I have only left in a few comments, so for any clarification, leave a comment and I&#8217;ll make it a little clearer.</p>
<p></p><pre class="crayon-plain-tag">package testCode;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Environment;
import android.text.format.DateFormat;
import android.util.Log;

public class DbExportImport {

	public static final String TAG = DbExportImport.class.getName();

	/** Directory that files are to be read from and written to **/
	protected static final File DATABASE_DIRECTORY =
		new File(Environment.getExternalStorageDirectory(),&quot;MyDirectory&quot;);

	/** File path of Db to be imported **/
	protected static final File IMPORT_FILE =
		new File(DATABASE_DIRECTORY,&quot;MyDb.db&quot;);

	public static final String PACKAGE_NAME = &quot;com.example.app&quot;;
	public static final String DATABASE_NAME = &quot;example.db&quot;;
	public static final String DATABASE_TABLE = &quot;entryTable&quot;;

	/** Contains: /data/data/com.example.app/databases/example.db **/
	private static final File DATA_DIRECTORY_DATABASE =
			new File(Environment.getDataDirectory() +
			&quot;/data/&quot; + PACKAGE_NAME +
			&quot;/databases/&quot; + DATABASE_NAME );

	/** Saves the application database to the
	 * export directory under MyDb.db **/
	protected static  boolean exportDb(){
		if( ! SdIsPresent() ) return false;

		File dbFile = DATA_DIRECTORY_DATABASE;
		String filename = &quot;MyDb.db&quot;;

		File exportDir = DATABASE_DIRECTORY;
		File file = new File(exportDir, filename);

		if (!exportDir.exists()) {
			exportDir.mkdirs();
		}

		try {
			file.createNewFile();
			copyFile(dbFile, file);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	/** Replaces current database with the IMPORT_FILE if
	 * import database is valid and of the correct type **/
	protected static boolean restoreDb(){
		if( ! SdIsPresent() ) return false;

		File exportFile = DATA_DIRECTORY_DATABASE;
		File importFile = IMPORT_FILE;

		if( ! checkDbIsValid(importFile) ) return false;

		if (!importFile.exists()) {
			Log.d(TAG, &quot;File does not exist&quot;);
			return false;
		}

		try {
			exportFile.createNewFile();
			copyFile(importFile, exportFile);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	/** Imports the file at IMPORT_FILE **/
	protected static boolean importIntoDb(Context ctx){
		if( ! SdIsPresent() ) return false;

		File importFile = IMPORT_FILE;

		if( ! checkDbIsValid(importFile) ) return false;

		try{
			SQLiteDatabase sqlDb = SQLiteDatabase.openDatabase
					(importFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);

			Cursor cursor = sqlDb.query(true, DATABASE_TABLE,
		    		null, null, null, null, null, null, null
		    );

			DbAdapter dbAdapter = new DbAdapter(ctx);
			dbAdapter.open();

			final int titleColumn = cursor.getColumnIndexOrThrow(&quot;title&quot;);
			final int timestampColumn = cursor.getColumnIndexOrThrow(&quot;timestamp&quot;);

			// Adds all items in cursor to current database
			cursor.moveToPosition(-1);
			while(cursor.moveToNext()){
				dbAdapter.createQuote(
						cursor.getString(titleColumn),
						cursor.getString(timestampColumn)
				);
			}

			sqlDb.close();
			cursor.close();
			dbAdapter.close();
		} catch( Exception e ){
			e.printStackTrace();
			return false;
		}

		return true;
	}

	/** Given an SQLite database file, this checks if the file
	 * is a valid SQLite database and that it contains all the
	 * columns represented by DbAdapter.ALL_COLUMN_KEYS **/
	protected static boolean checkDbIsValid( File db ){
		try{
			SQLiteDatabase sqlDb = SQLiteDatabase.openDatabase
				(db.getPath(), null, SQLiteDatabase.OPEN_READONLY);

			Cursor cursor = sqlDb.query(true, DATABASE_TABLE,
            		null, null, null, null, null, null, null
            );

			// ALL_COLUMN_KEYS should be an array of keys of essential columns.
			// Throws exception if any column is missing
			for( String s : DbAdapter.ALL_COLUMN_KEYS ){
				cursor.getColumnIndexOrThrow(s);
			}

			sqlDb.close();
			cursor.close();
		} catch( IllegalArgumentException e ) {
			Log.d(TAG, &quot;Database valid but not the right type&quot;);
			e.printStackTrace();
			return false;
		} catch( SQLiteException e ) {
			Log.d(TAG, &quot;Database file is invalid.&quot;);
			e.printStackTrace();
			return false;
		} catch( Exception e){
			Log.d(TAG, &quot;checkDbIsValid encountered an exception&quot;);
			e.printStackTrace();
			return false;
		}

		return true;
	}

	private static void copyFile(File src, File dst) throws IOException {
		FileChannel inChannel = new FileInputStream(src).getChannel();
		FileChannel outChannel = new FileOutputStream(dst).getChannel();
		try {
			inChannel.transferTo(0, inChannel.size(), outChannel);
		} finally {
			if (inChannel != null)
				inChannel.close();
			if (outChannel != null)
				outChannel.close();
		}
	}

	/** Returns whether an SD card is present and writable **/
	public static boolean SdIsPresent() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}
}</pre><p></p>
]]></content:encoded>
			<wfw:commentRss>http://hxcaine.com/blog/2010/09/14/backing-up-importing-and-restoring-databases-on-android/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>Backup, import and restore database options added to Quotation Keeper</title>
		<link>http://hxcaine.com/blog/2010/09/11/backup-import-and-restore-database-options-added-to-quotation-keeper/</link>
		<comments>http://hxcaine.com/blog/2010/09/11/backup-import-and-restore-database-options-added-to-quotation-keeper/#comments</comments>
		<pubDate>Sat, 11 Sep 2010 13:19:04 +0000</pubDate>
		<dc:creator><![CDATA[Horatio Caine]]></dc:creator>
				<category><![CDATA[Quotation Keeper]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[back up]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[load]]></category>
		<category><![CDATA[quotation keeper]]></category>
		<category><![CDATA[restore]]></category>
		<category><![CDATA[save]]></category>

		<guid isPermaLink="false">http://hxcaine.wordpress.com/?p=66</guid>
		<description><![CDATA[I&#8217;ve just added backup, import and restore capabilities to Quotation Keeper. Now if you change or format your device, or even want to send your entire database to another person, you only need to back up your database to your SD card and restore again when you&#8217;re ready. The difference between import and restore is [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve just added backup, import and restore capabilities to Quotation Keeper.</p>
<p>Now if you change or format your device, or even want to send your entire database to another person, you only need to back up your database to your SD card and restore again when you&#8217;re ready.</p>
<p>The difference between import and restore is simple:</p>
<p>If you <strong>import</strong>, you will <strong>add</strong> entries from the backed-up database to your existing database. If you <strong>restore</strong>, you will <strong>replace</strong> your own database with the backed-up one.</p>
<p><a href="http://i1.wp.com/hxcaine.com/blog/wp-content/uploads/2011/02/Pref-Backup-Import-Restore.png"><img class="alignnone size-medium wp-image-178" title="Pref-Backup-Import-Restore" src="http://i1.wp.com/hxcaine.com/blog/wp-content/uploads/2011/02/Pref-Backup-Import-Restore.png?resize=180%2C300" alt="" data-recalc-dims="1" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://hxcaine.com/blog/2010/09/11/backup-import-and-restore-database-options-added-to-quotation-keeper/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Updating files and folders to Android device with one click via adb</title>
		<link>http://hxcaine.com/blog/2010/09/11/updating-files-and-folders-with-one-click-via-adb/</link>
		<comments>http://hxcaine.com/blog/2010/09/11/updating-files-and-folders-with-one-click-via-adb/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 23:46:22 +0000</pubDate>
		<dc:creator><![CDATA[Horatio Caine]]></dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[adb]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[batch]]></category>
		<category><![CDATA[device]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[folders]]></category>
		<category><![CDATA[phone]]></category>
		<category><![CDATA[push]]></category>
		<category><![CDATA[save]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tablet]]></category>
		<category><![CDATA[update]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://hxcaine.wordpress.com/?p=55</guid>
		<description><![CDATA[I like to keep an updated copy of certain files on my Android phone at all times, such as the latest copy of certain code so I can look at it when I&#8217;m on the train. I&#8217;ve done it in quite a few different ways: Mount SD card as a removable storage device via USB [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I like to keep an updated copy of certain files on my Android phone at all times, such as the latest copy of certain code so I can look at it when I&#8217;m on the train.</p>
<p>I&#8217;ve done it in quite a few different ways:</p>
<ul>
<li>Mount SD card as a removable storage device via USB</li>
<li>Run an FTP server on my phone and transfer files over wifi</li>
<li>Bluetooth file transfer</li>
</ul>
<p>But these are all tedious because they&#8217;re all manual and they take too long.</p>
<p>Then I realised that ADB push and ADB shell are run from the command line/terminal, which means I can run a batch script. (A batch script is a text file with commands written that all run one after another in the command line)</p>
<p>Now all I need to do is click the file on my desktop and the files get updated within a fraction of a second on my phone.</p>
<p>So here&#8217;s an example version of my batch file that automatically updates a folder on my phone:</p>
<p></p><pre class="crayon-plain-tag">adb shell rm -r /sdcard/MyUpdateFolder

adb shell mkdir /sdcard/MyUpdateFolder

adb push C:/MyUpdateFolder /sdcard/MyUpdateFolder</pre><p></p>
<p><strong>&#8216;rm -r&#8217;</strong> delete the folder and everything in it.</p>
<p><strong>&#8216;mkdir&#8217; </strong>creates a folder by that name.</p>
<p><strong>&#8216;push&#8217;</strong> copies the folder contents from the first argument to the second argument.</p>
<p><strong>Note for those running emulators on their computers: Add &#8216;-d&#8217; after the word adb on each line to send the commands to a device and not an emulator.</strong></p>
<p>All that&#8217;s left is to write it up in Notepad and save it as Filename.bat. After that, just double-click the file and the operation should run in a fraction of a second.</p>
]]></content:encoded>
			<wfw:commentRss>http://hxcaine.com/blog/2010/09/11/updating-files-and-folders-with-one-click-via-adb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
