如何完全备份android系统上的system分区和data分区

安卓的系统备份在很多场合下可能会用到,下面这个python的脚本,可以用来备份整个的data分区:将所有data分区的文件和目录打包到data.zip,并生成recovery的专用edify脚本到update-script.sh。当然,简单的修改下脚本的备份路径就可以备份system分区了,如果再加上几行代码,就可以备份boot,recovery,uboot等等分区,不过这要根据不同的分区来写不同的备份代码:

backup.py

import sys
import os
from os.path import join
from os import path
import zipfile
from zipfile import ZipFile
 
def packdir(dir,z,dirinfo,emptydirs,linkinfo):
    dirs = os.listdir(dir)
    if not dirs:
        emptydirs.append(dir)
    else:
        for d in dirs:
            absdir = join(dir,d)
            if not path.islink(absdir) and (path.isdir(absdir) or path.isfile(absdir)):
                break
        else:
            emptydirs.append(dir)
 
    for d in dirs:
        absdir = join(dir,d)
        st = os.lstat(absdir)
        info = [st.st_uid,st.st_gid,st.st_mode]
        if path.islink(absdir):
            info.append(absdir)
            linkdir = os.readlink(absdir)
            linkinfo[linkdir] = info
        elif path.isdir(absdir):
            dirinfo[absdir] = info
            packdir(absdir,z,dirinfo,emptydirs,linkinfo)
        elif path.isfile(absdir):
            dirinfo[absdir] = info
            os.utime(absdir,(1403408099,1403408099))
            z.write(absdir)
        else:
             print 'file type unknow %s'%(absdir,)
            
zi = ZipFile('data.zip','w',zipfile.ZIP_DEFLATED)
dirinfo = {}
emptydirs = []
linkinfo = {}
packdir('/data',zi,dirinfo,emptydirs,linkinfo)
 
 
f = open('update-script.sh','wb');
 
f.write('ifelse(is_mounted("/data"),unmount("/data"),0);\n')
f.write('format("ext4", "EMMC", "/dev/block/data", "0", "/data");\n')
f.write('mount("ext4", "EMMC", "/dev/block/data", "/data");\n')
f.write('package_extract_dir("data", "/data");\n')
 
mkdir_cmd = 'run_program("/sbin/busybox","mkdir","-p","'
mkdir_cmd += '","'.join(emptydirs)
mkdir_cmd += '");\n'
 
f.write(mkdir_cmd)
 
for key in dirinfo.keys():
    info = dirinfo[key]
    info = list(info)
    info.append(key)
    info = tuple(info)
    set_perm = 'set_perm(%d, %d, %d, "%s");\n'%info
    f.write(set_perm)
 
for key in linkinfo:
    info = linkinfo[key]
    info = tuple(info)
    symlink = 'symlink("%s", "%s");\n'%(key,info[3])
    set_perm = 'run_program("/sbin/busybox","chown","-h","install:install","%s");\n'%(info[3],)
    f.write(symlink)
    f.write(set_perm)
 
f.write('unmount("/data");')
 
f.close()
update_script='update-script.sh' 
os.utime(update_script,(1403408099,1403408099))
zi.write(update_script)
 
zi.close()
 

如何运行:
先要有root权限,另外,该脚本的运行依赖于python-for-android。

然后要将该脚本打包到apk里,或者在adb shell上通过命令行的方式执行。
--------------------- 
作者:i2cbus 
来源:CSDN 
原文:https://blog.csdn.net/I2Cbus/article/details/38637993 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Acronis Disk Director Suite 10.0 Window 8.1/8/7/Vista/xp硬盘分区 注意:Acronis Disk Director Suite 10.0 中文简体汉化版有问题,一定要使用英文版本。 Acronis Disk Director Suite 10.0 调整硬盘分区的教程 1、安装:Acronis Disk Director Suite 10.0 的安装和Acronis Partition Expert 2003 的安装过程一样,中间让输入注册码,输入后一直下一步完成安装,然后会提示是否启动计算机,点击确定重新启动。Vista之家还是要可惜一下,这么强的软件,还是英文的,没有简体中文版,英文不好的,大家还是跟着刺客一起往下看截图和分步讲解吧。 2、重启启动计算机后,桌面上会有个“Acronis Disk Director Suite”的图标,双击运行。第一次运行Acronis Disk Director Suite 10.0 ,和Acronis Partition Expert 200一样,仍然会提示软件的运行的模式,一种是自动模式(Automatic Mode),就是以向导模式运行,一种是手动模式(Manual Mode),Vista123建议大家使用手动模式,实际操作反而更简单。 3、进入软件的主窗体后,右上角Winzard板块里面有个Increase Free Space,我们就用这个功能来调整C盘,或者其他盘的分区,下面,Vista123姑且用C盘当作例子吧,因为Windows Vista用户大多都是苦恼于C盘的空间不足! 点击Increase Free Space: 想增加哪个分区的容量,就选择那个,我们就选择C吧。下一步点击next: 4、找个硬盘分区的剩余空间大的贡献一下,刺客在此就用F盘当例子吧,大家也看到了,刺客的t60本本就80g硬盘,所剩无几了各个分区。不废话了,选择f盘,然后next进行下一步。 5、下一步的窗口是调整C盘的容量(就是你刚才第3步选择的那个要增加空间的区),根据付出空间的那个分区的剩余空间,你自己滑动鼠标选择合适的吧。Vista之家就不截图了,简单的很。 5、Acronis Disk Director Suite 10.0 会弹出一个窗口,开始locking c盘和f盘,进行硬盘分区锁定,然后,就返回到软件主窗口。 6、看到菜单栏那个旗帜的图标没,就是commit的命令,意思是开始执行。点击之后,根据提示启动计算机。然后Acronis Disk Director Suite 会在进入Window Vista前把你的分区按照你的要求都调整好,非常方便!! 7、Acronis Disk Director Suite 10.0究竟好在哪儿?看起来和老版本界面没有变化啊?请看下一篇文章: Partition Expert 10.0 的官方网站介绍: Acronis Disk Director Suite 10.0 Partition Manager Dividing your hard drive into individual partitions for your operating system, programs, data files, music, and images provides better organization and, most important, much better data safety in the event of a drive crash. Acronis Disk Director Suite 10.0 makes this task a breeze! The suite includes the Acronis Partition Expert product which allows you to automatically or manually merge, split, resize, copy, and move partitions without losing data. It also lets you reorganize the hard disk drive structure, optimize disk space usage and much more. The easiest partition software for all users Acronis sets a new partitioning and design standards. Now non-technical PC users can be insulated from
declare v_part_type int; /* 20161212 按天分区 type:1 201612 按月分区 type:2 2016 按年分区 type:3 161212 按天分区 type:4 自动分区 type:5 */ v_part_key_value varchar2(20); v_part_prefix varchar2(50); v_part_name varchar2(252); v_col_type varchar2(64); v_part_length int; v_part_key_length int; v_part_key_position int; type cur_type is ref cursor; my_cur sys_refcursor; val varchar2(1000); v_year varchar2(50); v_max_part_name varchar2(256); v_maxdate_part_name varchar2(256); v_part_tablespace varchar2(256); v_part_sums int; begin for v_minp_rec in (select a.table_owner, a.table_name, a.partition_name as minpart from dba_tab_partitions a, (select table_owner, table_name, decode(max(partition_position) - 1, 0, 1, max(partition_position) - 1) as part_posi from dba_tab_partitions t where table_owner NOT in ('SYS', 'SYSTEM') and t.table_name = 'F_PASSENGER_STOP' group by table_owner, table_name) b where a.table_owner = b.table_owner and a.table_name = b.table_name and a.partition_position = b.part_posi) loop v_part_name := v_minp_rec.minpart; --------------------------------------------------获取分区类型--------------------------------------------------- select length(v_part_name) - regexp_instr(reverse(v_part_name), '\D') into v_part_key_position from dual; -----获取分区key开始位置 v_part_key_value := substr(v_part_name, v_part_key_position + 2); -----获取分区key v_part_key_length := length(v_part_key_value); -----获取分区 if v_part_key_length = 8 then v_part_type := 1; elsif v_part_key_length = 6 then if regexp_instr(v_part_key_value, '201') = 1 then ---------判定是yyyymm v_part_type := 2; else ---------判定是yymmdd v_part_type := 4; end if; elsif v_part_key_length = 4 then v_part_type := 3; else dbms_output.put_line('-------------' || v_minp_rec.table_name || v_part_key_value); end if; v_part_prefix := substr(v_part_name, 0, length(v_part_name) - v_part_key_length); DBMS_OUTPUT.put_line('/*'); DBMS_OUTPUT.put_line('用户名:' || v_minp_rec.table_owner); DBMS_OUTPUT.put_line('表名: ' || v_minp_rec.table_name); DBMS_OUTPUT.put_line('分区类型值: ' || v_part_type); DBMS_OUTPUT.put_line('分区键值: ' || v_part_key_value); DBMS_OUTPUT.put_line('分区前缀: ' || v_part_prefix); select DATA_type into v_col_type from dba_tab_columns where table_name = v_minp_rec.table_name and owner = v_minp_rec.table_owner and column_name = (select column_name from dba_part_key_columns where name = v_minp_rec.table_name and owner = v_minp_rec.table_owner); DBMS_OUTPUT.put_line('分区键类型: ' || v_col_type); select partition_name into v_max_part_name from dba_tab_partitions where table_owner = v_minp_rec.table_owner and table_name = v_minp_rec.table_name and partition_position = (select max(partition_position) from dba_tab_partitions where table_owner = v_minp_rec.table_owner and table_name = v_minp_rec.table_name); DBMS_OUTPUT.put_line('最大分区名: ' || v_max_part_name); select max(partition_position) - 1 into v_part_sums from dba_tab_partitions where table_owner = v_minp_rec.table_owner and table_name = v_minp_rec.table_name; if v_part_sums = 0 then v_maxdate_part_name := v_max_part_name; v_part_type := 5; else select partition_name into v_maxdate_part_name from dba_tab_partitions where table_owner = v_minp_rec.table_owner and table_name = v_minp_rec.table_name and partition_position = v_part_sums; end if; DBMS_OUTPUT.put_line('最大日期分区名: ' || v_maxdate_part_name); select tablespace_name into v_part_tablespace from dba_tab_partitions where table_owner = v_minp_rec.table_owner and table_name = v_minp_rec.table_name and partition_name = v_maxdate_part_name; DBMS_OUTPUT.put_line('*/'); ------------------------------以to_date划分的日分区--------------------------- if (v_part_type = 1 or v_part_type = 4) and v_col_type = 'DATE' then open my_cur for --删除最大分区 select 'alter table ' || v_minp_rec.table_owner || '.' || v_minp_rec.table_name || ' drop partition ' || v_max_part_name || ';' from dual union all select 'alter table ' || v_minp_rec.table_owner || '.' || v_minp_rec.table_name || ' add partition ' || v_part_prefix || rq || ' values less than (TO_DATE(' || '''' || to_char(to_date(rq, 'yyyymmdd') + 1, 'YYYY-MM-DD HH24:MI:SS') || '''' || ',' || '''' || 'YYYY-MM-DD HH24:MI:SS' || '''' || '))' || 'tablespace ' || v_part_tablespace || ';' from (select to_char(trunc(sysdate + 120, 'yyyy') + level - 1, 'yyyymmdd') rq from dual connect by rownum <= 365) union all --增加最大分区 select 'alter table ' || v_minp_rec.table_owner || '.' || v_minp_rec.table_name || ' add partition ' || v_max_part_name || ' values less than (MAXVALUE)' || ' tablespace ' || v_part_tablespace || ';' from dual; LOOP ---输出执行语句 fetch my_cur into val; EXIT WHEN my_cur%NOTFOUND; DBMS_OUTPUT.put_line(val); end loop; ------------------------------以to_date划分的月分区--------------------------- elsif v_part_type = 2 and v_col_type = 'DATE' then open my_cur for --删除最大分区 select 'alter table ' || v_minp_rec.table_owner || '.' || v_minp_rec.table_name || ' drop partition ' || v_max_part_name || ';' from dual union all select 'alter table ' || v_minp_rec.table_owner || '.' || v_minp_rec.table_name || ' add partition ' || v_part_prefix || date_tab.part_name || ' values less than (TO_DATE(' || '''' || to_char(date_tab.lessvalue, 'YYYY-MM-DD HH24:MI:SS') || '''' || ',' || '''' || 'YYYY-MM-DD HH24:MI:SS' || '''' || ')) ' || 'tablespace ' || v_part_tablespace || ';' from (select '2017' || lpad(level, 2, '0') part_name, --需要更改年 add_months(to_date('2017' || lpad(level, 2, '0'), --需要更改年 'YYYYMM'), 1) lessvalue from dual connect by level <= 12) date_tab union all --增加最大分区 select 'alter table ' || v_minp_rec.table_owner || '.' || v_minp_rec.table_name || ' add partition ' || v_max_part_name || ' values less than (MAXVALUE)' || ' tablespace ' || v_part_tablespace || ';' from dual; LOOP ---输出执行语句 fetch my_cur into val; EXIT WHEN my_cur%NOTFOUND; DBMS_OUTPUT.put_line(val); end loop;
运行 清除fastboot进磁盘操作.bat 接着电脑自动安装驱动(不会自动安装的驱动路径在MiFlash安装目录下) 这时就进入磁盘模式了,接着就可以用DiskGenius,分区助手专业版和MiFlash进行操作了(还可以用DiskGenius做数据备份) 用MiFlash刷机(目的是为了还原删除的aboot也可以用我提供的备份还原)刷完后关MiFlash,可别拔掉手机哦 呵呵 补充:没用miflash刷机的一定要要还原aboot分区要不后面改好了启动不来(一定要刷机要不你自己备份的也是清除的分区里面没数据是没用的) 3. 打开DiskGenius要操作的磁盘是QualcommMMCStorage(里面有27个分区的就是) 认为有必要的备份好整个分区表和分区(极力推荐备份)。 也可以用我提供的备份(有些分区是各手机私有的不能用我的备份分别是ssd dbi bk1 bk2 modemst1 modemst2 fsc bk3 fsg bk4 bk5 这11个分区同时也是不可动的)。 以下是两种分区方法 选其中一种即可 1。 用分区助手无损移动分区 2。 用DiskGenius删分区再重建 第一种: 方法删除 system1 boot1 modem1 userdata四个分区就可以了。(认为有必要的懂得可以删其他分区) 用分区助手移动分区把未分配空间集合在右边也就是userdata分区那一边保存修改(提交)然后用DiskGenius新建分区userdata把剩下的空间都给它 文件系统类型linux data partion 勾上对齐到下列扇区数的整数倍数值是8 详细里面的分区名userdata 再点上无盘符。 分区建好保存更改(分区名一定要是userdata)。 换到分区助手格式化userdata分区 格式ext3其他默认(可以开机后进fastboot用命令格式化此分区命令是fastboot format userdata)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值