I've got a script I wrote to move root onto a USB drive for a
F2FS filesystem. I don't think I saw a script so far in this thread for F2FS, and the instructions seem to be specialized for EXT4. The script handles drives by PARTUUID; it generates a cmdline.txt file in /boot and also modifies fstab on the target drive.
Instructions
NOTE: Please back up all data before using this script. In particular, make sure you back up any data on your target USB drive that you intend to keep. Running gparted with partition changes or this script on the target USB drive can wipe out any and all data on the USB drive.
1. Install f2fs-tools, rsync, and gparted. In terminal:
Code: Select all
sudo apt-get update && sudo apt-get install f2fs-tools rsync gparted
2. Start gparted and create an adequately sized (larger than your current root folder)
f2fs partition on your target USB drive. This script is specific for f2fs target partitions and won't boot if the target partition isn't f2fs - this is because of the fstab file that is generated. If you want to modify the script to work with different filesystems, you just need to change the f2fs designation in the script. Take note of what you name the partition. Unplug and replug the USB drive such that the new partition is mounted.
3. Save the following code somewhere as backup.py
Code: Select all
#! /usr/bin/env python
import os, subprocess,sys,time
white = '\033[1;37m'
default = '\033[0;37m'
red = '\033[0;31m'
drives = []
if(len(sys.argv)==1):
entry = raw_input(white+"What is the name of the partition you would like to back up on?\n\t"+default)
else:
entry = sys.argv[1]
drives.append(entry)
filedir = os.path.dirname(os.path.abspath(__file__)) + '/'
d = subprocess.check_output(['df','-h'],shell=True)
d = d.replace("\n"," ")
driveslist = d.split(" ")
while(driveslist.count("")!=0):
driveslist.remove("")
devname = []
partname = []
copy = False
for i in range(len(driveslist)):
if(driveslist[i].count("/dev/")!=0 and driveslist[i-1].count("%")==0):
devname.append(driveslist[i])
copy = True
if(driveslist[i].count("%")!= 0 and driveslist[i-1].isdigit() and copy):
partname.append(driveslist[i+1])
copy = False
i = 0
while(i != len(partname)):
if('/media/pi' not in partname[i]):
partname.remove(partname[i])
devname.remove(devname[i])
else:
partname[i]=partname[i][len('/media/pi/'):]
i+=1
partition = ""
dev = ""
for i in partname:
if(drives.count(i)!=0):
partition = i
dev = devname[partname.index(i)]
print(i)
print(dev)
break
else:
sys.exit(red+"No valid drives found"+default)
uuid = ""
puuid = ""
blkid = subprocess.check_output(['sudo','blkid',dev])
blkid = blkid[blkid.index('UUID="')+6:]
uuid = blkid[:blkid.index('"')]
blkid = blkid[blkid.index('UUID="')+6:]
puuid = blkid[:blkid.index('"')]
subprocess.call(['sudo','umount',dev])
subprocess.call(['sudo','mount',dev,'/mnt'])
subprocess.call(['sudo','rsync','-axv','--delete','/','/mnt'])
f = open('/home/pi/'+partition+'-cmdline.txt','w')
f.write('dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=PARTUUID='+puuid+' rootfstype=f2fs elevator=deadline rootwait rootdelay=5')
f.close()
f = open('/home/pi/fstab','w')
f.write('proc /proc proc defaults 0 0\n/dev/mmcblk0p1 /boot vfat defaults 0 2\n#/dev/mmcblk0p2 / ext4 defaults,noatime 0 1\n# a swapfile is not a swap partition, no line here\n# use dphys-swapfile swap[on|off] for that\n/dev/disk/by-uuid/'+uuid+' / f2fs defaults,noatime 0 0')
f.close()
subprocess.call(['sudo','mv','/home/pi/fstab','/mnt/etc/fstab'])
subprocess.call(['sudo','mv','/home/pi/'+partition+'-cmdline.txt','/boot/'+partition+'-cmdline.txt'])
subprocess.call(['sudo','umount','/mnt'])
4. Run the script by navigating to the directory containing backup.py in terminal and then entering "python backup.py"
5. Enter the name of the partition. For example, if the partition is mounted as /media/pi/
partition_name , then
partition_name is input.
6. After the script completes, a new file should be present in /boot containing the cmdline.txt specific for the new backup. The name of the cmdline.txt variant is the partition name preceding cmdline.txt. Using the previous example, the filename should be partition_name-cmdline.txt. Copying this new cmdline.txt variant over the cmdline.txt will change the root directory to be shifted over to the USB drive on next boot. It's probably a good idea to back up the old cmdline.txt for convenience.
Code: Select all
sudo cp /boot/ cmdline.txt backup-cmdline.txt
sudo cp (partition name)-cmdline.txt cmdline.txt
***
That should be it. Tell me if it isn't working. I like the script a lot and use it often. The script allows for arguments and so you can conveniently set up a series of backups like this:
python backup.py partition1 && python backup.py partition2 && python backup.py partition3