Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.88 KB | None | 0 0
  1. iso=/path/to/isoimage #e.g. iso=~/Downloads/ubuntu-16.04-desktop-amd64.iso
  2.  
  3. s=/mnt/isoimage #Source mount point for ISO files (via loop file system)
  4. t=/media/USBRoot #Target mount point for USB files in partition #1
  5.  
  6. dev=/dev/sd? #set the "?" to your USB drive letter, e.g. /dev/sdb
  7.  
  8. sudo dd if=/dev/zero of=$dev #bs=2048 is optional and doesn't seem to matter
  9.  
  10. dd: writing to ‘/dev/sdb’: No space left on device
  11. 30326785+0 records in
  12. 30326784+0 records out
  13. 15527313408 bytes (16 GB) copied, 4099.2 s, 3.8 MB/s
  14.  
  15. sudo umount ${dev}1
  16. sudo umount ${dev}2
  17.  
  18. **sudo fdisk $dev**
  19. **o** [create partition map], and then
  20.  
  21. n (p) (1) (2048) 27343750 [new partition #1]
  22. t c [change partition type to type c, or W95 FAT32], and
  23.  
  24. n (p) (2) (default) (default) [new partition #2]
  25. t 2 83 [type 83 or linux], then
  26. a (2) [set [toggle] the bootable flag], and
  27.  
  28. p [check new table], and finally
  29.  
  30. Disk /dev/sdb: 14.5 GiB, 15527313408 bytes, 30326784 sectors
  31. Units: sectors of 1 * 512 = 512 bytes
  32. Sector size (logical/physical): 512 bytes / 512 bytes
  33. I/O size (minimum/optimal): 512 bytes / 512 bytes
  34. Disklabel type: dos
  35. Disk identifier: 0xa42995f9
  36.  
  37. Device Boot Start End Sectors Size Id Type
  38. /dev/sdb1 2048 27343750 27341703 13G c W95 FAT32 (LBA)
  39. /dev/sdb2 * 27344896 30326783 2981888 1.4G 83 Linux
  40.  
  41. w [write the partition table to the usb drive]
  42.  
  43. The partition table has been altered.
  44. Calling ioctl() to re-read partition table.
  45. Syncing disks.
  46.  
  47. The partition table has been altered.
  48. Calling ioctl() to re-read partition table.
  49. Re-reading the partition table failed.: Device or resource busy
  50.  
  51. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
  52.  
  53. sudo dd if=$iso of=${dev}2
  54.  
  55. sudo mkfs.vfat -F 32 -c ${dev}1
  56.  
  57. sudo mkdir -p $t
  58. sudo mount ${dev}1 $t
  59.  
  60. sudo grub-install --no-floppy --root-directory=$t $dev
  61.  
  62. sudo mkdir -p $s
  63. sudo mount -o loop $iso $s
  64. sudo cp $s/casper/{vmlinuz.efi,initrd.lz} $t/boot/
  65.  
  66. sudo dd if=/dev/zero of=$t/casper-rw bs=1M count=1024 #takes a long time
  67. sudo mkfs.ext3 -F $t/casper-rw
  68.  
  69. sudo nano $t/boot/grub/grub.cfg
  70.  
  71. echo LOADING USB DRIVE
  72. echo
  73. echo To continue press any key
  74. echo To abort press ^-alt-delete
  75. echo
  76. read
  77. echo Proceeding...
  78.  
  79. set default=0
  80. set timeout=10
  81. set title="Ubuntu (Live)"
  82. set root=(hd0,msdos1)
  83.  
  84. linux /boot/vmlinuz.efi boot=casper file=/preseed/ubuntu.seed persistent ro splash quiet
  85. echo vmlinuz.efi loaded
  86.  
  87. initrd /boot/initrd.lz
  88. echo initrd.lz loaded
  89. echo
  90. echo Grub done. Booting...
  91. boot
  92.  
  93. sync
  94.  
  95. sudo umount $s; sudo rmdir $s
  96. sudo umount $t; sudo rmdir $t
  97.  
  98. #!/bin/bash
  99.  
  100. #Usage:
  101. #
  102. # mybackup - show list of current backups
  103. # myrestore - ''
  104. #
  105. # mybackup <machine> <BackupFolderName> - machine: love2d or sharon-pc
  106. # myrestore <machine> <BackupFolderName> - by convention name is 'nn-descriptiveName' (so it sorts by date)
  107.  
  108.  
  109. #################################################################
  110. #################################################################
  111.  
  112. ### PARTITIONS ######################
  113. #Partition labels (also used for mount point folder names):
  114. # Note: use labels rather than UUID as they might be more controllable.
  115. BackupDrive='Linux backup' # USB backup drive (I removed space from 'name' & it removed it from 'label')
  116. BackupBase="$BackupDrive/Backups" # Backup base folder directory & name
  117. SubDir="files"
  118.  
  119.  
  120. ### PARTITIONS LABEL HELP:
  121. #lsblk -o +label gives (note older method was blkid, but this suggests we use lsblk):
  122. #NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT LABEL
  123. #sda 8:0 0 465.8G 0 disk
  124. #├─sda1 8:1 0 199M 0 part SYSTEM
  125. #├─sda2 8:2 0 288.1G 0 part
  126. #├─sda3 8:3 0 1K 0 part
  127. #├─sda4 8:4 0 29.3G 0 part Shared
  128. #├─sda5 8:5 0 23.3G 0 part d8root
  129. #├─sda6 8:6 0 119.5G 0 part d8home
  130. #└─sda7 8:7 0 5.4G 0 part [SWAP]
  131.  
  132. #sdc 8:32 0 3.7T 0 disk
  133. #├─sdc1 8:33 0 128M 0 part
  134. #├─sdc2 8:34 0 2.7T 0 part /media/ubuntu/Seagate Backup Plus Drive Seagate Backup Plus Drive
  135. #└─sdc3 8:35 0 976.6G 0 part Linux backup
  136.  
  137.  
  138.  
  139. ### MOUNTING 1of2 ######################
  140. sudo umount "/mnt/$BackupDrive">& /dev/null # --- cleanup from prior failed attempt:
  141. sudo mkdir "/mnt/$BackupDrive">& /dev/null; sudo mount -L "$BackupDrive" "/mnt/$BackupDrive" -o defaults,suid >& /dev/null #Allow to set user owner of files
  142.  
  143.  
  144.  
  145.  
  146. ########################################################################
  147. ### FUNCTIONS #################################################################
  148.  
  149. ####################
  150. function usage { echo; echo "Usage: ${0##*/} [machine name: Love2d|Sharon-pc] [BackupFolderName]";echo;}
  151.  
  152. ####################
  153.  
  154. ####################
  155. #If parameter just show dirs for that machine, else show for both
  156. function myls {
  157. echo -n "'$1' existing backups:"
  158. if [ -d "$2" ]; then echo; ls -lFgG "$2" |grep -v ^total |grep ' [0-9][0-9]-' |sed 's/..................//'; else echo ' (none)'; fi;
  159. }
  160.  
  161. ##################
  162. function currentbackups {
  163. if [ "$1" ]; then
  164. myls "$1" "/mnt/$BackupBase/$1/$SubDir"
  165. else
  166. myls 'Love2d' "/mnt/$BackupBase/Love2d/$SubDir"
  167. echo
  168. myls 'Sharon-pc' "/mnt/$BackupBase/Sharon-pc/$SubDir"
  169. echo
  170. fi
  171. }
  172.  
  173. ###################
  174. function badmachine { echo "Machine type '$1' is invalid.";}
  175.  
  176. ###################
  177. function cleanup {
  178. # echo "--- cleaning up --------------------------------------"
  179. sudo umount "/mnt/$BackupDrive"
  180. }
  181.  
  182. ########################################################################
  183. ########################################################################
  184.  
  185.  
  186. ### CHECK INPUTS #######################################################
  187. #Check if backup name paramter exists:
  188. if [ $# = 0 ]; then usage; cleanup; exit; fi
  189.  
  190. if [ $# = 1 ]; then if [ "$1" != "Love2d" -a "$1" != "Sharon-pc" ]; then badmachine "$1"; usage; else usage; currentbackups "$1"; fi; cleanup; exit; fi
  191.  
  192. if [ $# = 2 ]; then if [ "$1" != "Love2d" -a "$1" != "Sharon-pc" ]; then badmachine "$1"; usage; cleanup; exit; fi; fi
  193.  
  194.  
  195. ### MOUNTING 2of2 ######################
  196. if [ "$1" = 'Love2d' ]; then
  197. MyHome='d8home' # Love2 Debian /home partition name
  198. MyRoot='d8root' # Love2 Debian / (root) partition name
  199. MyShared='Shared' # Love2 Debian Shared partition name
  200.  
  201. sudo umount "/mnt/$MyRoot" >& /dev/null # --- cleanup from prior failed attempt:
  202. sudo umount "/mnt/$MyHome" >& /dev/null #
  203. sudo umount "/mnt/$MyShared" >& /dev/null #
  204.  
  205. sudo mkdir "/mnt/$MyRoot" >& /dev/null; sudo mount -L "$MyRoot" "/mnt/$MyRoot" >& /dev/null
  206. sudo mkdir "/mnt/$MyHome" >& /dev/null; sudo mount -L "$MyHome" "/mnt/$MyHome" >& /dev/null
  207. sudo mkdir "/mnt/$MyShared" >& /dev/null; sudo mount -L "$MyShared" "/mnt/$MyShared" >& /dev/null
  208.  
  209. else
  210. # MyHome='uhome' # Love2 Ubuntu /home partition name
  211. # MyRoot='uroot' # Love2 Ubuntu / (root) partition name
  212.  
  213. MyHome='a41eaa3e-bd31-4ebc-86d4-cf8ed5f3e779' # Love2 Ubuntu /home partition name
  214. MyRoot='f3b7424c-0144-42a6-8488-62fbee94d245' # Love2 Ubuntu / (root) partition name
  215.  
  216.  
  217. sudo umount "/mnt/$MyRoot" >& /dev/null # --- cleanup from prior failed attempt:
  218. sudo umount "/mnt/$MyHome" >& /dev/null #
  219.  
  220. #sudo mkdir "/mnt/$MyRoot" >& /dev/null; sudo mount -L "$MyRoot" "/mnt/$MyRoot" >& /dev/null
  221. #sudo mkdir "/mnt/$MyHome" >& /dev/null; sudo mount -L "$MyHome" "/mnt/$MyHome" >& /dev/null
  222.  
  223. sudo mkdir "/mnt/$MyRoot" >& /dev/null; sudo mount -U "$MyRoot" "/mnt/$MyRoot" >& /dev/null
  224. sudo mkdir "/mnt/$MyHome" >& /dev/null; sudo mount -U "$MyHome" "/mnt/$MyHome" >& /dev/null
  225. fi
  226.  
  227.  
  228. #=================================================================
  229. BackupDir="$BackupBase/$1/$SubDir/$2" # /dir/BackupFolderName
  230.  
  231.  
  232. #rSync stuff:
  233. MyRsync="sudo rsync -aAXv --delete"
  234. RootExclude=" --exclude={"/dev/*","/lost+found","/media/*","/mnt/*","/proc/*","/run/*","/sys/*","/tmp/*"}"
  235. HomeExclude=" --exclude='*cache*'" #this does not work
  236.  
  237. if [ "${0##*/}" = "mybackup" ]; then
  238. echo backing up...
  239. sudo mkdir -p "/mnt/$BackupDir/root" # Making directories to save backup to
  240. sudo mkdir -p "/mnt/$BackupDir/home"
  241.  
  242. echo "--- Backing up: / -----------------------------------"
  243. $MyRsync $RootExclude "/mnt/$MyRoot/" "/mnt/$BackupDir/root/"
  244. echo "--- Backing up: /home -------------------------------"
  245.  
  246. $MyRsync $HomeExclude "/mnt/$MyHome/" "/mnt/$BackupDir/home/"
  247.  
  248. if [ "$MyShared" ]; then #no shared partion on Sharon's machine
  249. sudo mkdir -p "/mnt/$BackupDir/shared"
  250. echo "--- Backing up: Shared ------------------------------"
  251. $MyRsync "/mnt/$MyShared/" "/mnt/$BackupDir/shared/"
  252. fi
  253.  
  254. else
  255.  
  256. # Confirm
  257. read -p "YOU ARE ABOUT TO OVERWRITE YOUR PARTITIONS - CONFIRM (y/N)?" -n 1 -r; echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborting.";exit; fi
  258. echo;
  259. read -p "DANGER! Really overwrite your hard disk partitions? (y/N)?" -n 1 -r; echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborting.";exit; fi
  260.  
  261. echo "--- Restoring: / -----------------------------------"
  262. $MyRsync "/mnt/$BackupDir/root/" "/mnt/$MyRoot"
  263. echo "--- Restoring: /home -------------------------------"
  264. $MyRsync "/mnt/$BackupDir/home/" "/mnt/$MyHome"
  265.  
  266. if [ "$MyShared" ]; then #no shared partion on Sharon's machine
  267. echo "--- Restoring: Shared ------------------------------"
  268. $MyRsync "/mnt/$BackupDir/shared/" "/mnt/$MyShared"
  269. fi
  270.  
  271. fi
  272.  
  273. cleanup
  274. sudo umount "/mnt/$MyRoot" ;sudo rmdir "/mnt/$MyRoot"
  275. sudo umount "/mnt/$MyHome" ;sudo rmdir "/mnt/$MyHome"
  276.  
  277. if [ "$MyShared" ]; then #no shared partion on Sharon's machine
  278. sudo umount "/mnt/$MyShared" ;sudo rmdir "/mnt/$MyShared"
  279. fi
  280.  
  281. echo "=== DONE. ============================================"
  282.  
  283. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement