Advertisement
xe1phix

Xe1phix-[Btrfs]-Cheatsheet-[v7.7.5].sh

Nov 10th, 2022
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 39.04 KB | None | 0 0
  1. #!/bin/sh
  2. ## Btrfs.sh
  3.  
  4.  
  5.  
  6. ##-======================================-##
  7. ##   [+] Show The Btrfs Full Help Page
  8. ##-======================================-##
  9. btrfs --help --full
  10.  
  11.  
  12. ##-===============================================-##
  13. ##   [+] List of Filesystem Features Supported:
  14. ##-===============================================-##
  15. mkfs.btrfs -O list-all
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.                     ## --------------------------------------------------------------- ##
  23. mixed-bg            ##  [+] Mixed data and metadata block groups
  24.                     ## --------------------------------------------------------------- ##
  25.  
  26.                     ## --------------------------------------------------------------- ##
  27. extref              ##  [+] Increased hardlink limit per file in a directory to 65536,
  28.                     ## --------------------------------------------------------------- ##
  29.  
  30.                     ## --------------------------------------------------------------- ##
  31. raid56              ##  [+] Extended format for RAID5/6
  32.                     ## --------------------------------------------------------------- ##
  33.  
  34.                     ## --------------------------------------------------------------- ##
  35. no-holes            ##  [+] Improved representation of file extents.
  36.                     ##      ( where holes arnt Stored as an extent.)
  37.                     ##  [?] Saves a few percent of metadata if sparse files are used
  38.                     ## --------------------------------------------------------------- ##
  39.  
  40.  
  41.  
  42.  
  43. ##-================================-##
  44. ##   [+] Format A Partition:
  45. ##-================================-##
  46.  
  47.  
  48. ##-=======================================-##
  49. ##   [+] Create A New Btrfs Partition:
  50. ##-=======================================-##
  51.  
  52.  
  53.  
  54. ##-==================================================-##
  55. ##   [+] Specify A Label For The New Partition:
  56. ##-==================================================-##
  57. ## -------------------------------------------------- ##
  58. ##   [?] The String Needs To Be Less Than 256 Bytes
  59. ## -------------------------------------------------- ##
  60. mkfs.btrfs -L BtreeFS /dev/sdc
  61.  
  62.  
  63.  
  64.  
  65. ##-====================================================================-##
  66. ##   [+] Specify An Allocated Profile For Your New Btrfs Filesystem:
  67. ##-====================================================================-##
  68. ## -------------------------------------------------------------------- ##
  69. ##  [?] A Single Device Filesystem Will Default To DUP
  70. ##  [?] Unless A SSD is Detected. Then it Will Default To Single.
  71. ## -------------------------------------------------------------------- ##
  72.  
  73. mkfs.btrfs -m single /dev/sdc
  74. mkfs.btrfs -m mixed /dev/sdc
  75.  
  76.  
  77. ## --------------------------------------------------------------- ##
  78. ##   [+] The mixed mode will remove the isolation and then,
  79. ##   [+] Store both types in the same block group type.
  80. ## --------------------------------------------------------------- ##
  81.  
  82.  
  83.  
  84. ## -------------------------------------------------------------------- ##
  85. ##  [?] The detection is based on the value of the queued rotation.
  86. ##  [?] where DEV is the short name of the device.
  87. ## -------------------------------------------------------------------- ##
  88. cat /sys/block/DEV/queue/rotational
  89.  
  90.  
  91.  
  92. ## --------------------------------------------------------------- ##
  93. ##   [+] Combined Both  Options:
  94. ## --------------------------------------------------------------- ##
  95. ##   [?] Specify The Data Block Groups Profile:
  96. ##   [?] Specify The Metadata Block Groups Profile:
  97. ## --------------------------------------------------------------- ##
  98. mkfs.btrfs --data single --label ChickenFucker /dev/sdb
  99. mkfs.btrfs --data single --metadata single --label Xe1phixGitLab /dev/sdc
  100. mkfs.btrfs --data single --metadata single /dev/sdc
  101.  
  102.  
  103.  
  104.  
  105. ##-===================================================-##
  106. ##   [+]  Forcibly overwrite the block devices
  107. ##        when an existing filesystem is detected.
  108. ##-===================================================-##
  109. mkfs.btrfs --force --data single --label ChickenFucker /dev/sdb
  110.  
  111.  
  112. ##-=============================================================-##
  113. ##   [?]  By default, mkfs.btrfs will utilize libblkid
  114. ##        To check For any btrfs filesystems on the devices.
  115. ##-=============================================================-##
  116.  
  117.  
  118.  
  119.  
  120. ##-==============================================-##
  121. ##   [+] Create A Subvolume Named "btrfs-test"
  122. ##-==============================================-##
  123. cd /run/media/public/BTree/
  124. btrfs subvolume create btrfs-test
  125.  
  126.  
  127.  
  128. ##-====================================================-##
  129. ##   [+] Create A Subvolume, And Add It To A qgroup
  130. ##-====================================================-##
  131. btrfs subvolume create -i $qgroupID
  132.  
  133.  
  134.  
  135. ##-===========================================-##
  136. ##   [+] Snapshot btrfs-test For A Backup:
  137. ##-===========================================-##
  138. btrfs subvolume snapshot btrfs-test/ btrfs-test-snap/
  139.  
  140.  
  141. ##-===========================================-##
  142. ##   [+] Readonly snapshot of the subvolume
  143. ##-===========================================-##
  144. btrfs subvolume snapshot -r btrfs-test/ btrfs-test-snap/
  145.  
  146.  
  147. ##-=========================================================-##
  148. ##   [+] List Subvolumes And Snapshots In The Filesystem.
  149. ##-=========================================================-##
  150. btrfs subvolume list /mnt/btrfs-test/
  151. btrfs subvolume list -p -u -t btrfs-test
  152.  
  153.  
  154. ## ----------------------------------------------------------------------------------------------- ##
  155.     btrfs subvolume list -p             ## print parent ID
  156. ## ----------------------------------------------------------------------------------------------- ##
  157.     btrfs subvolume list -c             ## print the ogeneration of the subvolume
  158. ## ----------------------------------------------------------------------------------------------- ##
  159.     btrfs subvolume list -g             ## print the generation of the subvolume
  160. ## ----------------------------------------------------------------------------------------------- ##
  161.     btrfs subvolume list -u             ## print the uuid of subvolumes (and snapshots)
  162. ## ----------------------------------------------------------------------------------------------- ##
  163.     btrfs subvolume list -q             ## print the parent uuid of the snapshots
  164. ## ----------------------------------------------------------------------------------------------- ##
  165.     btrfs subvolume list -R             ## print the uuid of the received snapshots
  166. ## ----------------------------------------------------------------------------------------------- ##
  167.     btrfs subvolume list -t             ## print the result as a table
  168. ## ----------------------------------------------------------------------------------------------- ##
  169.     btrfs subvolume list --sort=        ## list the subvolume in order of gen, ogen, rootid or path
  170. ## ----------------------------------------------------------------------------------------------- ##
  171.  
  172.  
  173.  
  174. ##-================================-##
  175. ##   [+] Query Btrfs Partitions:       
  176. ##-================================-##
  177. btrfs filesystem usage
  178. btrfs filesystem usage /
  179. btrfs fi usage /mnt
  180.  
  181.  
  182. btrfs filesystem usage -h -T --si btrfs-test
  183. btrfs filesystem usage -h -T btrfs-test
  184. btrfs filesystem usage -h btrfs-test
  185.  
  186.  
  187. ##-=============================================-##
  188. ##   [+] Scan Devices For A Btrfs Filesystem
  189. ##-=============================================-##
  190. btrfs device scan --all-devices
  191. btrfs device scan /dev/sdc
  192.  
  193.  
  194.  
  195. use the mount options device to specify the list of devices to scan at the time of mount.
  196.  
  197. mount -o device=/dev/sdb,device=/dev/sdc /dev/sda /mnt
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. ##-===================================================================-##
  210. ##   [+] Populate the toplevel subvolume with files from $RootDir:
  211. ##-===================================================================-##
  212. mkfs.btrfs --rootdir $RootDir
  213.  
  214.  
  215. ##-=============================================-##
  216. ##   [+] Show Device IO Error Statistics
  217. ##-=============================================-##
  218. btrfs device stats /dev/sdc
  219.  
  220.  
  221. ##-===========================================-##
  222. ##   [+] Summarize Disk Usage of Each File:
  223. ##-===========================================-##
  224. btrfs filesystem du
  225.  
  226.  
  227. ##-=========================================================-##
  228. ##   [+] Show Space Usage Information For A Mount Point:
  229. ##-=========================================================-##
  230. btrfs filesystem df /media/xe1phix/Btrfs
  231.  
  232.  
  233.  
  234. echo "##-==========================================================================-##"
  235. echo "   [+] Shows How Much Has Been Allocated Out of The Total Available Space       "
  236. echo "##-==========================================================================-##"
  237. btrfs fi show
  238.  
  239.  
  240. echo "##-================================================-##"
  241. echo "   [+] See How The Allocation is Used:            "
  242. echo "       See How Much of it Contains Useful Data:       "
  243. echo "##-================================================-##"
  244. btrfs fi df ../BTree/
  245. btrfs fi df btrfs-test
  246. btrfs filesystem df /run/media/public/BTree
  247.  
  248.  
  249.  
  250. ##-====================================-##
  251. ##   [+] List The Btrfs-Control File:
  252. ##-====================================-##
  253. ls -l /dev/btrfs-control
  254.  
  255.  
  256. ##-====================================================-##
  257. ##   [+] Print Btrfs Features Allowed By The Kernel:
  258. ##-====================================================-##
  259. ls /sys/fs/btrfs/features/
  260.  
  261.  
  262. ##-=================================-##
  263. ##   [+] Print Btrfs Sector Size:
  264. ##-=================================-##
  265. cat -vT /sys/fs/btrfs/425b905b-424e-4bd6-a3d6-008011146f9b/sectorsize
  266.  
  267.  
  268. ##-===========================-##
  269. ##   [+] Print Device Size:
  270. ##-===========================-##
  271. cat -vT /sys/fs/btrfs/425b905b-424e-4bd6-a3d6-008011146f9b/devices/sde/size
  272.  
  273.  
  274. ##-=====================================-##
  275. ##   [+] Print Btrfs Stat Information:
  276. ##-=====================================-##
  277. cat -vT /sys/fs/btrfs/eafed205-e99b-4b12-b31b-57625b69c5c1/devices/sdc/stat
  278.  
  279.  
  280.  
  281. ##-============================================-##
  282. ##   [+] Show Btrfs Superblock Information
  283. ##-============================================-##
  284. btrfs inspect-internal dump-super --full --all /dev/sdc
  285.  
  286.  
  287. ##-=============================-##
  288. ##   [+] Dump Tree Structures
  289. ##-=============================-##
  290. btrfs inspect-internal dump-tree
  291. btrfs inspect-internal dump-tree --extents /dev/sdc
  292. btrfs inspect-internal dump-tree --device /dev/sdc
  293. btrfs inspect-internal dump-tree --uuid /dev/sdc
  294.  
  295.  
  296. ##-============================================-##
  297. ##   [+] Print Sizes And Statistics of Trees:
  298. ##-============================================-##
  299. btrfs inspect-internal tree-stats /dev/sdc
  300.  
  301.  
  302.  
  303. ##-=====================================================-##
  304. ##   [?] A filesystem object can be a the filesystem,
  305. ##       a subvolume, an inode or a device.
  306. ##-=====================================================-##
  307.  
  308.  
  309. ##-============================================================-##
  310. ##   [+] Lists available properties with their descriptions
  311. ##-============================================================-##
  312. btrfs property list ../BTree/
  313. btrfs property list ../BTree/btrfs-test
  314. btrfs property list ../BTree/btrfs-test-snap/
  315.  
  316.  
  317. ##-============================================-##
  318. ##   [+] Get a property from a btrfs object:
  319. ##-============================================-##
  320. btrfs property get ../BTree/
  321. btrfs property get -t s btrfs-test
  322. btrfs property get -t d btrfs-test
  323. btrfs property get -t f btrfs-test
  324.  
  325.  
  326.  
  327.  
  328. Sets a property on a btrfs object.
  329.  
  330.  
  331. btrfs property set
  332. btrfs property set -t s /media/xe1phix/BonerBruser ro true
  333. btrfs property set -t s /media/xe1phix/BonerBruser ro false
  334. btrfs property set -t f /media/xe1phix/BonerBruser label $Label
  335.  
  336. ##-==========================================================-##
  337. ##   [?] Btrfs automatically tries to compress files using
  338. ##        Lempel-Ziv-Oberhumer (LZO) or zlib compression
  339. ##-==========================================================-##
  340.  
  341. ##-=========================================================-##
  342. ##   [+] Mount A Btrfs File System with lzo Compression:
  343. ##-=========================================================-##
  344. mount -o compress=lzo /dev/sdb /mnt
  345.  
  346.  
  347.  
  348. ##-=========================================================-##
  349. ##   [+] Mount A Btrfs File System with zlib Compression:
  350. ##-=========================================================-##
  351. mount -o compress=zlib /dev/sdb /mnt
  352.  
  353.  
  354. ##-=========================================-##
  355. ##   [+] The FStab Config Would Look Like:
  356. ##-=========================================-##
  357. /dev/sdb /mnt   btrfs   defaults,compress=lzo 0 1
  358.  
  359.  
  360.  
  361. ##-================================================================-##
  362. ##   [+] Compression Can Be Set At The File System-Level
  363. ##   [?] By Mounting The File System with Compression Enabled
  364. ##-================================================================-##
  365. mount -o compress=lzo /dev/sdb /mnt/$Path
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373. ##-============================================================-##
  374. ##   [+] Create a subvolume, mount using lzo compression:
  375. ##       Create a subvolume named mysubvol:
  376. ##-============================================================-##
  377. btrfs subvolume create /mnt/Btreez/Btree-Subvolume
  378.  
  379.  
  380. ##-====================================================-##
  381. ##   [+] Mount the subvolume and enable compression
  382. ##-====================================================-##
  383. mount -o compress=lzo,subvol=Btree-Subvolume /dev/sdb /mnt/Btreez/Btree-Subvolume
  384.  
  385.  
  386.  
  387.  
  388. ##-==================================================================-##
  389. ##   [+] Create snapshots almost instantly, Initially They
  390. ##       Consume virtually no additional disk space
  391. ##       (any modest impact results from additional metadata).
  392. ##-==================================================================-##
  393.  
  394.  
  395.  
  396. ##-==================================================================-##
  397. ##   [+] Create a snapshot of the MYFILES subvolume and
  398. ##       put the newly created snapshot in /mnt/Btreez/Snapshot
  399. ##-==================================================================-##
  400. btrfs subvolume snapshot /mnt/Btreez/Btree-Snapshot /mnt/Btreez/Snapshot
  401.  
  402.  
  403.  
  404.  
  405.  
  406. ##-============================================-##
  407. ##   [+] Creating a Snapshot of a Subvolume
  408. ##-============================================-##
  409.  
  410.  
  411.  
  412. ##-=================================================================-##
  413. ##  [?] Btrfs also supports the creation of clones for individual
  414. ##      files within a file system or subvolume
  415. ##-=================================================================-##
  416.  
  417. ##-=================================================================-##
  418. ##  [?] Clones - are lightweight copies—only an inode is created,
  419. ##      and it shares the same disk blocks as the original file.
  420. ##-=================================================================-##
  421.  
  422.  
  423.  
  424.  
  425. ##-====================================-##
  426. ##   [+] Clone the file: myfile1
  427. ##       Creating the clone myfile3
  428. ##-====================================-##
  429. cp --reflink /mnt/Btreez/Btreez/$file1 /mnt/Btreez/Btreez/$file3
  430.  
  431.  
  432.  
  433.  
  434. ##-================================================================-##
  435. ##   [+] Identify which files have changed on a given subvolume:
  436. ##-================================================================-##
  437. btrfs subvolume find-new
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454. ##-============================================-##
  455. ##  [+] Run A Btrfs Check on The Filesystem:
  456. ##-============================================-##
  457. btrfs check --repair $device
  458.  
  459.  
  460. ##-============================================================================-##
  461. ##  [?] If you have a dead btrfs file system,
  462. ##  [+] you can try to mount it with the recovery mount option
  463. ##      which will try to seek for a usable copy of the tree root:
  464. ##-============================================================================-##
  465. mount -o recovery /dev/sdb /mnt
  466.  
  467.  
  468.  
  469. ##-====================================-##
  470. ##  [+] Force a sync on a filesystem
  471. ##-====================================-##
  472. btrfs filesystem sync /dev/sdc
  473.  
  474.  
  475. ##-====================================-##
  476. ##  [+] Add a device to a filesystem
  477. ##-====================================-##
  478. btrfs device add /dev/sdc /mnt/BTree/  
  479.  
  480.  
  481.  
  482.  
  483.  
  484. btrfs filesystem show /dev/sdc
  485.  
  486.  
  487.  
  488.  
  489.  
  490. /sys/fs/btrfs/a77fbf52-6ab1-4169-9ffe-e105f3809ec0/devices/dm-1/trace/pid
  491.  
  492.  
  493.  
  494. /sys/fs/btrfs/a77fbf52-6ab1-4169-9ffe-e105f3809ec0/devices/dm-1/trace/enable
  495.  
  496.  
  497. /sys/fs/btrfs/a77fbf52-6ab1-4169-9ffe-e105f3809ec0/devices/dm-1/integrity/read_verify
  498.  
  499.  
  500. /sys/fs/btrfs/a77fbf52-6ab1-4169-9ffe-e105f3809ec0/devices/dm-1/integrity/device_is_integrity_capable
  501.  
  502.  
  503.  
  504. /sys/fs/btrfs/a77fbf52-6ab1-4169-9ffe-e105f3809ec0/devices/dm-1/dm/uuid
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513. echo "##-============================================================================-##"
  514. echo "   [+] Remove the failed hard drive by mounting the system in Degraded Mode:      "
  515. echo "##-============================================================================-##"
  516. mount -o degraded /dev/sdb /mnt
  517.  
  518.  
  519.  
  520. echo "##-============================================-##"
  521. echo "   [+] Create Snapshots of those subvolumes:      "
  522. echo "##-============================================-##"
  523. btrfs subvolume snapshot /source /destination
  524.  
  525.  
  526.  
  527. echo "##-====================================-##"
  528. echo "   [+] Get the ID of that subvolume:      "
  529. echo "##-====================================-##"
  530.  
  531. btrfs subvolume list /mntpt
  532.  
  533.  
  534.  
  535. echo "##-============================================-##"
  536. echo "   [+] Then mount it to another mount point:      "
  537. echo "##-============================================-##"
  538.  
  539. umount /mntpt
  540. mount -o subvol=231 /dev/sdx /newmntpt
  541.  
  542.  
  543.  
  544.  
  545.  
  546. echo "##-============================-##"
  547. echo "   [+] create a subvolume:        "
  548. echo "##-============================-##"
  549.  
  550. btrfs subvolume create $Path/$Subvolume
  551.  
  552.  
  553.  
  554. echo "##-=============================================-##"
  555. echo "   [+] list the current subvolumes under <path>:   "
  556. echo "##-=============================================-##"
  557.  
  558. btrfs subvolume list -p $Path
  559.  
  560.  
  561.  
  562. echo "##-============================-##"
  563. echo "   [+] delete a subvolume:        "
  564. echo "##-============================-##"
  565.  
  566. btrfs subvolume delete $Path/$Subvolume
  567.  
  568. btrfs subvolume delete /home-snap/
  569.  
  570.  
  571. echo "##-====================================-##"
  572. echo "   [+] Change the default sub-volume:     "
  573. echo "##-====================================-##"
  574.  
  575. btrfs subvolume set-default subvolume-id /
  576.  
  577.  
  578.  
  579.  
  580. echo "##-============================================-##"
  581. echo "   [+] Enable qgroups (Quota Group Support):      "
  582. echo "##-============================================-##"
  583.  
  584. btrfs quota enable $Path
  585.  
  586.  
  587. echo "##-================================================================-##"
  588. echo "   [+] Create a qgroup (quota group) for each of the subvolumes       "
  589. echo "       Using their <subvolume id> and rescan them:                    "
  590. echo "##-================================================================-##"
  591.  
  592.  
  593. btrfs subvolume list $Path | cut -d' ' -f2 | xargs -I{} -n1 btrfs qgroup create 0/{} $Path
  594.  
  595.  
  596.  
  597.  
  598. btrfs quota rescan $Path
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605. echo "##-====================================================-##"
  606. echo "   [?] Quota groups in Btrfs form a tree hierarchy        "
  607. echo "## ---------------------------------------------------- ##"
  608. echo "   [?] whereby qgroups are attached to subvolumes.        "
  609. echo "##-====================================================-##"
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616. echo "##-==================================================================-##"
  617. echo "   [+] show usage and limits for a given path within a filesystem:      "
  618. echo "##-==================================================================-##"
  619. btrfs qgroup show -reF $Path
  620.  
  621.  
  622. echo "##-=================================-##"
  623. echo "   [+] apply a limit to a qgroup       "
  624. echo "##-=================================-##"
  625. btrfs qgroup limit
  626.  
  627.  
  628. echo "##-=================================-##"
  629. echo "   [+] Rescan the subvolumes:          "
  630. echo "##-=================================-##"
  631. btrfs quota rescan $Path
  632.  
  633.  
  634.  
  635. echo "##-===========================================================-##"
  636. echo "   [+] Then you can assign a limit to any subvolume using;       "
  637. echo "##-===========================================================-##"
  638. btrfs qgroup limit 100G $Path/$Subvolume
  639.  
  640.  
  641. echo "##-========================================-##"
  642. echo "   [+] You can look at quota usage using      "
  643. echo "##-========================================-##"
  644. btrfs qgroup show $Path
  645.  
  646.  
  647.  
  648.  
  649.  
  650. echo "##-========================================================-##"
  651. echo "   [+] Disable copy-on-write for single files/directories:    "
  652. echo "##-========================================================-##"
  653. chattr +C /dir/file
  654.  
  655.  
  656.  
  657.  
  658. echo "##-============================================-##"
  659. echo "   [+] Manually Defragment your root Directory:   "
  660. echo "##-============================================-##"
  661. btrfs filesystem defragment -r /
  662.  
  663.  
  664.  
  665. ##-=======================================================-##
  666. ## [+] Recursively defragment files under $Directory.
  667. ## [+] wait until all blocks are flushed,
  668. ## [+] Then force file compression.
  669. ##-=======================================================-##
  670.  
  671. btrfs filesystem defrag -v -r -f -clzo $Directory
  672.  
  673.  
  674.  
  675.  
  676. echo "##-========================================================================-##"
  677. echo "   [+] Btrfs Scrub - Fault isolation and checksum algorithms                  "
  678. echo "##-========================================================================-##"
  679. echo "## ------------------------------------------------------------------------ ##"
  680. echo "   > It Reads all the data and metadata on the filesystem                     "
  681. echo "   > Uses checksums and the duplicate copies from RAID                        "
  682. echo "     Storage to identify and repair any corrupt data.                         "  
  683. echo "## ------------------------------------------------------------------------ ##"
  684.  
  685.  
  686. echo "## -------------------------------------------------------------------------------- ##"
  687. echo "   > Fault isolation - is provided by storing metadata separately from user data      "
  688. echo "     and by protecting information through cyclical redundancy checks (CRCs)          "
  689. echo "     that are stored in a btree that is separate from the data.                       "
  690. echo "## -------------------------------------------------------------------------------- ##"
  691.  
  692.  
  693.  
  694.  
  695. echo "##-======================================================================-##"
  696. echo "   [+] Start a (background) scrub on the filesystem which contains /:       "
  697. echo "##-======================================================================-##"
  698. btrfs scrub start /mnt/BTree
  699.  
  700.  
  701. echo "##-===========================================-##"
  702. echo "   [+] Initiate a check of the file system       "
  703. echo "##-===========================================-##"
  704. btrfs scrub start /mnt/BTree
  705.  
  706.  
  707. btrfs scrub start
  708.  
  709.  
  710. ## ------------------------------------------------------------------------------------------------------------- ##
  711.     btrfs scrub start -f $Path|$Device          ## force starting new scrub even if a scrub is already running
  712.                                                 ## this is useful when scrub stats record file is damaged
  713. ## ------------------------------------------------------------------------------------------------------------- ##
  714.     btrfs scrub start -R $Path|$Device          ## raw print mode, print full data instead of summary
  715. ## ------------------------------------------------------------------------------------------------------------- ##
  716.     btrfs scrub start -r $Path|$Device          ## read only mode
  717. ## ----------------------------------------------------------------------------------------------- ##
  718.     btrfs scrub start -d $Path|$Device          ## stats per device
  719. ## ----------------------------------------------------------------------------------------------- ##
  720.  
  721.  
  722.  
  723. echo "##-============================================-##"
  724. echo "   [+] Check the status of a running scrub:       "
  725. echo "##-============================================-##"
  726. btrfs scrub status /mnt/BTree
  727.  
  728. ## ----------------------------------------------------------------------------------------------- ##
  729.     btrfs scrub status -d /mnt/BTree         ## stats per device
  730. ## ----------------------------------------------------------------------------------------------- ##
  731.     btrfs scrub status -R /mnt/BTree         ## print raw stats
  732. ## ----------------------------------------------------------------------------------------------- ##
  733.  
  734.  
  735.  
  736. echo "##-================================================================================-##"
  737. echo "   [+] Btrfs Balance - Passes all data in the FS through the allocator again.         "
  738. echo "##-================================================================================-##"
  739. echo "## -------------------------------------------------------------------------------- ##"
  740. echo "   [?] It is primarily intended to rebalance the data in the FS                       "
  741. echo "       Across the devices (when a device is added or removed).                        "
  742. echo "## -------------------------------------------------------------------------------- ##"
  743. echo "   [?] A balance will regenerate missing copies for the                               "
  744. echo "       Redundant RAID levels, if a device has failed.                                 "
  745. echo "## -------------------------------------------------------------------------------- ##"
  746. echo "##-================================================================================-##"
  747.  
  748.  
  749.  
  750. echo "##-================================================================================-##"
  751. echo "   [?] On a single-device filesystem a balance may be also useful for (temporarily)   "
  752. echo "       Reducing the amount of allocated but unused (meta)data chunks.                 "
  753. echo "##-================================================================================-##"
  754. btrfs balance start /
  755. btrfs balance status /
  756.  
  757.  
  758.  
  759.  
  760.  
  761. echo "##-==========================================================================-##"
  762. echo " [?] A snapshot is simply a subvolume that shares its data (and metadata)       "
  763. echo "     with some other subvolume, using btrfs's COW capabilities.                 "
  764. echo "##-==========================================================================-##"
  765. echo "## -------------------------------------------------------------------------- ##"
  766. echo "   [?] Note: Snapshots are not recursive. Every nested subvolume                "
  767. echo "             will be an empty directory inside the snapshot.                    "
  768. echo "## -------------------------------------------------------------------------- ##"
  769.  
  770.  
  771.  
  772. echo "##-=============================-##"
  773. echo "   [+] create a snapshot:          "
  774. echo "##-=============================-##"
  775. btrfs subvolume snapshot $source /$dest/
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782. echo "##-=================================-##"
  783. echo "     [+] Compression level (0 ~ 9)     "
  784. echo "##-=================================-##"
  785. btrfs-image -w -c 5 /run/media/public/BTrees ~/BTrees
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792. echo "## -------------------------------------------------------------------------- ##"
  793. echo "   [?] A subvolume can be sent to stdout or a file using the send command.      "
  794. echo "## -------------------------------------------------------------------------- ##"
  795. echo "   [?] Which is useful for copying a subvolume to an external device            "
  796. echo "            (e.g. a USB disk mounted at /backup above).                         "
  797. echo "## -------------------------------------------------------------------------- ##"
  798.  
  799.  
  800. btrfs send $RootBackup | btrfs receive $Backup
  801. btrfs send /root_backup | btrfs receive /backup
  802.  
  803.  
  804.  
  805.  
  806.  
  807. echo "##-==========================================================================-##"
  808. echo "   [+] Send only the incremental difference to /backup:                         "
  809. echo "##-==========================================================================-##"
  810. echo "## -------------------------------------------------------------------------- ##"
  811. echo "   [?] A new subvolume named root_backup_new will be present in /backup.        "
  812. echo "## -------------------------------------------------------------------------- ##"
  813.  
  814.  
  815. btrfs send -p $RootBackup $RootBackupNew | btrfs receive $Backup
  816. btrfs send -p /root_backup /root_backup_new | btrfs receive /backup
  817.  
  818. btrfs send --verbose -f ~/Btree-Backup          ## write to a file
  819. btrfs receive --dump -v
  820.  
  821.  
  822. -p <parent>      Send an incremental stream from <parent> to <subvol>.
  823.  
  824.  
  825.  -c <clone-src>   Use this snapshot as a clone source for an
  826.                      incremental send (multiple allowed)
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833. echo "##-====================================================================-##"
  834. echo "     [?] btrfs-check cannot be used on a mounted file system.             "
  835. echo "## -------------------------------------------------------------------- ##"
  836. echo "     [?] To be able to use btrfs-check without booting from a live USB:   "
  837. echo "         Regenerate the initial ramdisk using mkinitcpio:                 "
  838. echo "## -------------------------------------------------------------------- ##"
  839. echo "##-====================================================================-##"
  840.  
  841.  
  842. echo "##-=====================================-##"
  843. echo "   [+] Add It To The Initial Ramdisk:      "
  844. echo "##-=====================================-##"
  845. /etc/mkinitcpio.conf
  846.  
  847. BINARIES=("/usr/bin/btrfs")
  848.  
  849.  
  850.  
  851.  
  852. echo "##-============================================================-##"
  853. echo "   [+] Checksum Hardware Acceleration - To Verify if Btrfs        "
  854. echo "       Checksum is Hardware Accelerated:                          "
  855. echo "##-============================================================-##"
  856. dmesg | grep crc32c
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866. echo "##-========================================-##"
  867. echo "   [+] create a RAID1 mirror in Btrfs?        "
  868. echo "##-========================================-##"
  869. mkfs.btrfs -m raid1 -d raid1 /dev/sda1 /dev/sdb1
  870.  
  871.  
  872. echo "##-====================================================-##"
  873. echo "   [?] Show the data structure of the RAID partitions:    "
  874. echo "##-====================================================-##"
  875. btrfs fi df /mnt/BTree
  876.  
  877.  
  878. ## -------------------------------------------------- ##"
  879. ## ----------------- Example Output ----------------- ##"
  880. ## -------------------------------------------------- ##"
  881. echo "  Data, RAID1: total=1.00GB, used=128.00KB        "
  882. echo "  Data: total=8.00MB, used=0.00                   "
  883. echo "  System, RAID1: total=8.00MB, used=4.00KB        "
  884. echo "  System: total=4.00MB, used=0.00                 "
  885. echo "  Metadata, RAID1: total=1.00GB, used=24.00KB     "
  886. echo "  Metadata: total=8.00MB, used=0.00               "
  887. ## -------------------------------------------------- ##"
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895. echo "##-================================================================-##"
  896. echo "   [+] "
  897. echo "##-================================================================-##"
  898.  
  899.  
  900.  
  901.  
  902. ##-=========================================-##
  903. ##  [+] Show btrfs superblock information
  904. ##-=========================================-##
  905. btrfs-show-super
  906. btrfs inspect-internal dump-super --full --all /dev/sdc
  907.  
  908.  
  909.  
  910.  
  911. ##-=========================-##
  912. ##  [+] Backup A Snapshot:
  913.  ##-=========================-##
  914. btrfs send /root_backup | btrfs receive /backup
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922. ##-========================================================-##
  923. ##  [+] Send only the difference between two snapshots
  924. ##-========================================================-##
  925. btrfs send -p /root_backup /root_backup_new | btrfs receive /backup
  926.  
  927.  
  928.  
  929.  
  930.  
  931.  
  932.  
  933.  
  934.  
  935. btrfsctl        ## control program to create snapshots and subvolumes:
  936.  
  937. mount /dev/sda2 /mnt
  938.  
  939. btrfsctl -s new_subvol_name /mnt
  940. btrfsctl -s snapshot_of_default /mnt/default
  941. btrfsctl -s snapshot_of_new_subvol /mnt/new_subvol_name
  942. btrfsctl -s snapshot_of_a_snapshot /mnt/snapshot_of_new_subvol
  943.  
  944. ls /mnt
  945.  
  946. default snapshot_of_a_snapshot snapshot_of_new_subvol
  947. new_subvol_name snapshot_of_default
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959. ##-================================================-##
  960. ##  [+] Btrfs provides a mount option that
  961. ##      enables an auto-defragmentation helper
  962. ##-================================================-##
  963. mount -o autodefrag
  964.  
  965.  
  966.  
  967.  
  968.  
  969. ##-=====================================================-##
  970. ##  [+] Initiate Offline file system defragmentation
  971. ##-=====================================================-##
  972. btrfs filesystem defrag /mnt
  973.  
  974.  
  975. btrfs filesystem defragment
  976.  
  977. -v                  be verbose
  978.         -r                  defragment files recursively
  979.         -c[zlib,lzo,zstd]   compress the file while defragmenting
  980.         -f                  flush data to disk immediately after defragmenting
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987. ## ----------------------------------------------------------------------------------------------- ##
  988.     btrfstune -r            ## enable extended inode refs
  989. ## ----------------------------------------------------------------------------------------------- ##
  990.     btrfstune -x            ## enable skinny metadata extent refs
  991. ## ----------------------------------------------------------------------------------------------- ##
  992.     btrfstune -n            ## enable no-holes feature (more efficient sparse file representation)
  993. ## ----------------------------------------------------------------------------------------------- ##
  994.     btrfstune -f            ## force to do dangerous operation, make sure that you are aware of the dangers
  995. ## ----------------------------------------------------------------------------------------------- ##
  996.     btrfstune -u            ## change fsid, use a random one
  997. ## ----------------------------------------------------------------------------------------------- ##
  998.     btrfstune -U $UUID      ## change fsid to UUID
  999. ## ----------------------------------------------------------------------------------------------- ##
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005.  
  1006.  
  1007. ## ----------------------------------------------------------------------------------------------- ##
  1008.     btrfs check --super $SuperBlock             ## use this superblock copy
  1009. ## ----------------------------------------------------------------------------------------------- ##
  1010.     btrfs check --backup                        ## use the first valid backup root copy
  1011. ## ----------------------------------------------------------------------------------------------- ##
  1012.     btrfs check --repair                        ## try to repair the filesystem
  1013. ## ----------------------------------------------------------------------------------------------- ##
  1014.     btrfs check --readonly                      ## run in read-only mode (default)
  1015. ## ----------------------------------------------------------------------------------------------- ##
  1016.     btrfs check --init-csum-tree                ## create a new CRC tree
  1017. ## ----------------------------------------------------------------------------------------------- ##
  1018.     btrfs check --init-extent-tree              ## create a new extent tree
  1019. ## ----------------------------------------------------------------------------------------------- ##
  1020.     btrfs check --check-data-csum               ## verify checksums of data blocks
  1021. ## ----------------------------------------------------------------------------------------------- ##
  1022.     btrfs check --qgroup-report                 ## print a report on qgroup consistency
  1023. ## ----------------------------------------------------------------------------------------------- ##
  1024.     btrfs check --subvol-extents $SubVolID      ## print subvolume extents and sharing state
  1025. ## ----------------------------------------------------------------------------------------------- ##
  1026.     btrfs check --progress                      ## indicate progress
  1027. ## ----------------------------------------------------------------------------------------------- ##
  1028.     btrfs check --clear-space-cache v1|v2       ## clear space cache for v1 or v2
  1029. ## ----------------------------------------------------------------------------------------------- ##
  1030.  
  1031. btrfs check --progress --check-data-csum
  1032.  
  1033.  
  1034.  
  1035.  
  1036. ## ----------------------------------------------------------------------------------------------- ##
  1037.     btrfs rescue fix-device-size $Device        ## Re-align device and super block sizes.
  1038. ## ----------------------------------------------------------------------------------------------- ##
  1039.     btrfs rescue zero-log $Device               ## Clear the tree log.
  1040. ## ----------------------------------------------------------------------------------------------- ##
  1041.     btrfs rescue super-recover -v $Device       ## Recover bad superblocks from good copies
  1042. ## ----------------------------------------------------------------------------------------------- ##
  1043.     btrfs rescue chunk-recover -v $Device       ## Recover the chunk tree by scanning
  1044.                                                 ## the devices one by one.
  1045. ## ----------------------------------------------------------------------------------------------- ##
  1046.  
  1047.  
  1048.  
  1049.  
  1050. ##-===================================================================-##
  1051. ##  [+] Try to restore files from a damaged filesystem (unmounted)
  1052. ##-===================================================================-##
  1053. btrfs restore --verbose <device> <path> | -l <device>
  1054.  
  1055.  
  1056. ## ----------------------------------------------------------------------------------------------- ##
  1057.     btrfs restore --verbose --snapshots             ##
  1058. ## ----------------------------------------------------------------------------------------------- ##
  1059.     btrfs restore --verbose --xattr                 ## restore extended attributes
  1060. ## ----------------------------------------------------------------------------------------------- ##
  1061.     btrfs restore --verbose --metadata              ## restore owner, mode and times
  1062. ## ----------------------------------------------------------------------------------------------- ##
  1063.     btrfs restore --verbose --symlink               ## restore symbolic links
  1064. ## ----------------------------------------------------------------------------------------------- ##
  1065.     btrfs restore --verbose --ignore-errors         ## ignore errors
  1066. ## ----------------------------------------------------------------------------------------------- ##
  1067.     btrfs restore --verbose --overwrite             ##
  1068. ## ----------------------------------------------------------------------------------------------- ##
  1069.     btrfs restore --verbose --super $mirror         ## super mirror
  1070. ## ----------------------------------------------------------------------------------------------- ##
  1071.     btrfs restore --verbose --root $rootid          ## root objectid
  1072. ## ----------------------------------------------------------------------------------------------- ##
  1073.     btrfs restore --verbose -d $Dir                 ## find directory
  1074. ## ----------------------------------------------------------------------------------------------- ##
  1075.     btrfs restore --verbose --list-roots            ## list tree roots
  1076. ## ----------------------------------------------------------------------------------------------- ##
  1077.     btrfs restore --verbose -t $bytenr              ## tree location
  1078.    
  1079. ## ----------------------------------------------------------------------------------------------- ##
  1080.     btrfs restore --verbose -f $bytenr              ## filesystem location
  1081. ## ----------------------------------------------------------------------------------------------- ##
  1082.  
  1083.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement